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;
49 * The tunnels that will be used to send tcp- and udp-packets
51 static struct GNUNET_MESH_Tunnel *tcp_tunnel;
52 static struct GNUNET_MESH_Tunnel *udp_tunnel;
55 * Start the helper-process
57 * If cls != NULL it is assumed that this function is called as a result of a dying
58 * helper. cls is then taken as handle to the old helper and is cleaned up.
62 start_helper_and_schedule (void *cls,
63 const struct GNUNET_SCHEDULER_TaskContext *tc)
65 shs_task = GNUNET_SCHEDULER_NO_TASK;
66 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
80 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IFNAME", &ifname))
82 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
83 "No entry 'IFNAME' in configuration!\n");
88 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR", &ipv6addr))
90 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
91 "No entry 'IPV6ADDR' in configuration!\n");
96 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6PREFIX",
99 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
100 "No entry 'IPV6PREFIX' in configuration!\n");
105 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR", &ipv4addr))
107 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
108 "No entry 'IPV4ADDR' in configuration!\n");
113 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK", &ipv4mask))
115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116 "No entry 'IPV4MASK' in configuration!\n");
121 * Messages get passed to the function message_token
122 * When the helper dies, this function will be called again with the
123 * helper_handle as cls.
126 start_helper (ifname, ipv6addr, ipv6prefix, ipv4addr, ipv4mask,
127 "vpn-gnunet", start_helper_and_schedule, message_token,
130 GNUNET_free (ipv6addr);
131 GNUNET_free (ipv6prefix);
132 GNUNET_free (ipv4addr);
133 GNUNET_free (ipv4mask);
134 GNUNET_free (ifname);
136 /* Tell the dns-service to rehijack the dns-port
137 * The routing-table gets flushed if an interface disappears.
140 if (NULL != dns_connection)
141 GNUNET_CLIENT_notify_transmit_ready (dns_connection,
142 sizeof (struct GNUNET_MessageHeader),
143 GNUNET_TIME_UNIT_FOREVER_REL,
144 GNUNET_YES, &send_query, NULL);
146 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
147 helper_handle->fh_to_helper, &helper_write,
154 * Send an dns-answer-packet to the helper
157 helper_write (void *cls
158 __attribute__ ((unused)),
159 const struct GNUNET_SCHEDULER_TaskContext *tsdkctx)
161 if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
164 struct answer_packet_list *ans = answer_proc_head;
169 size_t len = ntohs (ans->pkt.hdr.size);
171 GNUNET_assert (ans->pkt.subtype == GNUNET_DNS_ANSWER_TYPE_IP);
173 GNUNET_assert (20 == sizeof (struct ip_hdr));
174 GNUNET_assert (8 == sizeof (struct udp_pkt));
175 size_t data_len = len - sizeof (struct answer_packet) + 1;
176 size_t net_len = sizeof (struct ip_hdr) + sizeof (struct udp_dns) + data_len;
178 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) + net_len;
180 struct ip_udp_dns *pkt = alloca (pkt_len);
182 GNUNET_assert (pkt != NULL);
183 memset (pkt, 0, pkt_len);
185 /* set the gnunet-header */
186 pkt->shdr.size = htons (pkt_len);
187 pkt->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
189 /* set the tun-header (no flags and ethertype of IPv4) */
191 pkt->tun.type = htons (0x0800);
193 /* set the ip-header */
194 pkt->ip_hdr.version = 4;
195 pkt->ip_hdr.hdr_lngth = 5;
196 pkt->ip_hdr.diff_serv = 0;
197 pkt->ip_hdr.tot_lngth = htons (net_len);
198 pkt->ip_hdr.ident = 0;
199 pkt->ip_hdr.flags = 0;
200 pkt->ip_hdr.frag_off = 0;
201 pkt->ip_hdr.ttl = 255;
202 pkt->ip_hdr.proto = IPPROTO_UDP;
203 pkt->ip_hdr.chks = 0; /* Will be calculated later */
204 pkt->ip_hdr.sadr = ans->pkt.from;
205 pkt->ip_hdr.dadr = ans->pkt.to;
207 pkt->ip_hdr.chks = calculate_ip_checksum ((uint16_t *) & pkt->ip_hdr, 5 * 4);
209 /* set the udp-header */
210 pkt->udp_dns.udp_hdr.spt = htons (53);
211 pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
212 pkt->udp_dns.udp_hdr.len = htons (net_len - sizeof (struct ip_hdr));
213 pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
215 memcpy (&pkt->udp_dns.data, ans->pkt.data, data_len);
217 GNUNET_CONTAINER_DLL_remove (answer_proc_head, answer_proc_tail, ans);
220 if (GNUNET_DISK_file_write (helper_handle->fh_to_helper, pkt, pkt_len) < 0)
222 cleanup_helper (helper_handle);
223 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
227 /* if more packets are available, reschedule */
228 if (answer_proc_head != NULL)
229 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
230 helper_handle->fh_to_helper, &helper_write,
235 * Receive packets from the helper-process
238 message_token (void *cls __attribute__ ((unused)), void *client
239 __attribute__ ((unused)),
240 const struct GNUNET_MessageHeader *message)
242 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
244 struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
245 GNUNET_HashCode *key;
247 /* ethertype is ipv6 */
248 if (ntohs (pkt_tun->tun.type) == 0x86dd)
250 struct ip6_pkt *pkt6 = (struct ip6_pkt *) message;
252 GNUNET_assert (pkt6->ip6_hdr.version == 6);
253 struct ip6_tcp *pkt6_tcp;
254 struct ip6_udp *pkt6_udp;
255 struct ip6_icmp *pkt6_icmp;
257 switch (pkt6->ip6_hdr.nxthdr)
261 pkt6_tcp = (struct ip6_tcp *) pkt6;
262 pkt6_udp = (struct ip6_udp *) pkt6;
264 if ((key = address6_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
266 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
268 GNUNET_assert (me != NULL);
272 sizeof (struct GNUNET_MESH_Tunnel *) +
273 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
274 ntohs (pkt6->ip6_hdr.paylgth);
276 struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
277 struct GNUNET_MessageHeader *hdr =
278 (struct GNUNET_MessageHeader *) (cls + 1);
279 GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
282 htons (sizeof (struct GNUNET_MessageHeader) +
283 sizeof (GNUNET_HashCode) + ntohs (pkt6->ip6_hdr.paylgth));
285 GNUNET_MESH_ApplicationType app_type;
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "me->addrlen is %d\n",
289 if (me->addrlen == 0)
291 /* This is a mapping to a gnunet-service */
292 memcpy (hc, &me->desc.service_descriptor, sizeof (GNUNET_HashCode));
294 if (IPPROTO_UDP == pkt6->ip6_hdr.nxthdr &&
295 (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP)) &&
296 (port_in_ports (me->desc.ports, pkt6_udp->udp_hdr.dpt) ||
297 testBit (me->additional_ports, ntohs (pkt6_udp->udp_hdr.dpt))))
299 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP);
301 memcpy (hc + 1, &pkt6_udp->udp_hdr, ntohs (pkt6_udp->udp_hdr.len));
304 else if (IPPROTO_TCP == pkt6->ip6_hdr.nxthdr &&
305 (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP))
306 && (port_in_ports (me->desc.ports, pkt6_tcp->tcp_hdr.dpt)))
308 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP);
310 memcpy (hc + 1, &pkt6_tcp->tcp_hdr, ntohs (pkt6->ip6_hdr.paylgth));
315 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "pip: %d\n", port_in_ports(me->desc.ports, pkt6_tcp->tcp_hdr.dpt));
318 if (me->tunnel == NULL && NULL != cls)
321 GNUNET_MESH_peer_request_connect_all (mesh_handle,
322 GNUNET_TIME_UNIT_FOREVER_REL,
325 GNUNET_PeerIdentity *)
327 send_pkt_to_peer, NULL,
331 else if (NULL != cls)
334 send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336 "Queued to send IPv6 to peer %x, type %d\n",
337 *((unsigned int *) &me->desc.peer), ntohs (hdr->type));
342 /* This is a mapping to a "real" address */
343 struct remote_addr *s = (struct remote_addr *) hc;
345 s->addrlen = me->addrlen;
346 memcpy (s->addr, me->addr, me->addrlen);
347 s->proto = pkt6->ip6_hdr.nxthdr;
348 if (s->proto == IPPROTO_UDP)
350 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP);
351 memcpy (hc + 1, &pkt6_udp->udp_hdr, ntohs (pkt6_udp->udp_hdr.len));
352 app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
353 if (NULL != udp_tunnel)
354 me->tunnel = udp_tunnel;
356 else if (s->proto == IPPROTO_TCP)
358 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP);
359 memcpy (hc + 1, &pkt6_tcp->tcp_hdr, ntohs (pkt6->ip6_hdr.paylgth));
360 app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
361 if (NULL != tcp_tunnel)
362 me->tunnel = tcp_tunnel;
368 if (me->tunnel == NULL && NULL != cls)
371 GNUNET_MESH_peer_request_connect_by_type (mesh_handle,
372 GNUNET_TIME_UNIT_FOREVER_REL,
377 if (GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY == app_type)
379 else if (GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY == app_type)
382 else if (NULL != cls)
385 send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392 "Packet to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x, which has no mapping\n",
393 pkt6->ip6_hdr.dadr[0], pkt6->ip6_hdr.dadr[1],
394 pkt6->ip6_hdr.dadr[2], pkt6->ip6_hdr.dadr[3],
395 pkt6->ip6_hdr.dadr[4], pkt6->ip6_hdr.dadr[5],
396 pkt6->ip6_hdr.dadr[6], pkt6->ip6_hdr.dadr[7],
397 pkt6->ip6_hdr.dadr[8], pkt6->ip6_hdr.dadr[9],
398 pkt6->ip6_hdr.dadr[10], pkt6->ip6_hdr.dadr[11],
399 pkt6->ip6_hdr.dadr[12], pkt6->ip6_hdr.dadr[13],
400 pkt6->ip6_hdr.dadr[14], pkt6->ip6_hdr.dadr[15]);
405 pkt6_icmp = (struct ip6_icmp *) pkt6;
406 /* If this packet is an icmp-echo-request and a mapping exists, answer */
407 if (pkt6_icmp->icmp_hdr.type == 0x80 &&
408 (key = address6_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
411 pkt6_icmp = GNUNET_malloc (ntohs (pkt6->shdr.size));
412 memcpy (pkt6_icmp, pkt6, ntohs (pkt6->shdr.size));
413 GNUNET_SCHEDULER_add_now (&send_icmp6_response, pkt6_icmp);
418 /* ethertype is ipv4 */
419 else if (ntohs (pkt_tun->tun.type) == 0x0800)
421 struct ip_pkt *pkt = (struct ip_pkt *) message;
422 struct ip_udp *udp = (struct ip_udp *) message;
423 struct ip_tcp *pkt_tcp;
424 struct ip_udp *pkt_udp;
425 struct ip_icmp *pkt_icmp;
427 GNUNET_assert (pkt->ip_hdr.version == 4);
429 /* Send dns-packets to the service-dns */
430 if (pkt->ip_hdr.proto == IPPROTO_UDP && ntohs (udp->udp_hdr.dpt) == 53)
432 /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
433 size_t len = sizeof (struct query_packet) + ntohs (udp->udp_hdr.len) - 9;
435 struct query_packet_list *query =
436 GNUNET_malloc (len + 2 * sizeof (struct query_packet_list *));
437 query->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS);
438 query->pkt.hdr.size = htons (len);
439 query->pkt.orig_to = pkt->ip_hdr.dadr;
440 query->pkt.orig_from = pkt->ip_hdr.sadr;
441 query->pkt.src_port = udp->udp_hdr.spt;
442 memcpy (query->pkt.data, udp->data, ntohs (udp->udp_hdr.len) - 8);
444 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, query);
446 GNUNET_assert (head != NULL);
448 if (dns_connection != NULL)
449 GNUNET_CLIENT_notify_transmit_ready (dns_connection, len,
450 GNUNET_TIME_UNIT_FOREVER_REL,
451 GNUNET_YES, &send_query, NULL);
455 uint32_t dadr = pkt->ip_hdr.dadr;
456 unsigned char *c = (unsigned char *) &dadr;
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packet to %d.%d.%d.%d, proto %x\n",
459 c[0], c[1], c[2], c[3], pkt->ip_hdr.proto);
460 switch (pkt->ip_hdr.proto)
464 pkt_tcp = (struct ip_tcp *) pkt;
465 pkt_udp = (struct ip_udp *) pkt;
467 if ((key = address4_mapping_exists (dadr)) != NULL)
469 struct map_entry *me =
470 GNUNET_CONTAINER_multihashmap_get (hashmap, key);
471 GNUNET_assert (me != NULL);
475 sizeof (struct GNUNET_MESH_Tunnel *) +
476 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
477 ntohs (pkt->ip_hdr.tot_lngth) - 4 * pkt->ip_hdr.hdr_lngth;
479 struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
480 struct GNUNET_MessageHeader *hdr =
481 (struct GNUNET_MessageHeader *) (cls + 1);
482 GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
485 htons (sizeof (struct GNUNET_MessageHeader) +
486 sizeof (GNUNET_HashCode) + ntohs (pkt->ip_hdr.tot_lngth) -
487 4 * pkt->ip_hdr.hdr_lngth);
489 GNUNET_MESH_ApplicationType app_type;
491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "me->addrlen is %d\n",
493 if (me->addrlen == 0)
495 /* This is a mapping to a gnunet-service */
496 memcpy (hc, &me->desc.service_descriptor, sizeof (GNUNET_HashCode));
498 if ((IPPROTO_UDP == pkt->ip_hdr.proto) &&
499 (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP)) &&
500 (port_in_ports (me->desc.ports, pkt_udp->udp_hdr.dpt) ||
501 testBit (me->additional_ports, ntohs (pkt_udp->udp_hdr.dpt))))
503 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP);
505 memcpy (hc + 1, &pkt_udp->udp_hdr, ntohs (pkt_udp->udp_hdr.len));
508 else if ((IPPROTO_TCP == pkt->ip_hdr.proto) &&
510 desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP))
511 && (port_in_ports (me->desc.ports, pkt_tcp->tcp_hdr.dpt)))
513 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP);
515 memcpy (hc + 1, &pkt_tcp->tcp_hdr,
516 ntohs (pkt->ip_hdr.tot_lngth) -
517 4 * pkt->ip_hdr.hdr_lngth);
520 if (me->tunnel == NULL && NULL != cls)
523 GNUNET_MESH_peer_request_connect_all (mesh_handle,
524 GNUNET_TIME_UNIT_FOREVER_REL,
527 GNUNET_PeerIdentity *)
529 send_pkt_to_peer, NULL,
533 else if (NULL != cls)
536 send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
538 "Queued to send IPv4 to peer %x, type %d\n",
539 *((unsigned int *) &me->desc.peer),
545 /* This is a mapping to a "real" address */
546 struct remote_addr *s = (struct remote_addr *) hc;
548 s->addrlen = me->addrlen;
549 memcpy (s->addr, me->addr, me->addrlen);
550 s->proto = pkt->ip_hdr.proto;
551 if (s->proto == IPPROTO_UDP)
553 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP);
554 memcpy (hc + 1, &pkt_udp->udp_hdr, ntohs (pkt_udp->udp_hdr.len));
555 app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
557 else if (s->proto == IPPROTO_TCP)
559 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP);
560 memcpy (hc + 1, &pkt_tcp->tcp_hdr,
561 ntohs (pkt->ip_hdr.tot_lngth) -
562 4 * pkt->ip_hdr.hdr_lngth);
563 app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
565 if (me->tunnel == NULL && NULL != cls)
568 GNUNET_MESH_peer_request_connect_by_type (mesh_handle,
569 GNUNET_TIME_UNIT_FOREVER_REL,
575 else if (NULL != cls)
578 send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
585 "Packet to %x which has no mapping\n", dadr);
590 pkt_icmp = (struct ip_icmp *) pkt;
591 if (pkt_icmp->icmp_hdr.type == 0x8 &&
592 (key = address4_mapping_exists (dadr)) != NULL)
595 pkt_icmp = GNUNET_malloc (ntohs (pkt->shdr.size));
596 memcpy (pkt_icmp, pkt, ntohs (pkt->shdr.size));
597 GNUNET_SCHEDULER_add_now (&send_icmp4_response, pkt_icmp);
606 write_to_helper (void *buf, size_t len)
608 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
612 schedule_helper_write (struct GNUNET_TIME_Relative time, void *cls)
614 if (GNUNET_SCHEDULER_NO_TASK != shs_task)
616 GNUNET_SCHEDULER_add_write_file (time, helper_handle->fh_to_helper,