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