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-vpn-packet.h"
30 #include "gnunet-vpn-pretty-print.h"
31 #include "gnunet_common.h"
32 #include "gnunet_protocols.h"
33 #include <gnunet_mesh_service.h>
34 #include "gnunet_client_lib.h"
35 #include "gnunet_container_lib.h"
36 #include "gnunet_constants.h"
37 #include <block_dns.h>
38 #include "gnunet-daemon-vpn-helper.h"
39 #include "gnunet-daemon-vpn-dns.h"
41 #include "gnunet-daemon-vpn.h"
49 * This hashmap contains the mapping from peer, service-descriptor,
50 * source-port and destination-port to a socket
52 static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
55 * Function scheduled as very last function, cleans up after us
59 cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
60 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
63 cleanup_helper(helper_handle);
65 /* close the connection to the service-dns */
66 if (dns_connection != NULL)
68 GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
69 dns_connection = NULL;
72 if (mesh_handle != NULL)
74 GNUNET_MESH_disconnect(mesh_handle);
80 static uint32_t calculate_checksum_update(uint32_t sum, uint16_t *hdr, short len) {
81 for(; len >= 2; len -= 2)
84 sum += *((unsigned char*)hdr);
88 static uint16_t calculate_checksum_end(uint32_t sum) {
90 sum = (sum >> 16) + (sum & 0xFFFF);
96 * Calculate the checksum of an IPv4-Header
99 calculate_ip_checksum(uint16_t* hdr, short len) {
100 uint32_t sum = calculate_checksum_update(0, hdr, len);
101 return calculate_checksum_end(sum);
105 * @return the hash of the IP-Address if a mapping exists, NULL otherwise
108 address_mapping_exists(unsigned char addr[]) {
109 GNUNET_HashCode* key = GNUNET_malloc(sizeof(GNUNET_HashCode));
110 unsigned char* k = (unsigned char*)key;
111 memset(key, 0, sizeof(GNUNET_HashCode));
113 for (i = 0; i < 16; i++)
116 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(hashmap, key))
126 send_icmp_response(void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
127 struct ip6_icmp* request = cls;
129 struct ip6_icmp* response = alloca(ntohs(request->shdr.size));
130 GNUNET_assert(response != NULL);
131 memset(response, 0, ntohs(request->shdr.size));
133 response->shdr.size = request->shdr.size;
134 response->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
136 response->tun.flags = 0;
137 response->tun.type = htons(0x86dd);
139 response->ip6_hdr.hoplmt = 255;
140 response->ip6_hdr.paylgth = request->ip6_hdr.paylgth;
141 response->ip6_hdr.nxthdr = 0x3a;
142 response->ip6_hdr.version = 6;
143 memcpy(&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16);
144 memcpy(&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16);
146 response->icmp_hdr.code = 0;
147 response->icmp_hdr.type = 0x81;
149 /* Magic, more Magic! */
150 response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1;
152 /* Copy the rest of the packet */
153 memcpy(response+1, request+1, ntohs(request->shdr.size) - sizeof(struct ip6_icmp));
155 write_to_helper(response, ntohs(response->shdr.size));
157 GNUNET_free(request);
161 * cls is the pointer to a GNUNET_MessageHeader that is
162 * followed by the service-descriptor and the udp-packet that should be sent;
165 send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
167 struct GNUNET_MESH_Tunnel **tunnel = cls;
168 struct GNUNET_MessageHeader *hdr =
169 (struct GNUNET_MessageHeader *) (tunnel + 1);
170 GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
171 struct udp_pkt *udp = (struct udp_pkt *) (hc + 1);
172 hdr->size = htons (sizeof (struct GNUNET_MessageHeader) +
173 sizeof (GNUNET_HashCode) + ntohs (udp->len));
174 hdr->type = ntohs (GNUNET_MESSAGE_TYPE_SERVICE_UDP);
175 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "send_udp_to_peer_notify_callback: buf = %x; size = %u;\n", buf, size);
176 GNUNET_assert (size >= ntohs (hdr->size));
177 memcpy (buf, hdr, ntohs (hdr->size));
178 size = ntohs(hdr->size);
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
185 port_in_ports (uint64_t ports, uint16_t port)
187 uint16_t *ps = (uint16_t *) & ports;
188 return ps[0] == port || ps[1] == port || ps[2] == port || ps[3] == port;
192 send_udp_to_peer (void *cls,
193 const struct GNUNET_PeerIdentity *peer,
194 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
196 if (peer == NULL) return;
197 struct GNUNET_MESH_Tunnel **tunnel = cls;
198 struct GNUNET_MessageHeader *hdr =
199 (struct GNUNET_MessageHeader *) (tunnel + 1);
200 GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
201 struct udp_pkt *udp = (struct udp_pkt *) (hc + 1);
202 GNUNET_MESH_notify_transmit_ready (*tunnel,
205 GNUNET_TIME_relative_divide(GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
207 (struct GNUNET_MessageHeader) +
208 sizeof (GNUNET_HashCode) +
210 send_udp_to_peer_notify_callback,
215 * Create a new Address from an answer-packet
218 new_ip6addr(unsigned char* buf, const GNUNET_HashCode *peer, const GNUNET_HashCode *service_desc) { /* {{{ */
220 unsigned long long ipv6prefix;
221 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
222 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
223 GNUNET_assert(ipv6prefix < 127);
224 ipv6prefix = (ipv6prefix + 7)/8;
226 inet_pton (AF_INET6, ipv6addr, buf);
227 GNUNET_free(ipv6addr);
229 int peer_length = 16 - ipv6prefix - 6;
230 if (peer_length <= 0)
233 int service_length = 16 - ipv6prefix - peer_length;
234 if (service_length <= 0)
237 memcpy(buf+ipv6prefix, service_desc, service_length);
238 memcpy(buf+ipv6prefix+service_length, peer, peer_length);
243 * This gets scheduled with cls pointing to an answer_packet and does everything
244 * needed in order to send it to the helper.
246 * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
247 * doing nothing for "real" services.
250 process_answer(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
251 struct answer_packet* pkt = cls;
252 struct answer_packet_list* list;
254 /* This answer is about a .gnunet-service
256 * It contains an almost complete DNS-Response, we have to fill in the ip
257 * at the offset pkt->addroffset
260 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
262 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
265 memset(&key, 0, sizeof(GNUNET_HashCode));
267 unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset);
268 unsigned char* k = (unsigned char*)&key;
269 new_ip6addr(c, &pkt->service_descr.peer, &pkt->service_descr.service_descriptor);
271 * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
274 for (i = 0; i < 16; i++)
277 uint16_t namelen = strlen((char*)pkt->data+12)+1;
279 struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
280 char* name = (char*)(value +1);
282 value->namelen = namelen;
283 memcpy(name, pkt->data+12, namelen);
285 memcpy(&value->desc, &pkt->service_descr, sizeof(struct GNUNET_vpn_service_descriptor));
287 value->additional_ports = 0;
289 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(hashmap,
292 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
294 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not store to hashmap\n");
298 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
300 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
303 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
306 memset(&key, 0, sizeof key);
307 unsigned char* k = (unsigned char*)&key;
308 unsigned char* s = pkt->data+12;
310 /* Whoever designed the reverse IPv6-lookup is batshit insane */
311 for (i = 0; i < 16; i++)
313 unsigned char c1 = s[(4*i)+1];
314 unsigned char c2 = s[(4*i)+3];
318 k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */
320 k[i] += 16*(c2 - '0');
322 k[i] += 16*(c2 - 87);
325 struct map_entry* map_entry = GNUNET_CONTAINER_multihashmap_get(hashmap, &key);
326 uint16_t offset = ntohs(pkt->addroffset);
328 if (map_entry == NULL)
334 unsigned short namelen = htons(map_entry->namelen);
335 char* name = (char*)(map_entry + 1);
337 list = GNUNET_malloc(2*sizeof(struct answer_packet_list*) + offset + 2 + ntohs(namelen));
339 struct answer_packet* rpkt = &list->pkt;
341 /* The offset points to the first byte belonging to the address */
342 memcpy(rpkt, pkt, offset - 1);
344 rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
345 rpkt->hdr.size = ntohs(offset + 2 + ntohs(namelen));
347 memcpy(((char*)rpkt)+offset, &namelen, 2);
348 memcpy(((char*)rpkt)+offset+2, name, ntohs(namelen));
351 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
353 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
354 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
365 GNUNET_CONTAINER_DLL_insert_after(answer_proc_head, answer_proc_tail, answer_proc_tail, list);
367 schedule_helper_write(GNUNET_TIME_UNIT_FOREVER_REL, NULL);
373 add_additional_port (struct map_entry *me, uint16_t port)
375 uint16_t *ps = (uint16_t *) & me->additional_ports;
377 for (i = 0; i < 4; i++)
388 receive_udp_back (void *cls, struct GNUNET_MESH_Tunnel* tunnel,
390 const struct GNUNET_MessageHeader *message,
391 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
393 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
394 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
395 const struct GNUNET_PeerIdentity* other = GNUNET_MESH_get_peer(tunnel);
397 size_t size = sizeof(struct ip6_udp) + ntohs(pkt->len) - 1 - sizeof(struct udp_pkt);
399 struct ip6_udp* pkt6 = alloca(size);
401 GNUNET_assert(pkt6 != NULL);
403 new_ip6addr(pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
405 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Relaying calc:%d gnu:%d udp:%d bytes!\n", size, ntohs(message->size), ntohs(pkt->len));
407 pkt6->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
408 pkt6->shdr.size = htons(size);
411 pkt6->tun.type = htons(0x86dd);
413 pkt6->ip6_hdr.version = 6;
414 pkt6->ip6_hdr.tclass_h = 0;
415 pkt6->ip6_hdr.tclass_l = 0;
416 pkt6->ip6_hdr.flowlbl = 0;
417 pkt6->ip6_hdr.paylgth = pkt->len;
418 pkt6->ip6_hdr.nxthdr = 0x11;
419 pkt6->ip6_hdr.hoplmt = 0xff;
423 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
424 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
425 GNUNET_free(ipv6addr);
427 memcpy(&pkt6->udp_hdr, pkt, ntohs(pkt->len));
429 GNUNET_HashCode* key = address_mapping_exists(pkt6->ip6_hdr.sadr);
430 GNUNET_assert (key != NULL);
432 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get(hashmap, key);
436 GNUNET_assert (me != NULL);
437 GNUNET_assert (me->desc.service_type & htonl(GNUNET_DNS_SERVICE_TYPE_UDP));
438 if (!port_in_ports(me->desc.ports, pkt6->udp_hdr.spt) ||
439 !port_in_ports(me->additional_ports, pkt6->udp_hdr.spt)) {
440 add_additional_port(me, pkt6->udp_hdr.spt);
443 pkt6->udp_hdr.crc = 0;
445 sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->ip6_hdr.sadr, 16);
446 sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->ip6_hdr.dadr, 16);
447 uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
448 sum = calculate_checksum_update(sum, (uint16_t*)&tmp, 4);
449 tmp = htons(((pkt6->ip6_hdr.nxthdr & 0x00ff)));
450 sum = calculate_checksum_update(sum, (uint16_t*)&tmp, 4);
452 sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->udp_hdr, ntohs(pkt->len));
453 pkt6->udp_hdr.crc = calculate_checksum_end(sum);
455 write_to_helper(pkt6, size);
460 void init_mesh (void* cls, struct GNUNET_MESH_Handle* server, const struct GNUNET_PeerIdentity* my_identity, const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey) {
461 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to MESH, I am %x\n", *((unsigned long*)my_identity));
464 void connect_mesh (void* cls, const struct GNUNET_PeerIdentity* peer, const struct GNUNET_TRANSPORT_ATS_Information *atsi) {
465 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %x\n", *((unsigned long*)peer));
469 * Main function that will be run by the scheduler.
472 * @param args remaining command-line arguments
473 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
474 * @param cfg_ configuration
480 const struct GNUNET_CONFIGURATION_Handle *cfg_)
482 const static struct GNUNET_MESH_MessageHandler handlers[] = {
483 {receive_udp_back, GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK, 0},
486 mesh_handle = GNUNET_MESH_connect(cfg_,
492 hashmap = GNUNET_CONTAINER_multihashmap_create(65536);
493 udp_connections = GNUNET_CONTAINER_multihashmap_create(65536);
494 GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
495 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
496 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
500 * The main function to obtain template from gnunetd.
502 * @param argc number of arguments from the command line
503 * @param argv command line arguments
504 * @return 0 ok, 1 on error
507 main (int argc, char *const *argv) {
508 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
509 GNUNET_GETOPT_OPTION_END
513 GNUNET_PROGRAM_run (argc,
516 gettext_noop ("help text"),
517 options, &run, NULL)) ? ret : 1;
520 /* end of gnunet-daemon-vpn.c */