authentication of ciphertexts (+ seed)
[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         struct query_packet_list *head;
60         struct query_packet_list *tail;
61
62         struct answer_packet_list *answer_head;
63         struct answer_packet_list *answer_tail;
64 };
65
66 static struct vpn_cls mycls;
67
68 static void cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
69         if (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) {
70                 PLIBC_KILL(mycls.helper_pid, SIGTERM);
71                 GNUNET_OS_process_wait(mycls.helper_pid);
72         }
73 }
74
75 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx);
76
77 static void start_helper_and_schedule() {
78         mycls.helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);;
79         mycls.helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
80
81         if (mycls.helper_in == NULL || mycls.helper_out == NULL) return;
82
83         mycls.helper_pid = GNUNET_OS_start_process(mycls.helper_in, mycls.helper_out, "gnunet-helper-vpn", "gnunet-helper-vpn", NULL);
84
85         mycls.fh_from_helper = GNUNET_DISK_pipe_handle (mycls.helper_out, GNUNET_DISK_PIPE_END_READ);
86         mycls.fh_to_helper = GNUNET_DISK_pipe_handle (mycls.helper_in, GNUNET_DISK_PIPE_END_WRITE);
87
88         GNUNET_DISK_pipe_close_end(mycls.helper_out, GNUNET_DISK_PIPE_END_WRITE);
89         GNUNET_DISK_pipe_close_end(mycls.helper_in, GNUNET_DISK_PIPE_END_READ);
90
91         GNUNET_SCHEDULER_add_read_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_from_helper, &helper_read, NULL);
92 }
93
94
95 static void restart_helper(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
96         // FIXME: Ratelimit this!
97
98         // Kill the helper
99         PLIBC_KILL(mycls.helper_pid, SIGKILL);
100         GNUNET_OS_process_wait(mycls.helper_pid);
101
102         // 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
103
104         GNUNET_DISK_pipe_close(mycls.helper_in);
105         GNUNET_DISK_pipe_close(mycls.helper_out);
106
107         // Restart the helper
108         start_helper_and_schedule(mycls);
109
110 }
111
112 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
113         char buf[65535];
114
115         if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
116                 return;
117
118         int t = GNUNET_DISK_file_read(mycls.fh_from_helper, &buf, 65535);
119         if (t<=0) {
120                 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Read error for header: %m\n");
121                 GNUNET_SCHEDULER_add_now(mycls.sched, restart_helper, cls);
122                 return;
123         }
124
125         /* FIXME */ GNUNET_SERVER_mst_receive(mycls.mst, NULL, buf, t, 0, 0);
126
127         GNUNET_SCHEDULER_add_read_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_from_helper, &helper_read, NULL);
128 }
129
130 static uint16_t calculate_ip_checksum(uint16_t* hdr, short len) {
131         uint32_t sum = 0;
132         for(; len >= 2; len -= 2)
133                 sum += *(hdr++);
134         if (len == 1)
135                 sum += *((unsigned char*)hdr);
136
137         sum = (sum >> 16) + (sum & 0xFFFF);
138
139         return ~sum;
140 }
141
142 static void helper_write(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
143         if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
144                 return;
145         struct answer_packet_list* ans = mycls.answer_head;
146         size_t len = ntohs(ans->pkt.hdr.size);
147
148         size_t data_len = len - sizeof(struct answer_packet) + 1;
149         size_t net_len = sizeof(struct ip_hdr) + sizeof(struct udp_dns) + data_len;
150         size_t pkt_len = sizeof(struct GNUNET_MessageHeader) + sizeof(struct pkt_tun) + net_len;
151
152         struct ip_udp_dns* pkt = alloca(pkt_len);
153
154         pkt->shdr.size = htons(pkt_len);
155         pkt->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
156
157         pkt->tun.flags = 0;
158         pkt->tun.type = htons(0x0800);
159
160         pkt->ip_hdr.version = 4;
161         pkt->ip_hdr.hdr_lngth = 5;
162         pkt->ip_hdr.diff_serv = 0;
163         pkt->ip_hdr.tot_lngth = htons(net_len);
164         pkt->ip_hdr.ident = 0;
165         pkt->ip_hdr.flags = 0;
166         pkt->ip_hdr.frag_off = 0;
167         pkt->ip_hdr.ttl = 255;
168         pkt->ip_hdr.proto = 0x11; /* UDP */
169         pkt->ip_hdr.chks = 0; /* Will be calculated later*/
170         pkt->ip_hdr.sadr = ans->pkt.from;
171         pkt->ip_hdr.dadr = ans->pkt.to;
172
173         pkt->ip_hdr.chks = calculate_ip_checksum((uint16_t*)&pkt->ip_hdr, 5*4);
174
175         pkt->udp_dns.udp_hdr.spt = htons(53);
176         pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
177         pkt->udp_dns.udp_hdr.len = htons(net_len - sizeof(struct ip_hdr));
178         pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
179
180         memcpy(&pkt->udp_dns.data, ans->pkt.data, data_len);
181
182         /* GNUNET_MessageHeader
183          * pkt_tun
184          * ip_hdr
185          * udp_dns
186          *     udp_pkt
187               !!data!!
188          */
189         
190         GNUNET_CONTAINER_DLL_remove (mycls.answer_head, mycls.answer_tail, ans);
191         GNUNET_free(ans);
192
193         /* FIXME */ GNUNET_DISK_file_write(mycls.fh_to_helper, pkt, pkt_len);
194
195         if (mycls.answer_head != NULL)
196                 GNUNET_SCHEDULER_add_write_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_to_helper, &helper_write, NULL);
197 }
198
199 size_t send_query(void* cls, size_t size, void* buf)
200 {
201         struct query_packet_list* query = mycls.head;
202         size_t len = ntohs(query->pkt.hdr.size);
203
204         GNUNET_assert(len <= size);
205
206         memcpy(buf, &query->pkt.hdr, len);
207
208         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sent %d bytes.\n", len);
209
210         GNUNET_CONTAINER_DLL_remove (mycls.head, mycls.tail, query);
211
212         GNUNET_free(query);
213
214         if (mycls.head != NULL) {
215                 GNUNET_CLIENT_notify_transmit_ready(mycls.dns_connection, ntohs(mycls.head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
216         }
217
218         return len;
219 }
220
221 static void message_token(void *cls, void *client, const struct GNUNET_MessageHeader *message) {
222         if (ntohs(message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) return;
223
224         struct tun_pkt *pkt_tun = (struct tun_pkt*) message;
225
226         if (ntohs(pkt_tun->tun.type) == 0x86dd) {
227                 struct ip6_pkt *pkt6 = (struct ip6_pkt*) message;
228                 struct ip6_tcp *pkt6_tcp;
229                 struct ip6_udp *pkt6_udp;
230
231                 pkt_printf(pkt6);
232                 switch(pkt6->ip6_hdr.nxthdr) {
233                         case 0x06:
234                                 pkt6_tcp = (struct ip6_tcp*)pkt6;
235                                 pkt_printf_ip6tcp(pkt6_tcp);
236                                 break;
237                         case 0x11:
238                                 pkt6_udp = (struct ip6_udp*)pkt6;
239                                 pkt_printf_ip6udp(pkt6_udp);
240                                 if (ntohs(pkt6_udp->udp_hdr.dpt) == 53) {
241                                         pkt_printf_ip6dns((struct ip6_udp_dns*)pkt6_udp);
242                                 }
243                                 break;
244                 }
245         } else if (ntohs(pkt_tun->tun.type) == 0x0800) {
246                 struct ip_pkt *pkt = (struct ip_pkt*) message;
247                 struct ip_udp *udp = (struct ip_udp*) message;
248                 GNUNET_assert(pkt->ip_hdr.version == 4);
249                 if (pkt->ip_hdr.proto == 0x11 && ntohs(udp->udp_hdr.dpt) == 53 ) {
250                         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]; */
251                         struct query_packet_list* query = GNUNET_malloc(len + 2*sizeof(struct query_packet_list*));
252                         query->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS);
253                         query->pkt.hdr.size = htons(len);
254                         query->pkt.orig_to = pkt->ip_hdr.dadr;
255                         query->pkt.orig_from = pkt->ip_hdr.sadr;
256                         query->pkt.src_port = udp->udp_hdr.spt;
257                         memcpy(query->pkt.data, udp->data, ntohs(udp->udp_hdr.len) - 8);
258
259                         GNUNET_CONTAINER_DLL_insert_after(mycls.head, mycls.tail, mycls.tail, query);
260
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                         if (th != NULL)
263                                 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Queued sending of %d bytes.\n", len);
264                         else
265                                 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Already queued for %d bytes.\n", len);
266                 }
267         }
268
269 }
270
271 void dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg) {
272         if (msg == NULL) return;
273         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got an answer!\n");
274
275         if (msg->type != htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS)) goto out;
276
277         struct answer_packet_list* pkt = GNUNET_malloc(ntohs(msg->size) + 2*sizeof(struct answer_packet_list*));
278
279         memcpy(&pkt->pkt, msg, ntohs(msg->size));
280
281         GNUNET_CONTAINER_DLL_insert_after(mycls.answer_head, mycls.answer_tail, mycls.answer_tail, pkt);
282
283         GNUNET_SCHEDULER_add_write_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_to_helper, &helper_write, NULL);
284
285 out:
286         GNUNET_CLIENT_receive(mycls.dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
287 }
288
289 /**
290  * Main function that will be run by the scheduler.
291  *
292  * @param cls closure
293  * @param sched the scheduler to use
294  * @param args remaining command-line arguments
295  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
296  * @param cfg configuration
297  */
298 static void
299 run (void *cls,
300      struct GNUNET_SCHEDULER_Handle *sched,
301      char *const *args,
302      const char *cfgfile,
303      const struct GNUNET_CONFIGURATION_Handle *cfg) 
304 {
305   mycls.sched = sched;
306   mycls.mst = GNUNET_SERVER_mst_create(&message_token, NULL);
307
308   mycls.dns_connection = GNUNET_CLIENT_connect (sched, "dns", cfg);
309   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connection: %x\n", mycls.dns_connection);
310
311   GNUNET_CLIENT_receive(mycls.dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
312
313   GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls); 
314   start_helper_and_schedule(mycls);
315 }
316
317
318 /**
319  * The main function to obtain template from gnunetd.
320  *
321  * @param argc number of arguments from the command line
322  * @param argv command line arguments
323  * @return 0 ok, 1 on error
324  */
325 int
326 main (int argc, char *const *argv)
327 {
328   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
329     GNUNET_GETOPT_OPTION_END
330   };
331
332   return (GNUNET_OK ==
333           GNUNET_PROGRAM_run (argc,
334                               argv,
335                               "gnunet-daemon-vpn",
336                               gettext_noop ("help text"),
337                               options, &run, NULL)) ? ret : 1;
338 }
339
340 /* end of gnunet-daemon-vpn.c */