2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff
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.
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.
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.
22 * @file vpn/gnunet-daemon-vpn.c
24 * @author Philipp Toelke
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 #include "block_dns.h"
47 * The configuration to use
49 static const struct GNUNET_CONFIGURATION_Handle *cfg;
52 * PipeHandle to receive data from the helper
54 static struct GNUNET_DISK_PipeHandle* helper_in;
57 * PipeHandle to send data to the helper
59 static struct GNUNET_DISK_PipeHandle* helper_out;
62 * FileHandle to receive data from the helper
64 static const struct GNUNET_DISK_FileHandle* fh_from_helper;
67 * FileHandle to send data to the helper
69 static const struct GNUNET_DISK_FileHandle* fh_to_helper;
72 * The Message-Tokenizer that tokenizes the messages comming from the helper
74 static struct GNUNET_SERVER_MessageStreamTokenizer* mst;
77 * The connection to the service-dns
79 static struct GNUNET_CLIENT_Connection *dns_connection;
82 * A flag to show that the service-dns has to rehijack the outbound dns-packets
84 * This gets set when the helper restarts as the routing-tables are flushed when
85 * the interface vanishes.
87 static unsigned char restart_hijack;
90 * The process id of the helper
92 static struct GNUNET_OS_Process *helper_proc;
95 * a list of outgoing dns-query-packets
97 static struct query_packet_list *head;
100 * The last element of the list of outgoing dns-query-packets
102 static struct query_packet_list *tail;
105 * A list of processed dns-responses.
107 * "processed" means that the packet is complete and can be sent out via udp
110 static struct answer_packet_list *answer_proc_head;
113 * The last element of the list of processed dns-responses.
115 static struct answer_packet_list *answer_proc_tail;
117 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx);
118 static void dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg);
121 * Callback called by notify_transmit_ready; sends dns-queries or rehijack-messages
125 send_query(void* cls, size_t size, void* buf) {
128 * Send the rehijack-message
130 if (restart_hijack == 1)
134 * The message is just a header
136 GNUNET_assert(sizeof(struct GNUNET_MessageHeader) <= size);
137 struct GNUNET_MessageHeader* hdr = buf;
138 len = sizeof(struct GNUNET_MessageHeader);
139 hdr->size = htons(len);
140 hdr->type = htons(GNUNET_MESSAGE_TYPE_REHIJACK);
144 struct query_packet_list* query = head;
145 len = ntohs(query->pkt.hdr.size);
147 GNUNET_assert(len <= size);
149 memcpy(buf, &query->pkt.hdr, len);
151 GNUNET_CONTAINER_DLL_remove (head, tail, query);
157 * Check whether more data is to be sent
161 GNUNET_CLIENT_notify_transmit_ready(dns_connection, ntohs(head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
163 else if (restart_hijack == 1)
165 GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
172 * Function scheduled as very last function, cleans up after us
175 cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
176 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
178 /* stop the helper */
179 GNUNET_OS_process_kill (helper_proc, SIGTERM);
180 GNUNET_OS_process_wait (helper_proc);
181 GNUNET_OS_process_close (helper_proc);
184 /* close the connection to the service-dns */
185 if (dns_connection != NULL)
187 GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
188 dns_connection = NULL;
193 * Start the helper-process
196 start_helper_and_schedule(void *cls,
197 const struct GNUNET_SCHEDULER_TaskContext *tc) {
198 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
201 helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
202 helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
204 if (helper_in == NULL || helper_out == NULL) return;
206 helper_proc = GNUNET_OS_start_process(helper_in, helper_out, "gnunet-helper-vpn", "gnunet-helper-vpn", NULL);
208 fh_from_helper = GNUNET_DISK_pipe_handle (helper_out, GNUNET_DISK_PIPE_END_READ);
209 fh_to_helper = GNUNET_DISK_pipe_handle (helper_in, GNUNET_DISK_PIPE_END_WRITE);
211 GNUNET_DISK_pipe_close_end(helper_out, GNUNET_DISK_PIPE_END_WRITE);
212 GNUNET_DISK_pipe_close_end(helper_in, GNUNET_DISK_PIPE_END_READ);
214 /* Tell the dns-service to rehijack the dns-port
215 * The routing-table gets flushed if an interface disappears.
218 GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
220 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fh_from_helper, &helper_read, NULL);
224 * Restart the helper-process
227 restart_helper(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
229 GNUNET_OS_process_kill (helper_proc, SIGKILL);
230 GNUNET_OS_process_wait (helper_proc);
231 GNUNET_OS_process_close (helper_proc);
234 GNUNET_DISK_pipe_close(helper_in);
235 GNUNET_DISK_pipe_close(helper_out);
237 /* Restart the helper */
238 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, start_helper_and_schedule, NULL);
242 * Read from the helper-process
245 helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
246 /* no message can be bigger then 64k */
249 if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
252 int t = GNUNET_DISK_file_read(fh_from_helper, &buf, 65535);
254 /* On read-error, restart the helper */
256 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read error for header from vpn-helper: %m\n");
257 GNUNET_SCHEDULER_add_now(restart_helper, cls);
261 /* FIXME */ GNUNET_SERVER_mst_receive(mst, NULL, buf, t, 0, 0);
263 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fh_from_helper, &helper_read, NULL);
267 * Calculate the checksum of an IPv4-Header
270 calculate_ip_checksum(uint16_t* hdr, short len) {
272 for(; len >= 2; len -= 2)
275 sum += *((unsigned char*)hdr);
277 sum = (sum >> 16) + (sum & 0xFFFF);
283 * Send an dns-answer-packet to the helper
286 helper_write(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
287 if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
290 struct answer_packet_list* ans = answer_proc_head;
291 size_t len = ntohs(ans->pkt.hdr.size);
293 GNUNET_assert(ans->pkt.subtype == GNUNET_DNS_ANSWER_TYPE_IP);
295 size_t data_len = len - sizeof(struct answer_packet) + 1;
296 size_t net_len = sizeof(struct ip_hdr) + sizeof(struct udp_dns) + data_len;
297 size_t pkt_len = sizeof(struct GNUNET_MessageHeader) + sizeof(struct pkt_tun) + net_len;
299 struct ip_udp_dns* pkt = alloca(pkt_len);
300 memset(pkt, 0, pkt_len);
302 /* set the gnunet-header */
303 pkt->shdr.size = htons(pkt_len);
304 pkt->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
306 /* set the tun-header (no flags and ethertype of IPv4) */
308 pkt->tun.type = htons(0x0800);
310 /* set the ip-header */
311 pkt->ip_hdr.version = 4;
312 pkt->ip_hdr.hdr_lngth = 5;
313 pkt->ip_hdr.diff_serv = 0;
314 pkt->ip_hdr.tot_lngth = htons(net_len);
315 pkt->ip_hdr.ident = 0;
316 pkt->ip_hdr.flags = 0;
317 pkt->ip_hdr.frag_off = 0;
318 pkt->ip_hdr.ttl = 255;
319 pkt->ip_hdr.proto = 0x11; /* UDP */
320 pkt->ip_hdr.chks = 0; /* Will be calculated later*/
321 pkt->ip_hdr.sadr = ans->pkt.from;
322 pkt->ip_hdr.dadr = ans->pkt.to;
324 pkt->ip_hdr.chks = calculate_ip_checksum((uint16_t*)&pkt->ip_hdr, 5*4);
326 /* set the udp-header */
327 pkt->udp_dns.udp_hdr.spt = htons(53);
328 pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
329 pkt->udp_dns.udp_hdr.len = htons(net_len - sizeof(struct ip_hdr));
330 pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
332 memcpy(&pkt->udp_dns.data, ans->pkt.data, data_len);
334 GNUNET_CONTAINER_DLL_remove (answer_proc_head, answer_proc_tail, ans);
337 /* FIXME */ GNUNET_DISK_file_write(fh_to_helper, pkt, pkt_len);
339 /* if more packets are available, reschedule */
340 if (answer_proc_head != NULL)
341 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
348 address_mapping_exists(unsigned char addr[]) {
353 send_icmp_response(void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
354 struct ip6_icmp* request = cls;
356 struct ip6_icmp* response = alloca(ntohs(request->shdr.size));
357 memset(response, 0, ntohs(request->shdr.size));
359 response->shdr.size = request->shdr.size;
360 response->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
362 response->tun.flags = 0;
363 response->tun.type = htons(0x86dd);
365 response->ip6_hdr.hoplmt = 255;
366 response->ip6_hdr.paylgth = request->ip6_hdr.paylgth;
367 response->ip6_hdr.nxthdr = 0x3a;
368 response->ip6_hdr.version = 6;
369 memcpy(&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16);
370 memcpy(&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16);
372 response->icmp_hdr.code = 0;
373 response->icmp_hdr.type = 0x81;
375 /* Magic, more Magic! */
376 response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1;
378 /* Copy the rest of the packet */
379 memcpy(response+1, request+1, ntohs(request->shdr.size) - sizeof(struct ip6_icmp));
381 /* FIXME */ GNUNET_DISK_file_write(fh_to_helper, response, ntohs(response->shdr.size));
383 GNUNET_free(request);
387 * Receive packets from the helper-process
390 message_token(void *cls,
392 const struct GNUNET_MessageHeader *message) {
393 GNUNET_assert(ntohs(message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
395 struct tun_pkt *pkt_tun = (struct tun_pkt*) message;
397 /* ethertype is ipv6 */
398 if (ntohs(pkt_tun->tun.type) == 0x86dd)
400 struct ip6_pkt *pkt6 = (struct ip6_pkt*) message;
401 GNUNET_assert(pkt6->ip6_hdr.version == 6);
402 struct ip6_tcp *pkt6_tcp;
403 struct ip6_udp *pkt6_udp;
404 struct ip6_icmp *pkt6_icmp;
407 switch(pkt6->ip6_hdr.nxthdr)
410 pkt6_tcp = (struct ip6_tcp*)pkt6;
411 pkt_printf_ip6tcp(pkt6_tcp);
414 pkt6_udp = (struct ip6_udp*)pkt6;
415 pkt_printf_ip6udp(pkt6_udp);
416 if (ntohs(pkt6_udp->udp_hdr.dpt) == 53) {
417 pkt_printf_ip6dns((struct ip6_udp_dns*)pkt6_udp);
422 pkt6_icmp = GNUNET_malloc(ntohs(pkt6->shdr.size));
423 memcpy(pkt6_icmp, pkt6, ntohs(pkt6->shdr.size));
424 /* If this packet is an icmp-echo-request and a mapping exists, answer */
425 if (pkt6_icmp->icmp_hdr.type == 0x80 && address_mapping_exists(pkt6->ip6_hdr.sadr))
426 GNUNET_SCHEDULER_add_now(&send_icmp_response, pkt6_icmp);
430 /* ethertype is ipv4 */
431 else if (ntohs(pkt_tun->tun.type) == 0x0800)
433 struct ip_pkt *pkt = (struct ip_pkt*) message;
434 struct ip_udp *udp = (struct ip_udp*) message;
435 GNUNET_assert(pkt->ip_hdr.version == 4);
437 /* Send dns-packets to the service-dns */
438 if (pkt->ip_hdr.proto == 0x11 && ntohs(udp->udp_hdr.dpt) == 53 )
440 /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
441 size_t len = sizeof(struct query_packet) + ntohs(udp->udp_hdr.len) - 9;
443 struct query_packet_list* query = GNUNET_malloc(len + 2*sizeof(struct query_packet_list*));
444 query->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS);
445 query->pkt.hdr.size = htons(len);
446 query->pkt.orig_to = pkt->ip_hdr.dadr;
447 query->pkt.orig_from = pkt->ip_hdr.sadr;
448 query->pkt.src_port = udp->udp_hdr.spt;
449 memcpy(query->pkt.data, udp->data, ntohs(udp->udp_hdr.len) - 8);
451 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, query);
453 if (dns_connection != NULL)
454 GNUNET_CLIENT_notify_transmit_ready(dns_connection,
456 GNUNET_TIME_UNIT_FOREVER_REL,
465 * Connect to the service-dns
468 connect_to_service_dns (void *cls,
469 const struct GNUNET_SCHEDULER_TaskContext *tc) {
470 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
472 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting to service-dns\n");
473 GNUNET_assert (dns_connection == NULL);
474 dns_connection = GNUNET_CLIENT_connect ("dns", cfg);
475 GNUNET_CLIENT_receive(dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
477 /* If a packet is already in the list, schedule to send it */
479 GNUNET_CLIENT_notify_transmit_ready(dns_connection,
480 ntohs(head->pkt.hdr.size),
481 GNUNET_TIME_UNIT_FOREVER_REL,
485 else if (restart_hijack == 1)
487 GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
492 * This gets scheduled with cls pointing to an answer_packet and does everything
493 * needed in order to send it to the helper.
495 * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
496 * doing nothing for "real" services.
499 process_answer(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
500 struct answer_packet* pkt = cls;
501 struct answer_packet_list* list;
503 /* This answer is about a .gnunet-service
505 * It contains an almost complete DNS-Response, we have to fill in the ip
506 * at the offset pkt->addroffset
509 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
511 unsigned char ip6addr[16];
513 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
514 memcpy(ip6addr, (int[]){htons(0x1234)}, 2);
515 memcpy(ip6addr+2, &pkt->peer, 7);
516 memcpy(ip6addr+9, &pkt->service_descriptor, 7);
518 memcpy(((char*)pkt)+ntohs(pkt->addroffset), ip6addr, 16);
521 * -save DNS_Record into hashmap, pointed to by ip
522 * -regularily walk through hashmap, deleting old entries
523 * when is an entry old?
524 * have a last-used field
525 * don't remove if last-used "recent", ask dht again if record expired
528 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
530 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
532 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
534 unsigned short offset = ntohs(pkt->addroffset);
536 unsigned short namelen = htons(22); /* calculate the length of the answer */
537 char name[22] = {13, 'p', 'h', 'i', 'l', 'i', 'p', 'p', 't', 'o', 'e', 'l', 'k', 'e', 6, 'g', 'n', 'u', 'n', 'e', 't', 0};
539 list = GNUNET_malloc(2*sizeof(struct answer_packet_list*) + offset + 2 + ntohs(namelen));
541 struct answer_packet* rpkt = &list->pkt;
543 memcpy(rpkt, pkt, offset);
545 rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
546 rpkt->hdr.size = ntohs(offset + 2 + ntohs(namelen));
548 memcpy(((char*)rpkt)+offset, &namelen, 2);
549 memcpy(((char*)rpkt)+offset+2, name, ntohs(namelen));
552 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
554 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
555 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
566 GNUNET_CONTAINER_DLL_insert_after(answer_proc_head, answer_proc_tail, answer_proc_tail, list);
568 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, fh_to_helper, &helper_write, NULL);
574 * This receives packets from the service-dns and schedules process_answer to
578 dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg) {
579 /* the service disconnected, reconnect after short wait */
582 GNUNET_CLIENT_disconnect(dns_connection, GNUNET_NO);
583 dns_connection = NULL;
584 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
585 &connect_to_service_dns,
590 /* the service did something strange, reconnect immediately */
591 if (msg->type != htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS))
594 GNUNET_CLIENT_disconnect(dns_connection, GNUNET_NO);
595 dns_connection = NULL;
596 GNUNET_SCHEDULER_add_now (&connect_to_service_dns,
600 void *pkt = GNUNET_malloc(ntohs(msg->size));
602 memcpy(pkt, msg, ntohs(msg->size));
604 GNUNET_SCHEDULER_add_now(process_answer, pkt);
605 GNUNET_CLIENT_receive(dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
609 * Main function that will be run by the scheduler.
612 * @param args remaining command-line arguments
613 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
614 * @param cfg configuration
620 const struct GNUNET_CONFIGURATION_Handle *cfg_) {
621 mst = GNUNET_SERVER_mst_create(&message_token, NULL);
624 GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
625 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
626 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
630 * The main function to obtain template from gnunetd.
632 * @param argc number of arguments from the command line
633 * @param argv command line arguments
634 * @return 0 ok, 1 on error
637 main (int argc, char *const *argv) {
638 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
639 GNUNET_GETOPT_OPTION_END
643 GNUNET_PROGRAM_run (argc,
646 gettext_noop ("help text"),
647 options, &run, NULL)) ? ret : 1;
650 /* end of gnunet-daemon-vpn.c */