clarifying comment
[oweals/gnunet.git] / src / vpn / gnunet-daemon-vpn-helper.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file vpn/gnunet-daemon-vpn-helper.c
23  * @brief
24  * @author Philipp Toelke
25  */
26 #include <platform.h>
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>
37
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"
45
46 struct GNUNET_VPN_HELPER_Handle *helper_handle;
47
48 /**
49  * Start the helper-process
50  *
51  * If cls != NULL it is assumed that this function is called as a result of a dying
52  * helper. cls is then taken as handle to the old helper and is cleaned up.
53  * {{{
54  */
55 void
56 start_helper_and_schedule(void *cls,
57                           const struct GNUNET_SCHEDULER_TaskContext *tc) {
58     if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
59       return;
60
61     if (cls != NULL)
62       cleanup_helper(cls);
63     cls = NULL;
64
65     char* ifname;
66     char* ipv6addr;
67     char* ipv6prefix;
68     char* ipv4addr;
69     char* ipv4mask;
70
71     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IFNAME", &ifname))
72       {
73         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n");
74         exit(1);
75       }
76
77     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr))
78       {
79         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n");
80         exit(1);
81       }
82
83     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6PREFIX", &ipv6prefix))
84       {
85         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n");
86         exit(1);
87       }
88
89     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV4ADDR", &ipv4addr))
90       {
91         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n");
92         exit(1);
93       }
94
95     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV4MASK", &ipv4mask))
96       {
97         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n");
98         exit(1);
99       }
100
101     /* Start the helper
102      * Messages get passed to the function message_token
103      * When the helper dies, this function will be called again with the
104      * helper_handle as cls.
105      */
106     helper_handle = start_helper(ifname,
107                                  ipv6addr,
108                                  ipv6prefix,
109                                  ipv4addr,
110                                  ipv4mask,
111                                  "vpn-gnunet",
112                                  start_helper_and_schedule,
113                                  message_token,
114                                  NULL,
115                                  NULL);
116
117     GNUNET_free(ipv6addr);
118     GNUNET_free(ipv6prefix);
119     GNUNET_free(ipv4addr);
120     GNUNET_free(ipv4mask);
121     GNUNET_free(ifname);
122
123     /* Tell the dns-service to rehijack the dns-port
124      * The routing-table gets flushed if an interface disappears.
125      */
126     restart_hijack = 1;
127     if (NULL != dns_connection)
128       GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
129 }
130 /*}}}*/
131
132 /**
133  * Send an dns-answer-packet to the helper
134  */
135 void
136 helper_write(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
137     if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
138       return;
139
140     struct answer_packet_list* ans = answer_proc_head;
141     size_t len = ntohs(ans->pkt.hdr.size);
142
143     GNUNET_assert(ans->pkt.subtype == GNUNET_DNS_ANSWER_TYPE_IP);
144
145     GNUNET_assert (20 == sizeof (struct ip_hdr));
146     GNUNET_assert (8 == sizeof (struct udp_pkt));
147     size_t data_len = len - sizeof(struct answer_packet) + 1;
148     size_t net_len = sizeof(struct ip_hdr) + sizeof(struct udp_dns) + data_len;
149     size_t pkt_len = sizeof(struct GNUNET_MessageHeader) + sizeof(struct pkt_tun) + net_len;
150
151     struct ip_udp_dns* pkt = alloca(pkt_len);
152     GNUNET_assert(pkt != NULL);
153     memset(pkt, 0, pkt_len);
154
155     /* set the gnunet-header */
156     pkt->shdr.size = htons(pkt_len);
157     pkt->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
158
159     /* set the tun-header (no flags and ethertype of IPv4) */
160     pkt->tun.flags = 0;
161     pkt->tun.type = htons(0x0800);
162
163     /* set the ip-header */
164     pkt->ip_hdr.version = 4;
165     pkt->ip_hdr.hdr_lngth = 5;
166     pkt->ip_hdr.diff_serv = 0;
167     pkt->ip_hdr.tot_lngth = htons(net_len);
168     pkt->ip_hdr.ident = 0;
169     pkt->ip_hdr.flags = 0;
170     pkt->ip_hdr.frag_off = 0;
171     pkt->ip_hdr.ttl = 255;
172     pkt->ip_hdr.proto = 0x11; /* UDP */
173     pkt->ip_hdr.chks = 0; /* Will be calculated later*/
174     pkt->ip_hdr.sadr = ans->pkt.from;
175     pkt->ip_hdr.dadr = ans->pkt.to;
176
177     pkt->ip_hdr.chks = calculate_ip_checksum((uint16_t*)&pkt->ip_hdr, 5*4);
178
179     /* set the udp-header */
180     pkt->udp_dns.udp_hdr.spt = htons(53);
181     pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
182     pkt->udp_dns.udp_hdr.len = htons(net_len - sizeof(struct ip_hdr));
183     pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
184
185     memcpy(&pkt->udp_dns.data, ans->pkt.data, data_len);
186
187     GNUNET_CONTAINER_DLL_remove (answer_proc_head, answer_proc_tail, ans);
188     GNUNET_free(ans);
189
190     if (GNUNET_DISK_file_write(helper_handle->fh_to_helper, pkt, pkt_len) < 0)
191       {
192         cleanup_helper(helper_handle);
193         GNUNET_SCHEDULER_add_now(start_helper_and_schedule, NULL);
194         return;
195       }
196
197     /* if more packets are available, reschedule */
198     if (answer_proc_head != NULL)
199       GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
200                                        helper_handle->fh_to_helper,
201                                        &helper_write,
202                                        NULL);
203 }
204
205 /**
206  * Receive packets from the helper-process
207  */
208 void
209 message_token (void *cls,
210                void *client, const struct GNUNET_MessageHeader *message)
211 {
212   GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
213
214   struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
215
216   /* ethertype is ipv6 */
217   if (ntohs (pkt_tun->tun.type) == 0x86dd)
218     {
219       struct ip6_pkt *pkt6 = (struct ip6_pkt *) message;
220       GNUNET_assert (pkt6->ip6_hdr.version == 6);
221       struct ip6_tcp *pkt6_tcp;
222       struct ip6_udp *pkt6_udp;
223       struct ip6_icmp *pkt6_icmp;
224       GNUNET_HashCode *key;
225
226       switch (pkt6->ip6_hdr.nxthdr)
227         {
228         case 0x06:             /* TCP */
229         case 0x11:             /* UDP */
230           pkt6_tcp = (struct ip6_tcp *) pkt6;
231           pkt6_udp = (struct ip6_udp *) pkt6;
232
233           if ((key = address_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
234             {
235               struct map_entry *me =
236                 GNUNET_CONTAINER_multihashmap_get (hashmap, key);
237               GNUNET_assert (me != NULL);
238               GNUNET_free (key);
239
240               size_t size =
241                 sizeof (struct GNUNET_MESH_Tunnel *) +
242                 sizeof (struct GNUNET_MessageHeader) +
243                 sizeof (GNUNET_HashCode) + ntohs (pkt6->ip6_hdr.paylgth);
244
245               struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
246               struct GNUNET_MessageHeader *hdr =
247                 (struct GNUNET_MessageHeader *) (cls + 1);
248               GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
249
250               hdr->size = htons (sizeof (struct GNUNET_MessageHeader) +
251                                  sizeof (GNUNET_HashCode) +
252                                  ntohs (pkt6->ip6_hdr.paylgth));
253
254               GNUNET_MESH_ApplicationType app_type;
255               if (me->addrlen == 0)
256                 {
257                   /* This is a mapping to a gnunet-service */
258                   memcpy (hc, &me->desc.service_descriptor,
259                           sizeof (GNUNET_HashCode));
260
261                   if (0x11 == pkt6->ip6_hdr.nxthdr
262                       && (me->desc.
263                           service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP))
264                       && (port_in_ports (me->desc.ports, pkt6_udp->udp_hdr.dpt)
265                           || testBit (me->additional_ports,
266                                       ntohs (pkt6_udp->udp_hdr.dpt))))
267                     {
268                       hdr->type = ntohs (GNUNET_MESSAGE_TYPE_SERVICE_UDP);
269
270                       memcpy (hc + 1, &pkt6_udp->udp_hdr,
271                               ntohs (pkt6_udp->udp_hdr.len));
272
273                     }
274                   else if (0x06 == pkt6->ip6_hdr.nxthdr
275                            && (me->desc.
276                                service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP))
277                            &&
278                            (port_in_ports (me->desc.ports, pkt6_tcp->tcp_hdr.dpt)))
279                     {
280                       hdr->type = ntohs (GNUNET_MESSAGE_TYPE_SERVICE_TCP);
281
282                       memcpy (hc + 1, &pkt6_tcp->tcp_hdr,
283                               ntohs (pkt6->ip6_hdr.paylgth));
284
285                     }
286                   if (me->tunnel == NULL && NULL != cls)
287                     {
288                       *cls =
289                         GNUNET_MESH_peer_request_connect_all (mesh_handle,
290                                                               GNUNET_TIME_UNIT_FOREVER_REL,
291                                                               1,
292                                                               (struct
293                                                                GNUNET_PeerIdentity
294                                                                *) &me->desc.peer,
295                                                               send_pkt_to_peer,
296                                                               NULL, cls);
297                       me->tunnel = *cls;
298                     }
299                   else if (NULL != cls)
300                     {
301                       *cls = me->tunnel;
302                       send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1,
303                                         NULL);
304                       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305                                   "Queued to send to peer %x, type %d\n",
306                                   *((unsigned int *) &me->desc.peer), ntohs(hdr->type));
307                     }
308                 }
309               else
310                 {
311                   /* This is a mapping to a "real" address */
312                   struct remote_addr *s = (struct remote_addr*) hc;
313                   s->addrlen = me->addrlen;
314                   memcpy(s->addr, me->addr, me->addrlen);
315                   s->proto= pkt6->ip6_hdr.nxthdr;
316                   if (s->proto == 0x11)
317                     {
318                       hdr->type = GNUNET_MESSAGE_TYPE_REMOTE_UDP;
319                       memcpy (hc + 1, &pkt6_udp->udp_hdr,
320                               ntohs (pkt6_udp->udp_hdr.len));
321                       app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
322                     }
323                   else if (s->proto == 0x06)
324                     {
325                       hdr->type = GNUNET_MESSAGE_TYPE_REMOTE_TCP;
326                       memcpy (hc + 1, &pkt6_tcp->tcp_hdr,
327                               ntohs (pkt6->ip6_hdr.paylgth));
328                       if (ntohs(pkt6_tcp->tcp_hdr.dpt) == 443)
329                         app_type = GNUNET_APPLICATION_TYPE_INTERNET_HTTPS_GATEWAY;
330                       else if (ntohs(pkt6_tcp->tcp_hdr.dpt) == 80)
331                         app_type = GNUNET_APPLICATION_TYPE_INTERNET_HTTP_GATEWAY;
332                       else
333                         app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
334                     }
335                   if (me->tunnel == NULL && NULL != cls)
336                     {
337                       *cls = GNUNET_MESH_peer_request_connect_by_type(mesh_handle,
338                                                                       GNUNET_TIME_UNIT_FOREVER_REL,
339                                                                       app_type,
340                                                                       send_pkt_to_peer,
341                                                                       NULL,
342                                                                       cls);
343                       me->tunnel = *cls;
344                     }
345                   else if (NULL != cls)
346                     {
347                       *cls = me->tunnel;
348                       send_pkt_to_peer(cls, (struct GNUNET_PeerIdentity*) 1, NULL);
349                     }
350                 }
351             }
352           break;
353         case 0x3a:
354           /* ICMPv6 */
355           pkt6_icmp = (struct ip6_icmp *) pkt6;
356           /* If this packet is an icmp-echo-request and a mapping exists, answer */
357           if (pkt6_icmp->icmp_hdr.type == 0x80
358               && (key = address_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
359             {
360               GNUNET_free (key);
361               pkt6_icmp = GNUNET_malloc (ntohs (pkt6->shdr.size));
362               memcpy (pkt6_icmp, pkt6, ntohs (pkt6->shdr.size));
363               GNUNET_SCHEDULER_add_now (&send_icmp_response, pkt6_icmp);
364             }
365           break;
366         }
367     }
368   /* ethertype is ipv4 */
369   else if (ntohs (pkt_tun->tun.type) == 0x0800)
370     {
371       struct ip_pkt *pkt = (struct ip_pkt *) message;
372       struct ip_udp *udp = (struct ip_udp *) message;
373       GNUNET_assert (pkt->ip_hdr.version == 4);
374
375       /* Send dns-packets to the service-dns */
376       if (pkt->ip_hdr.proto == 0x11 && ntohs (udp->udp_hdr.dpt) == 53)
377         {
378           /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
379           size_t len =
380             sizeof (struct query_packet) + ntohs (udp->udp_hdr.len) - 9;
381
382           struct query_packet_list *query =
383             GNUNET_malloc (len + 2 * sizeof (struct query_packet_list *));
384           query->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS);
385           query->pkt.hdr.size = htons (len);
386           query->pkt.orig_to = pkt->ip_hdr.dadr;
387           query->pkt.orig_from = pkt->ip_hdr.sadr;
388           query->pkt.src_port = udp->udp_hdr.spt;
389           memcpy (query->pkt.data, udp->data, ntohs (udp->udp_hdr.len) - 8);
390
391           GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, query);
392
393           GNUNET_assert (head != NULL);
394
395           if (dns_connection != NULL)
396             GNUNET_CLIENT_notify_transmit_ready (dns_connection,
397                                                  len,
398                                                  GNUNET_TIME_UNIT_FOREVER_REL,
399                                                  GNUNET_YES,
400                                                  &send_query, NULL);
401         }
402     }
403 }
404
405 void write_to_helper(void* buf, size_t len)
406 {
407   (void)GNUNET_DISK_file_write(helper_handle->fh_to_helper, buf, len);
408 }
409
410 void schedule_helper_write(struct GNUNET_TIME_Relative time, void* cls)
411 {
412   GNUNET_SCHEDULER_add_write_file (time, helper_handle->fh_to_helper, &helper_write, cls);
413 }