allow the service-dns to answer not only with an unchanged dns-packet, but
[oweals/gnunet.git] / src / vpn / gnunet-daemon-vpn.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.c
23  * @brief 
24  * @author Philipp Tölke
25  */
26 #include "platform.h"
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet-vpn-helper-p.h"
31 #include "gnunet-vpn-packet.h"
32 #include "gnunet-vpn-pretty-print.h"
33 #include "gnunet_common.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet-service-dns-p.h"
37 #include "gnunet_client_lib.h"
38 #include "gnunet_container_lib.h"
39
40 /**
41  * Final status code.
42  */
43 static int ret;
44
45 struct vpn_cls {
46         struct GNUNET_DISK_PipeHandle* helper_in; // From the helper
47         struct GNUNET_DISK_PipeHandle* helper_out; // To the helper
48         const struct GNUNET_DISK_FileHandle* fh_from_helper;
49         const struct GNUNET_DISK_FileHandle* fh_to_helper;
50
51         struct GNUNET_SERVER_MessageStreamTokenizer* mst;
52
53         struct GNUNET_SCHEDULER_Handle *sched;
54
55         struct GNUNET_CLIENT_Connection *dns_connection;
56
57         pid_t helper_pid;
58
59         const struct GNUNET_CONFIGURATION_Handle *cfg;
60
61         struct query_packet_list *head;
62         struct query_packet_list *tail;
63
64         struct answer_packet_list *answer_head;
65         struct answer_packet_list *answer_tail;
66 };
67
68 static struct vpn_cls mycls;
69
70 static void cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
71   GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
72   PLIBC_KILL(mycls.helper_pid, SIGTERM);
73   GNUNET_OS_process_wait(mycls.helper_pid);
74   if (mycls.dns_connection != NULL)
75     {
76       GNUNET_CLIENT_disconnect (mycls.dns_connection, GNUNET_NO);
77       mycls.dns_connection = NULL;
78     }
79 }
80
81 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx);
82
83 static void start_helper_and_schedule() {
84         mycls.helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);;
85         mycls.helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
86
87         if (mycls.helper_in == NULL || mycls.helper_out == NULL) return;
88
89         mycls.helper_pid = GNUNET_OS_start_process(mycls.helper_in, mycls.helper_out, "gnunet-helper-vpn", "gnunet-helper-vpn", NULL);
90
91         mycls.fh_from_helper = GNUNET_DISK_pipe_handle (mycls.helper_out, GNUNET_DISK_PIPE_END_READ);
92         mycls.fh_to_helper = GNUNET_DISK_pipe_handle (mycls.helper_in, GNUNET_DISK_PIPE_END_WRITE);
93
94         GNUNET_DISK_pipe_close_end(mycls.helper_out, GNUNET_DISK_PIPE_END_WRITE);
95         GNUNET_DISK_pipe_close_end(mycls.helper_in, GNUNET_DISK_PIPE_END_READ);
96
97         GNUNET_SCHEDULER_add_read_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_from_helper, &helper_read, NULL);
98 }
99
100
101 static void restart_helper(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
102         // FIXME: Ratelimit this!
103
104         // Kill the helper
105         PLIBC_KILL(mycls.helper_pid, SIGKILL);
106         GNUNET_OS_process_wait(mycls.helper_pid);
107
108         // FIXME: send msg to service-dns -- the hijacker has to be started again, too, the routing table is flushed if it depends on one interface
109
110         GNUNET_DISK_pipe_close(mycls.helper_in);
111         GNUNET_DISK_pipe_close(mycls.helper_out);
112
113         // Restart the helper
114         start_helper_and_schedule(mycls);
115
116 }
117
118 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
119         char buf[65535];
120
121         if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
122                 return;
123
124         int t = GNUNET_DISK_file_read(mycls.fh_from_helper, &buf, 65535);
125         if (t<=0) {
126                 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read error for header from vpn-helper: %m\n");
127                 GNUNET_SCHEDULER_add_now(mycls.sched, restart_helper, cls);
128                 return;
129         }
130
131         /* FIXME */ GNUNET_SERVER_mst_receive(mycls.mst, NULL, buf, t, 0, 0);
132
133         GNUNET_SCHEDULER_add_read_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_from_helper, &helper_read, NULL);
134 }
135
136 static uint16_t calculate_ip_checksum(uint16_t* hdr, short len) {
137         uint32_t sum = 0;
138         for(; len >= 2; len -= 2)
139                 sum += *(hdr++);
140         if (len == 1)
141                 sum += *((unsigned char*)hdr);
142
143         sum = (sum >> 16) + (sum & 0xFFFF);
144
145         return ~sum;
146 }
147
148 static void helper_write(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
149         if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
150                 return;
151         struct answer_packet_list* ans = mycls.answer_head;
152         size_t len = ntohs(ans->pkt.hdr.size);
153
154         GNUNET_assert(ans->pkt.subtype == GNUNET_DNS_ANSWER_TYPE_IP);
155
156         size_t data_len = len - sizeof(struct answer_packet) + 1;
157         size_t net_len = sizeof(struct ip_hdr) + sizeof(struct udp_dns) + data_len;
158         size_t pkt_len = sizeof(struct GNUNET_MessageHeader) + sizeof(struct pkt_tun) + net_len;
159
160         struct ip_udp_dns* pkt = alloca(pkt_len);
161         memset(pkt, 0, pkt_len);
162
163         pkt->shdr.size = htons(pkt_len);
164         pkt->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
165
166         pkt->tun.flags = 0;
167         pkt->tun.type = htons(0x0800);
168
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         pkt->udp_dns.udp_hdr.spt = htons(53);
185         pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
186         pkt->udp_dns.udp_hdr.len = htons(net_len - sizeof(struct ip_hdr));
187         pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
188
189         memcpy(&pkt->udp_dns.data, ans->pkt.data, data_len);
190         
191         GNUNET_CONTAINER_DLL_remove (mycls.answer_head, mycls.answer_tail, ans);
192         GNUNET_free(ans);
193
194         /* FIXME */ GNUNET_DISK_file_write(mycls.fh_to_helper, pkt, pkt_len);
195
196         if (mycls.answer_head != NULL)
197                 GNUNET_SCHEDULER_add_write_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_to_helper, &helper_write, NULL);
198 }
199
200 size_t send_query(void* cls, size_t size, void* buf)
201 {
202         struct query_packet_list* query = mycls.head;
203         size_t len = ntohs(query->pkt.hdr.size);
204
205         GNUNET_assert(len <= size);
206
207         memcpy(buf, &query->pkt.hdr, len);
208
209         GNUNET_CONTAINER_DLL_remove (mycls.head, mycls.tail, query);
210
211         GNUNET_free(query);
212
213         if (mycls.head != NULL) {
214                 GNUNET_CLIENT_notify_transmit_ready(mycls.dns_connection, ntohs(mycls.head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
215         }
216
217         return len;
218 }
219
220 static void message_token(void *cls, void *client, const struct GNUNET_MessageHeader *message) {
221         if (ntohs(message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) return;
222
223         struct tun_pkt *pkt_tun = (struct tun_pkt*) message;
224
225         if (ntohs(pkt_tun->tun.type) == 0x86dd) {
226                 struct ip6_pkt *pkt6 = (struct ip6_pkt*) message;
227                 struct ip6_tcp *pkt6_tcp;
228                 struct ip6_udp *pkt6_udp;
229
230                 pkt_printf(pkt6);
231                 switch(pkt6->ip6_hdr.nxthdr) {
232                         case 0x06:
233                                 pkt6_tcp = (struct ip6_tcp*)pkt6;
234                                 pkt_printf_ip6tcp(pkt6_tcp);
235                                 break;
236                         case 0x11:
237                                 pkt6_udp = (struct ip6_udp*)pkt6;
238                                 pkt_printf_ip6udp(pkt6_udp);
239                                 if (ntohs(pkt6_udp->udp_hdr.dpt) == 53) {
240                                         pkt_printf_ip6dns((struct ip6_udp_dns*)pkt6_udp);
241                                 }
242                                 break;
243                 }
244         } else if (ntohs(pkt_tun->tun.type) == 0x0800) {
245                 struct ip_pkt *pkt = (struct ip_pkt*) message;
246                 struct ip_udp *udp = (struct ip_udp*) message;
247                 GNUNET_assert(pkt->ip_hdr.version == 4);
248                 if (pkt->ip_hdr.proto == 0x11 && ntohs(udp->udp_hdr.dpt) == 53 ) {
249                         size_t len = sizeof(struct query_packet) + ntohs(udp->udp_hdr.len) - 9; /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
250                         struct query_packet_list* query = GNUNET_malloc(len + 2*sizeof(struct query_packet_list*));
251                         query->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS);
252                         query->pkt.hdr.size = htons(len);
253                         query->pkt.orig_to = pkt->ip_hdr.dadr;
254                         query->pkt.orig_from = pkt->ip_hdr.sadr;
255                         query->pkt.src_port = udp->udp_hdr.spt;
256                         memcpy(query->pkt.data, udp->data, ntohs(udp->udp_hdr.len) - 8);
257
258                         GNUNET_CONTAINER_DLL_insert_after(mycls.head, mycls.tail, mycls.tail, query);
259
260                         if (mycls.dns_connection != NULL)
261                           /* struct GNUNET_CLIENT_TransmitHandle* th = */ GNUNET_CLIENT_notify_transmit_ready(mycls.dns_connection, len, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
262                 }
263         }
264
265 }
266
267 static void 
268 dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg);
269
270 static void 
271 reconnect_to_service_dns (void *cls,
272                           const struct GNUNET_SCHEDULER_TaskContext *tc) {
273   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
274     return;
275   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting\n");
276   GNUNET_assert (mycls.dns_connection == NULL);
277   mycls.dns_connection = GNUNET_CLIENT_connect (mycls.sched, "dns", mycls.cfg); 
278   GNUNET_CLIENT_receive(mycls.dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
279   if (mycls.head != NULL)
280     /* struct GNUNET_CLIENT_TransmitHandle* th = */ GNUNET_CLIENT_notify_transmit_ready(mycls.dns_connection, ntohs(mycls.head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
281 }
282
283 static void 
284 dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg) 
285 {
286   if (msg == NULL) 
287     {
288       GNUNET_CLIENT_disconnect(mycls.dns_connection, GNUNET_NO);
289       mycls.dns_connection = NULL;
290       GNUNET_SCHEDULER_add_delayed (mycls.sched,
291                                     GNUNET_TIME_UNIT_SECONDS,
292                                     &reconnect_to_service_dns,
293                                     NULL);
294       return;
295     }
296
297   if (msg->type != htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS)) 
298     {
299       GNUNET_break (0);
300       GNUNET_CLIENT_disconnect(mycls.dns_connection, GNUNET_NO);
301       mycls.dns_connection = NULL;
302       GNUNET_SCHEDULER_add_now (mycls.sched,
303                                 &reconnect_to_service_dns,
304                                 NULL);
305       return;
306     }  
307   struct answer_packet_list* pkt = GNUNET_malloc(ntohs(msg->size) + 2*sizeof(struct answer_packet_list*));
308   
309   memcpy(&pkt->pkt, msg, ntohs(msg->size));
310   GNUNET_CONTAINER_DLL_insert_after(mycls.answer_head, mycls.answer_tail, mycls.answer_tail, pkt);  
311   GNUNET_SCHEDULER_add_write_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_to_helper, &helper_write, NULL);
312   GNUNET_CLIENT_receive(mycls.dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
313 }
314
315 /**
316  * Main function that will be run by the scheduler.
317  *
318  * @param cls closure
319  * @param sched the scheduler to use
320  * @param args remaining command-line arguments
321  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
322  * @param cfg configuration
323  */
324 static void
325 run (void *cls,
326      struct GNUNET_SCHEDULER_Handle *sched,
327      char *const *args,
328      const char *cfgfile,
329      const struct GNUNET_CONFIGURATION_Handle *cfg) 
330 {
331   mycls.sched = sched;
332   mycls.mst = GNUNET_SERVER_mst_create(&message_token, NULL);
333   mycls.cfg = cfg;
334   GNUNET_SCHEDULER_add_now (sched, &reconnect_to_service_dns, NULL);
335   GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls); 
336   start_helper_and_schedule(mycls);
337 }
338
339
340 /**
341  * The main function to obtain template from gnunetd.
342  *
343  * @param argc number of arguments from the command line
344  * @param argv command line arguments
345  * @return 0 ok, 1 on error
346  */
347 int
348 main (int argc, char *const *argv)
349 {
350   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
351     GNUNET_GETOPT_OPTION_END
352   };
353
354   return (GNUNET_OK ==
355           GNUNET_PROGRAM_run (argc,
356                               argv,
357                               "gnunet-daemon-vpn",
358                               gettext_noop ("help text"),
359                               options, &run, NULL)) ? ret : 1;
360 }
361
362 /* end of gnunet-daemon-vpn.c */