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