Restart the hijack, if the tun was down
[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         unsigned char restart_hijack;
57
58         pid_t helper_pid;
59
60         const struct GNUNET_CONFIGURATION_Handle *cfg;
61
62         struct query_packet_list *head;
63         struct query_packet_list *tail;
64
65         struct answer_packet_list *answer_proc_head;
66         struct answer_packet_list *answer_proc_tail;
67 };
68
69 static struct vpn_cls mycls;
70
71 size_t send_query(void* cls, size_t size, void* buf);
72
73 static void cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
74   GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
75   PLIBC_KILL(mycls.helper_pid, SIGTERM);
76   GNUNET_OS_process_wait(mycls.helper_pid);
77   if (mycls.dns_connection != NULL)
78     {
79       GNUNET_CLIENT_disconnect (mycls.dns_connection, GNUNET_NO);
80       mycls.dns_connection = NULL;
81     }
82 }
83
84 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx);
85
86 static void start_helper_and_schedule(void *cls,
87                                       const struct GNUNET_SCHEDULER_TaskContext *tc) {
88
89         if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
90           return;
91
92         mycls.helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
93         mycls.helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
94
95         if (mycls.helper_in == NULL || mycls.helper_out == NULL) return;
96
97         mycls.helper_pid = GNUNET_OS_start_process(mycls.helper_in, mycls.helper_out, "gnunet-helper-vpn", "gnunet-helper-vpn", NULL);
98
99         mycls.fh_from_helper = GNUNET_DISK_pipe_handle (mycls.helper_out, GNUNET_DISK_PIPE_END_READ);
100         mycls.fh_to_helper = GNUNET_DISK_pipe_handle (mycls.helper_in, GNUNET_DISK_PIPE_END_WRITE);
101
102         GNUNET_DISK_pipe_close_end(mycls.helper_out, GNUNET_DISK_PIPE_END_WRITE);
103         GNUNET_DISK_pipe_close_end(mycls.helper_in, GNUNET_DISK_PIPE_END_READ);
104
105         GNUNET_SCHEDULER_add_read_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_from_helper, &helper_read, NULL);
106 }
107
108
109 static void restart_helper(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
110         // Kill the helper
111         PLIBC_KILL(mycls.helper_pid, SIGKILL);
112         GNUNET_OS_process_wait(mycls.helper_pid);
113
114         /* Tell the dns-service to rehijack the dns-port
115          * The routing-table gets flushed if an interface disappears.
116          */
117         mycls.restart_hijack = 1;
118         GNUNET_CLIENT_notify_transmit_ready(mycls.dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
119
120         GNUNET_DISK_pipe_close(mycls.helper_in);
121         GNUNET_DISK_pipe_close(mycls.helper_out);
122
123         // Restart the helper
124         GNUNET_SCHEDULER_add_delayed (mycls.sched, GNUNET_TIME_UNIT_SECONDS, start_helper_and_schedule, NULL);
125
126 }
127
128 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
129         char buf[65535];
130
131         if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
132                 return;
133
134         int t = GNUNET_DISK_file_read(mycls.fh_from_helper, &buf, 65535);
135         if (t<=0) {
136                 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read error for header from vpn-helper: %m\n");
137                 GNUNET_SCHEDULER_add_now(mycls.sched, restart_helper, cls);
138                 return;
139         }
140
141         /* FIXME */ GNUNET_SERVER_mst_receive(mycls.mst, NULL, buf, t, 0, 0);
142
143         GNUNET_SCHEDULER_add_read_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_from_helper, &helper_read, NULL);
144 }
145
146 static uint16_t calculate_ip_checksum(uint16_t* hdr, short len) {
147         uint32_t sum = 0;
148         for(; len >= 2; len -= 2)
149                 sum += *(hdr++);
150         if (len == 1)
151                 sum += *((unsigned char*)hdr);
152
153         sum = (sum >> 16) + (sum & 0xFFFF);
154
155         return ~sum;
156 }
157
158 static void helper_write(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
159         if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
160                 return;
161         struct answer_packet_list* ans = mycls.answer_proc_head;
162         size_t len = ntohs(ans->pkt.hdr.size);
163
164         GNUNET_assert(ans->pkt.subtype == GNUNET_DNS_ANSWER_TYPE_IP);
165
166         size_t data_len = len - sizeof(struct answer_packet) + 1;
167         size_t net_len = sizeof(struct ip_hdr) + sizeof(struct udp_dns) + data_len;
168         size_t pkt_len = sizeof(struct GNUNET_MessageHeader) + sizeof(struct pkt_tun) + net_len;
169
170         struct ip_udp_dns* pkt = alloca(pkt_len);
171         memset(pkt, 0, pkt_len);
172
173         pkt->shdr.size = htons(pkt_len);
174         pkt->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
175
176         pkt->tun.flags = 0;
177         pkt->tun.type = htons(0x0800);
178
179         pkt->ip_hdr.version = 4;
180         pkt->ip_hdr.hdr_lngth = 5;
181         pkt->ip_hdr.diff_serv = 0;
182         pkt->ip_hdr.tot_lngth = htons(net_len);
183         pkt->ip_hdr.ident = 0;
184         pkt->ip_hdr.flags = 0;
185         pkt->ip_hdr.frag_off = 0;
186         pkt->ip_hdr.ttl = 255;
187         pkt->ip_hdr.proto = 0x11; /* UDP */
188         pkt->ip_hdr.chks = 0; /* Will be calculated later*/
189         pkt->ip_hdr.sadr = ans->pkt.from;
190         pkt->ip_hdr.dadr = ans->pkt.to;
191
192         pkt->ip_hdr.chks = calculate_ip_checksum((uint16_t*)&pkt->ip_hdr, 5*4);
193
194         pkt->udp_dns.udp_hdr.spt = htons(53);
195         pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
196         pkt->udp_dns.udp_hdr.len = htons(net_len - sizeof(struct ip_hdr));
197         pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
198
199         memcpy(&pkt->udp_dns.data, ans->pkt.data, data_len);
200         
201         GNUNET_CONTAINER_DLL_remove (mycls.answer_proc_head, mycls.answer_proc_tail, ans);
202         GNUNET_free(ans);
203
204         /* FIXME */ GNUNET_DISK_file_write(mycls.fh_to_helper, pkt, pkt_len);
205
206         if (mycls.answer_proc_head != NULL)
207                 GNUNET_SCHEDULER_add_write_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_to_helper, &helper_write, NULL);
208 }
209
210 size_t send_query(void* cls, size_t size, void* buf)
211 {
212   size_t len;
213   if (mycls.restart_hijack == 1)
214     {
215       mycls.restart_hijack = 0;
216       GNUNET_assert(sizeof(struct GNUNET_MessageHeader) >= size);
217       struct GNUNET_MessageHeader* hdr = buf;
218       len = sizeof(struct GNUNET_MessageHeader);
219       hdr->size = htons(len);
220       hdr->type = htons(GNUNET_MESSAGE_TYPE_REHIJACK);
221     }
222   else
223     {
224         struct query_packet_list* query = mycls.head;
225         len = ntohs(query->pkt.hdr.size);
226
227         GNUNET_assert(len <= size);
228
229         memcpy(buf, &query->pkt.hdr, len);
230
231         GNUNET_CONTAINER_DLL_remove (mycls.head, mycls.tail, query);
232
233         GNUNET_free(query);
234     }
235
236         if (mycls.head != NULL || mycls.restart_hijack == 1) {
237                 GNUNET_CLIENT_notify_transmit_ready(mycls.dns_connection, ntohs(mycls.head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
238         }
239
240         return len;
241 }
242
243 static void message_token(void *cls, void *client, const struct GNUNET_MessageHeader *message) {
244         if (ntohs(message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) return;
245
246         struct tun_pkt *pkt_tun = (struct tun_pkt*) message;
247
248         if (ntohs(pkt_tun->tun.type) == 0x86dd) {
249                 struct ip6_pkt *pkt6 = (struct ip6_pkt*) message;
250                 struct ip6_tcp *pkt6_tcp;
251                 struct ip6_udp *pkt6_udp;
252
253                 pkt_printf(pkt6);
254                 switch(pkt6->ip6_hdr.nxthdr) {
255                         case 0x06:
256                                 pkt6_tcp = (struct ip6_tcp*)pkt6;
257                                 pkt_printf_ip6tcp(pkt6_tcp);
258                                 break;
259                         case 0x11:
260                                 pkt6_udp = (struct ip6_udp*)pkt6;
261                                 pkt_printf_ip6udp(pkt6_udp);
262                                 if (ntohs(pkt6_udp->udp_hdr.dpt) == 53) {
263                                         pkt_printf_ip6dns((struct ip6_udp_dns*)pkt6_udp);
264                                 }
265                                 break;
266                 }
267         } else if (ntohs(pkt_tun->tun.type) == 0x0800) {
268                 struct ip_pkt *pkt = (struct ip_pkt*) message;
269                 struct ip_udp *udp = (struct ip_udp*) message;
270                 GNUNET_assert(pkt->ip_hdr.version == 4);
271                 if (pkt->ip_hdr.proto == 0x11 && ntohs(udp->udp_hdr.dpt) == 53 ) {
272                         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]; */
273                         struct query_packet_list* query = GNUNET_malloc(len + 2*sizeof(struct query_packet_list*));
274                         query->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS);
275                         query->pkt.hdr.size = htons(len);
276                         query->pkt.orig_to = pkt->ip_hdr.dadr;
277                         query->pkt.orig_from = pkt->ip_hdr.sadr;
278                         query->pkt.src_port = udp->udp_hdr.spt;
279                         memcpy(query->pkt.data, udp->data, ntohs(udp->udp_hdr.len) - 8);
280
281                         GNUNET_CONTAINER_DLL_insert_after(mycls.head, mycls.tail, mycls.tail, query);
282
283                         if (mycls.dns_connection != NULL)
284                           /* struct GNUNET_CLIENT_TransmitHandle* th = */ GNUNET_CLIENT_notify_transmit_ready(mycls.dns_connection, len, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
285                 }
286         }
287
288 }
289
290 static void 
291 dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg);
292
293 static void 
294 reconnect_to_service_dns (void *cls,
295                           const struct GNUNET_SCHEDULER_TaskContext *tc) {
296   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
297     return;
298   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting\n");
299   GNUNET_assert (mycls.dns_connection == NULL);
300   mycls.dns_connection = GNUNET_CLIENT_connect (mycls.sched, "dns", mycls.cfg); 
301   GNUNET_CLIENT_receive(mycls.dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
302   if (mycls.head != NULL)
303     /* 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);
304 }
305
306 static void
307 process_answer(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
308     struct answer_packet* pkt = cls;
309
310     if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
311       {
312         struct answer_packet_list* list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
313
314         memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
315
316         GNUNET_CONTAINER_DLL_insert_after(mycls.answer_proc_head, mycls.answer_proc_tail, mycls.answer_proc_tail, list);
317
318         GNUNET_SCHEDULER_add_write_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_to_helper, &helper_write, NULL);
319
320         return;
321       }
322
323     if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
324       {
325         /*FIXME:
326          * -find new IP-address for this service
327          * -create a DNS-Answer
328          */
329       }
330 }
331
332 static void
333 dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg)
334 {
335   if (msg == NULL)
336     {
337       GNUNET_CLIENT_disconnect(mycls.dns_connection, GNUNET_NO);
338       mycls.dns_connection = NULL;
339       GNUNET_SCHEDULER_add_delayed (mycls.sched,
340                                     GNUNET_TIME_UNIT_SECONDS,
341                                     &reconnect_to_service_dns,
342                                     NULL);
343       return;
344     }
345
346   if (msg->type != htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS)) 
347     {
348       GNUNET_break (0);
349       GNUNET_CLIENT_disconnect(mycls.dns_connection, GNUNET_NO);
350       mycls.dns_connection = NULL;
351       GNUNET_SCHEDULER_add_now (mycls.sched,
352                                 &reconnect_to_service_dns,
353                                 NULL);
354       return;
355     }
356   void *pkt = GNUNET_malloc(ntohs(msg->size));
357
358   memcpy(pkt, msg, ntohs(msg->size));
359
360   GNUNET_SCHEDULER_add_now(mycls.sched, process_answer, pkt);
361   GNUNET_CLIENT_receive(mycls.dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
362 }
363
364 /**
365  * Main function that will be run by the scheduler.
366  *
367  * @param cls closure
368  * @param sched the scheduler to use
369  * @param args remaining command-line arguments
370  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
371  * @param cfg configuration
372  */
373 static void
374 run (void *cls,
375      struct GNUNET_SCHEDULER_Handle *sched,
376      char *const *args,
377      const char *cfgfile,
378      const struct GNUNET_CONFIGURATION_Handle *cfg) 
379 {
380   mycls.sched = sched;
381   mycls.mst = GNUNET_SERVER_mst_create(&message_token, NULL);
382   mycls.cfg = cfg;
383   mycls.restart_hijack = 0;
384   GNUNET_SCHEDULER_add_now (sched, &reconnect_to_service_dns, NULL);
385   GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls); 
386   GNUNET_SCHEDULER_add_now (sched, start_helper_and_schedule, NULL);
387 }
388
389
390 /**
391  * The main function to obtain template from gnunetd.
392  *
393  * @param argc number of arguments from the command line
394  * @param argv command line arguments
395  * @return 0 ok, 1 on error
396  */
397 int
398 main (int argc, char *const *argv)
399 {
400   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
401     GNUNET_GETOPT_OPTION_END
402   };
403
404   return (GNUNET_OK ==
405           GNUNET_PROGRAM_run (argc,
406                               argv,
407                               "gnunet-daemon-vpn",
408                               gettext_noop ("help text"),
409                               options, &run, NULL)) ? ret : 1;
410 }
411
412 /* end of gnunet-daemon-vpn.c */