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;
51 unsigned char addr[16];
56 * Start the helper-process
58 * If cls != NULL it is assumed that this function is called as a result of a dying
59 * helper. cls is then taken as handle to the old helper and is cleaned up.
63 start_helper_and_schedule(void *cls,
64 const struct GNUNET_SCHEDULER_TaskContext *tc) {
65 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
78 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IFNAME", &ifname))
80 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n");
84 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr))
86 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n");
90 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6PREFIX", &ipv6prefix))
92 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n");
96 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV4ADDR", &ipv4addr))
98 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n");
102 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV4MASK", &ipv4mask))
104 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n");
109 * Messages get passed to the function message_token
110 * When the helper dies, this function will be called again with the
111 * helper_handle as cls.
113 helper_handle = start_helper(ifname,
119 start_helper_and_schedule,
124 GNUNET_free(ipv6addr);
125 GNUNET_free(ipv6prefix);
126 GNUNET_free(ipv4addr);
127 GNUNET_free(ipv4mask);
130 /* Tell the dns-service to rehijack the dns-port
131 * The routing-table gets flushed if an interface disappears.
134 if (NULL != dns_connection)
135 GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
140 * Send an dns-answer-packet to the helper
143 helper_write(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
144 if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
147 struct answer_packet_list* ans = answer_proc_head;
148 size_t len = ntohs(ans->pkt.hdr.size);
150 GNUNET_assert(ans->pkt.subtype == GNUNET_DNS_ANSWER_TYPE_IP);
152 GNUNET_assert (20 == sizeof (struct ip_hdr));
153 GNUNET_assert (8 == sizeof (struct udp_pkt));
154 size_t data_len = len - sizeof(struct answer_packet) + 1;
155 size_t net_len = sizeof(struct ip_hdr) + sizeof(struct udp_dns) + data_len;
156 size_t pkt_len = sizeof(struct GNUNET_MessageHeader) + sizeof(struct pkt_tun) + net_len;
158 struct ip_udp_dns* pkt = alloca(pkt_len);
159 GNUNET_assert(pkt != NULL);
160 memset(pkt, 0, pkt_len);
162 /* set the gnunet-header */
163 pkt->shdr.size = htons(pkt_len);
164 pkt->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
166 /* set the tun-header (no flags and ethertype of IPv4) */
168 pkt->tun.type = htons(0x0800);
170 /* set the ip-header */
171 pkt->ip_hdr.version = 4;
172 pkt->ip_hdr.hdr_lngth = 5;
173 pkt->ip_hdr.diff_serv = 0;
174 pkt->ip_hdr.tot_lngth = htons(net_len);
175 pkt->ip_hdr.ident = 0;
176 pkt->ip_hdr.flags = 0;
177 pkt->ip_hdr.frag_off = 0;
178 pkt->ip_hdr.ttl = 255;
179 pkt->ip_hdr.proto = 0x11; /* UDP */
180 pkt->ip_hdr.chks = 0; /* Will be calculated later*/
181 pkt->ip_hdr.sadr = ans->pkt.from;
182 pkt->ip_hdr.dadr = ans->pkt.to;
184 pkt->ip_hdr.chks = calculate_ip_checksum((uint16_t*)&pkt->ip_hdr, 5*4);
186 /* set the udp-header */
187 pkt->udp_dns.udp_hdr.spt = htons(53);
188 pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
189 pkt->udp_dns.udp_hdr.len = htons(net_len - sizeof(struct ip_hdr));
190 pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
192 memcpy(&pkt->udp_dns.data, ans->pkt.data, data_len);
194 GNUNET_CONTAINER_DLL_remove (answer_proc_head, answer_proc_tail, ans);
197 if (GNUNET_DISK_file_write(helper_handle->fh_to_helper, pkt, pkt_len) < 0)
199 cleanup_helper(helper_handle);
200 GNUNET_SCHEDULER_add_now(start_helper_and_schedule, NULL);
204 /* if more packets are available, reschedule */
205 if (answer_proc_head != NULL)
206 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
207 helper_handle->fh_to_helper,
213 * Receive packets from the helper-process
216 message_token (void *cls,
217 void *client, const struct GNUNET_MessageHeader *message)
219 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
221 struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
223 /* ethertype is ipv6 */
224 if (ntohs (pkt_tun->tun.type) == 0x86dd)
226 struct ip6_pkt *pkt6 = (struct ip6_pkt *) message;
227 GNUNET_assert (pkt6->ip6_hdr.version == 6);
228 struct ip6_tcp *pkt6_tcp;
229 struct ip6_udp *pkt6_udp;
230 struct ip6_icmp *pkt6_icmp;
231 GNUNET_HashCode *key;
233 switch (pkt6->ip6_hdr.nxthdr)
237 pkt6_tcp = (struct ip6_tcp *) pkt6;
238 pkt6_udp = (struct ip6_udp *) pkt6;
240 if ((key = address_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
242 struct map_entry *me =
243 GNUNET_CONTAINER_multihashmap_get (hashmap, key);
244 GNUNET_assert (me != NULL);
248 sizeof (struct GNUNET_MESH_Tunnel *) +
249 sizeof (struct GNUNET_MessageHeader) +
250 sizeof (GNUNET_HashCode) + ntohs (pkt6->ip6_hdr.paylgth);
252 struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
253 struct GNUNET_MessageHeader *hdr =
254 (struct GNUNET_MessageHeader *) (cls + 1);
255 GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
257 hdr->size = htons (sizeof (struct GNUNET_MessageHeader) +
258 sizeof (GNUNET_HashCode) +
259 ntohs (pkt6->ip6_hdr.paylgth));
261 GNUNET_MESH_ApplicationType app_type;
262 if (me->addrlen == 0)
264 /* This is a mapping to a gnunet-service */
265 memcpy (hc, &me->desc.service_descriptor,
266 sizeof (GNUNET_HashCode));
268 if (0x11 == pkt6->ip6_hdr.nxthdr
270 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP))
271 && (port_in_ports (me->desc.ports, pkt6_udp->udp_hdr.dpt)
272 || testBit (me->additional_ports,
273 ntohs (pkt6_udp->udp_hdr.dpt))))
275 hdr->type = ntohs (GNUNET_MESSAGE_TYPE_SERVICE_UDP);
277 memcpy (hc + 1, &pkt6_udp->udp_hdr,
278 ntohs (pkt6_udp->udp_hdr.len));
281 else if (0x06 == pkt6->ip6_hdr.nxthdr
283 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP))
285 (port_in_ports (me->desc.ports, pkt6_tcp->tcp_hdr.dpt)))
287 hdr->type = ntohs (GNUNET_MESSAGE_TYPE_SERVICE_TCP);
289 memcpy (hc + 1, &pkt6_tcp->tcp_hdr,
290 ntohs (pkt6->ip6_hdr.paylgth));
293 if (me->tunnel == NULL && NULL != cls)
296 GNUNET_MESH_peer_request_connect_all (mesh_handle,
297 GNUNET_TIME_UNIT_FOREVER_REL,
306 else if (NULL != cls)
309 send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1,
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312 "Queued to send to peer %x, type %d\n",
313 *((unsigned int *) &me->desc.peer), ntohs(hdr->type));
318 /* This is a mapping to a "real" address */
319 struct remote_addr *s = (struct remote_addr*) hc;
320 s->addrlen = me->addrlen;
321 memcpy(s->addr, me->addr, me->addrlen);
322 s->proto= pkt6->ip6_hdr.nxthdr;
323 if (s->proto == 0x11)
325 hdr->type = GNUNET_MESSAGE_TYPE_REMOTE_UDP;
326 memcpy (hc + 1, &pkt6_udp->udp_hdr,
327 ntohs (pkt6_udp->udp_hdr.len));
328 app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
330 else if (s->proto == 0x06)
332 hdr->type = GNUNET_MESSAGE_TYPE_REMOTE_TCP;
333 memcpy (hc + 1, &pkt6_tcp->tcp_hdr,
334 ntohs (pkt6->ip6_hdr.paylgth));
335 if (ntohs(pkt6_tcp->tcp_hdr.dpt) == 443)
336 app_type = GNUNET_APPLICATION_TYPE_INTERNET_HTTPS_GATEWAY;
337 else if (ntohs(pkt6_tcp->tcp_hdr.dpt) == 80)
338 app_type = GNUNET_APPLICATION_TYPE_INTERNET_HTTP_GATEWAY;
340 app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
343 if (me->tunnel == NULL && NULL != cls)
345 *cls = GNUNET_MESH_peer_request_connect_by_type(mesh_handle,
346 GNUNET_TIME_UNIT_FOREVER_REL,
353 else if (NULL != cls)
356 send_pkt_to_peer(cls, (struct GNUNET_PeerIdentity*) 1, NULL);
362 pkt6_icmp = (struct ip6_icmp *) pkt6;
363 /* If this packet is an icmp-echo-request and a mapping exists, answer */
364 if (pkt6_icmp->icmp_hdr.type == 0x80
365 && (key = address_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
368 pkt6_icmp = GNUNET_malloc (ntohs (pkt6->shdr.size));
369 memcpy (pkt6_icmp, pkt6, ntohs (pkt6->shdr.size));
370 GNUNET_SCHEDULER_add_now (&send_icmp_response, pkt6_icmp);
375 /* ethertype is ipv4 */
376 else if (ntohs (pkt_tun->tun.type) == 0x0800)
378 struct ip_pkt *pkt = (struct ip_pkt *) message;
379 struct ip_udp *udp = (struct ip_udp *) message;
380 GNUNET_assert (pkt->ip_hdr.version == 4);
382 /* Send dns-packets to the service-dns */
383 if (pkt->ip_hdr.proto == 0x11 && ntohs (udp->udp_hdr.dpt) == 53)
385 /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
387 sizeof (struct query_packet) + ntohs (udp->udp_hdr.len) - 9;
389 struct query_packet_list *query =
390 GNUNET_malloc (len + 2 * sizeof (struct query_packet_list *));
391 query->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS);
392 query->pkt.hdr.size = htons (len);
393 query->pkt.orig_to = pkt->ip_hdr.dadr;
394 query->pkt.orig_from = pkt->ip_hdr.sadr;
395 query->pkt.src_port = udp->udp_hdr.spt;
396 memcpy (query->pkt.data, udp->data, ntohs (udp->udp_hdr.len) - 8);
398 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, query);
400 GNUNET_assert (head != NULL);
402 if (dns_connection != NULL)
403 GNUNET_CLIENT_notify_transmit_ready (dns_connection,
405 GNUNET_TIME_UNIT_FOREVER_REL,
412 void write_to_helper(void* buf, size_t len)
414 (void)GNUNET_DISK_file_write(helper_handle->fh_to_helper, buf, len);
417 void schedule_helper_write(struct GNUNET_TIME_Relative time, void* cls)
419 GNUNET_SCHEDULER_add_write_file (time, helper_handle->fh_to_helper, &helper_write, cls);