Merge branch 'master' into 1.1
[tinc] / src / route.c
index 2da781e..fd0c697 100644 (file)
@@ -20,7 +20,7 @@
 
 #include "system.h"
 
-#include "avl_tree.h"
+#include "splay_tree.h"
 #include "connection.h"
 #include "ethernet.h"
 #include "ipv4.h"
@@ -49,6 +49,8 @@ static const size_t icmp6_size = sizeof(struct icmp6_hdr);
 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
 static const size_t opt_size = sizeof(struct nd_opt_hdr);
 
+static struct event age_subnets_event;
+
 /* RFC 1071 */
 
 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
@@ -72,6 +74,7 @@ static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
 static bool ratelimit(int frequency) {
        static time_t lasttime = 0;
        static int count = 0;
+       time_t now = time(NULL);
        
        if(lasttime == now) {
                if(++count > frequency)
@@ -99,9 +102,44 @@ static void swap_mac_addresses(vpn_packet_t *packet) {
        memcpy(&packet->data[6], &tmp, sizeof tmp);
 }
        
-static void learn_mac(mac_t *address) {
+static void age_subnets(int fd, short events, void *data) {
+       subnet_t *s;
+       connection_t *c;
+       splay_node_t *node, *next, *node2;
+       bool left = false;
+       time_t now = time(NULL);
+
+       for(node = myself->subnet_tree->head; node; node = next) {
+               next = node->next;
+               s = node->data;
+               if(s->expires && s->expires < now) {
+                       ifdebug(TRAFFIC) {
+                               char netstr[MAXNETSTR];
+                               if(net2str(netstr, sizeof netstr, s))
+                                       logger(LOG_INFO, "Subnet %s expired", netstr);
+                       }
+
+                       for(node2 = connection_tree->head; node2; node2 = node2->next) {
+                               c = node2->data;
+                               if(c->status.active)
+                                       send_del_subnet(c, s);
+                       }
+
+                       subnet_del(myself, s);
+               } else {
+                       if(s->expires)
+                               left = true;
+               }
+       }
+
+       if(left)
+               event_add(&age_subnets_event, &(struct timeval){10, 0});
+}
+
+static void learn_mac(mac_t *address)
+{
        subnet_t *subnet;
-       avl_node_t *node;
+       splay_node_t *node;
        connection_t *c;
 
        subnet = lookup_subnet_mac(address);
@@ -115,7 +153,7 @@ static void learn_mac(mac_t *address) {
 
                subnet = new_subnet();
                subnet->type = SUBNET_MAC;
-               subnet->expires = now + macexpire;
+               subnet->expires = time(NULL) + macexpire;
                subnet->net.mac.address = *address;
                subnet_add(myself, subnet);
 
@@ -126,35 +164,13 @@ static void learn_mac(mac_t *address) {
                        if(c->status.active)
                                send_add_subnet(c, subnet);
                }
-       }
-
-       if(subnet->expires)
-               subnet->expires = now + macexpire;
-}
-
-void age_subnets(void) {
-       subnet_t *s;
-       connection_t *c;
-       avl_node_t *node, *next, *node2;
-
-       for(node = myself->subnet_tree->head; node; node = next) {
-               next = node->next;
-               s = node->data;
-               if(s->expires && s->expires < now) {
-                       ifdebug(TRAFFIC) {
-                               char netstr[MAXNETSTR];
-                               if(net2str(netstr, sizeof netstr, s))
-                                       logger(LOG_INFO, "Subnet %s expired", netstr);
-                       }
 
-                       for(node2 = connection_tree->head; node2; node2 = node2->next) {
-                               c = node2->data;
-                               if(c->status.active)
-                                       send_del_subnet(c, s);
-                       }
-
-                       subnet_del(myself, s);
-               }
+               if(!timeout_initialized(&age_subnets_event))
+                       timeout_set(&age_subnets_event, age_subnets, NULL);
+               event_add(&age_subnets_event, &(struct timeval){10, 0});
+       } else {
+               if(subnet->expires)
+                       subnet->expires = time(NULL) + macexpire;
        }
 }
 
@@ -407,7 +423,7 @@ static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t
 
        /* Generate checksum */
        
-       checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
+       checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
        checksum = inet_checksum(&icmp6, icmp6_size, checksum);
        checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
 
@@ -526,7 +542,7 @@ static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
 
        /* Generate checksum */
 
-       checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
+       checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
        checksum = inet_checksum(&ns, ns_size, checksum);
        if(has_opt) {
                checksum = inet_checksum(&opt, opt_size, checksum);
@@ -589,7 +605,7 @@ static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
 
        /* Generate checksum */
 
-       checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
+       checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
        checksum = inet_checksum(&ns, ns_size, checksum);
        if(has_opt) {
                checksum = inet_checksum(&opt, opt_size, checksum);
@@ -650,7 +666,7 @@ static void route_arp(node_t *source, vpn_packet_t *packet) {
        /* Check if this is a valid ARP request */
 
        if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
-          arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
+          arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof addr || ntohs(arp.arp_op) != ARPOP_REQUEST) {
                ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
                return;
        }
@@ -674,9 +690,9 @@ static void route_arp(node_t *source, vpn_packet_t *packet) {
        memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN);        /* copy destination address */
        packet->data[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(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
-       memcpy(arp.arp_spa, &addr, sizeof(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_spa, &addr, sizeof addr);        /* ... */
 
        memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN);     /* set target hard/proto addr */
        memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */