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