Merge remote-tracking branch 'remotes/guus/1.1' into thkr-1.1-ponyhof

This commit is contained in:
thorkill 2015-11-07 23:21:18 +01:00
commit dcf313cdbf
3 changed files with 24 additions and 25 deletions

View file

@ -1255,8 +1255,12 @@ void send_packet(node_t *n, vpn_packet_t *packet) {
// If it's for myself, write it to the tun/tap device. // If it's for myself, write it to the tun/tap device.
if(n == myself) { if(n == myself) {
if(overwrite_mac) if(overwrite_mac) {
memcpy(DATA(packet), mymac.x, ETH_ALEN); memcpy(DATA(packet), mymac.x, ETH_ALEN);
// Use an arbitrary fake source address.
memcpy(DATA(packet) + ETH_ALEN, DATA(packet), ETH_ALEN);
DATA(packet)[ETH_ALEN * 2 - 1] ^= 0xFF;
}
n->out_packets++; n->out_packets++;
n->out_bytes += packet->len; n->out_bytes += packet->len;
devops.write(packet); devops.write(packet);

View file

@ -34,8 +34,7 @@
splay_tree_t *node_tree; splay_tree_t *node_tree;
static splay_tree_t *node_id_tree; static splay_tree_t *node_id_tree;
static hash_t *node_udp_cache; static splay_tree_t *node_udp_tree;
static hash_t *node_id_cache;
node_t *myself; node_t *myself;
@ -47,16 +46,21 @@ static int node_id_compare(const node_t *a, const node_t *b) {
return memcmp(&a->id, &b->id, sizeof(node_id_t)); return memcmp(&a->id, &b->id, sizeof(node_id_t));
} }
static int node_udp_compare(const node_t *a, const node_t *b) {
int result = sockaddrcmp(&a->address, &b->address);
if (result)
return result;
return (a->name && b->name) ? strcmp(a->name, b->name) : 0;
}
void init_nodes(void) { void init_nodes(void) {
node_tree = splay_alloc_tree((splay_compare_t) node_compare, (splay_action_t) free_node); node_tree = splay_alloc_tree((splay_compare_t) node_compare, (splay_action_t) free_node);
node_id_tree = splay_alloc_tree((splay_compare_t) node_id_compare, NULL); node_id_tree = splay_alloc_tree((splay_compare_t) node_id_compare, NULL);
node_udp_cache = hash_alloc(0x100, sizeof(sockaddr_t)); node_udp_tree = splay_alloc_tree((splay_compare_t) node_udp_compare, NULL);
node_id_cache = hash_alloc(0x100, sizeof(node_id_t));
} }
void exit_nodes(void) { void exit_nodes(void) {
hash_free(node_id_cache); splay_delete_tree(node_udp_tree);
hash_free(node_udp_cache);
splay_delete_tree(node_id_tree); splay_delete_tree(node_id_tree);
splay_delete_tree(node_tree); splay_delete_tree(node_tree);
} }
@ -116,8 +120,7 @@ void node_add(node_t *n) {
} }
void node_del(node_t *n) { void node_del(node_t *n) {
hash_delete(node_udp_cache, &n->address); splay_delete(node_udp_tree, n);
hash_delete(node_id_cache, &n->id);
for splay_each(subnet_t, s, n->subnet_tree) for splay_each(subnet_t, s, n->subnet_tree)
subnet_del(n, s); subnet_del(n, s);
@ -139,19 +142,13 @@ node_t *lookup_node(char *name) {
} }
node_t *lookup_node_id(const node_id_t *id) { node_t *lookup_node_id(const node_id_t *id) {
node_t *n = hash_search(node_id_cache, id); node_t n = {.id = *id};
if(!n) { return splay_search(node_id_tree, &n);
node_t tmp = {.id = *id};
n = splay_search(node_id_tree, &tmp);
if(n)
hash_insert(node_id_cache, id, n);
}
return n;
} }
node_t *lookup_node_udp(const sockaddr_t *sa) { node_t *lookup_node_udp(const sockaddr_t *sa) {
return hash_search(node_udp_cache, sa); node_t tmp = {.address = *sa};
return splay_search(node_udp_tree, &tmp);
} }
void update_node_udp(node_t *n, const sockaddr_t *sa) { void update_node_udp(node_t *n, const sockaddr_t *sa) {
@ -160,7 +157,7 @@ void update_node_udp(node_t *n, const sockaddr_t *sa) {
return; return;
} }
hash_delete(node_udp_cache, &n->address); splay_delete(node_udp_tree, n);
if(sa) { if(sa) {
n->address = *sa; n->address = *sa;
@ -171,7 +168,7 @@ void update_node_udp(node_t *n, const sockaddr_t *sa) {
break; break;
} }
} }
hash_insert(node_udp_cache, sa, n); splay_insert(node_udp_tree, n);
free(n->hostname); free(n->hostname);
n->hostname = sockaddr2hostname(&n->address); n->hostname = sockaddr2hostname(&n->address);
logger(DEBUG_PROTOCOL, LOG_DEBUG, "UDP address of %s set to %s", n->name, n->hostname); logger(DEBUG_PROTOCOL, LOG_DEBUG, "UDP address of %s set to %s", n->name, n->hostname);

View file

@ -837,15 +837,13 @@ static void route_arp(node_t *source, vpn_packet_t *packet) {
if(subnet->owner == myself) if(subnet->owner == myself)
return; /* silently ignore */ return; /* silently ignore */
memcpy(DATA(packet), DATA(packet) + ETH_ALEN, ETH_ALEN); /* copy destination address */
DATA(packet)[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
memcpy(&addr, arp.arp_tpa, sizeof addr); /* save protocol addr */ memcpy(&addr, arp.arp_tpa, sizeof addr); /* save protocol addr */
memcpy(arp.arp_tpa, arp.arp_spa, sizeof addr); /* swap destination and source protocol address */ memcpy(arp.arp_tpa, arp.arp_spa, sizeof addr); /* swap destination and source protocol address */
memcpy(arp.arp_spa, &addr, sizeof addr); /* ... */ memcpy(arp.arp_spa, &addr, sizeof addr); /* ... */
memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */ memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
memcpy(arp.arp_sha, DATA(packet) + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */ memcpy(arp.arp_sha, DATA(packet) + ETH_ALEN, ETH_ALEN); /* set source hard/proto addr */
arp.arp_sha[ETH_ALEN - 1] ^= 0xFF; /* for consistency with route_packet() */
arp.arp_op = htons(ARPOP_REPLY); arp.arp_op = htons(ARPOP_REPLY);
/* Copy structs on stack back to packet */ /* Copy structs on stack back to packet */