Fix incorrect format qualifiers.
[tinc] / src / route.c
index 46867d9..8c65c4c 100644 (file)
@@ -1,7 +1,7 @@
 /*
     route.c -- routing
     Copyright (C) 2000-2005 Ivo Timmermans,
-                  2000-2012 Guus Sliepen <guus@tinc-vpn.org>
+                  2000-2013 Guus Sliepen <guus@tinc-vpn.org>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -59,7 +59,7 @@ static const size_t opt_size = sizeof(struct nd_opt_hdr);
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
 #endif
 
-static struct event age_subnets_event;
+static timeout_t age_subnets_timeout;
 
 /* RFC 1071 */
 
@@ -84,13 +84,12 @@ 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(lasttime == now.tv_sec) {
                if(count >= frequency)
                        return true;
        } else {
-               lasttime = now;
+               lasttime = now.tv_sec;
                count = 0;
        }
 
@@ -192,12 +191,11 @@ static void swap_mac_addresses(vpn_packet_t *packet) {
        memcpy(&packet->data[6], &tmp, sizeof tmp);
 }
 
-static void age_subnets(int fd, short events, void *data) {
+static void age_subnets(void *data) {
        bool left = false;
-       time_t now = time(NULL);
 
        for splay_each(subnet_t, s, myself->subnet_tree) {
-               if(s->expires && s->expires < now) {
+               if(s->expires && s->expires < now.tv_sec) {
                        if(debug_level >= DEBUG_TRAFFIC) {
                                char netstr[MAXNETSTR];
                                if(net2str(netstr, sizeof netstr, s))
@@ -216,7 +214,7 @@ static void age_subnets(int fd, short events, void *data) {
        }
 
        if(left)
-               event_add(&age_subnets_event, &(struct timeval){10, rand() % 100000});
+               timeout_set(&age_subnets_timeout, &(struct timeval){10, rand() % 100000});
 }
 
 static void learn_mac(mac_t *address) {
@@ -225,13 +223,13 @@ static void learn_mac(mac_t *address) {
        /* If we don't know this MAC address yet, store it */
 
        if(!subnet) {
-               logger(DEBUG_TRAFFIC, LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
+               logger(DEBUG_TRAFFIC, LOG_INFO, "Learned new MAC address %x:%x:%x:%x:%x:%x",
                                   address->x[0], address->x[1], address->x[2], address->x[3],
                                   address->x[4], address->x[5]);
 
                subnet = new_subnet();
                subnet->type = SUBNET_MAC;
-               subnet->expires = time(NULL) + macexpire;
+               subnet->expires = now.tv_sec + macexpire;
                subnet->net.mac.address = *address;
                subnet->weight = 10;
                subnet_add(myself, subnet);
@@ -243,12 +241,10 @@ static void learn_mac(mac_t *address) {
                        if(c->status.active)
                                send_add_subnet(c, subnet);
 
-               if(!timeout_initialized(&age_subnets_event))
-                       timeout_set(&age_subnets_event, age_subnets, NULL);
-               event_add(&age_subnets_event, &(struct timeval){10, rand() % 100000});
+               timeout_add(&age_subnets_timeout, age_subnets, NULL, &(struct timeval){10, rand() % 100000});
        } else {
                if(subnet->expires)
-                       subnet->expires = time(NULL) + macexpire;
+                       subnet->expires = now.tv_sec + macexpire;
        }
 }
 
@@ -375,7 +371,10 @@ static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t et
        }
 }
 
-static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
+static void route_ipv4(node_t *source, vpn_packet_t *packet) {
+       if(!checklength(source, packet, ether_size + ip_size))
+               return;
+
        subnet_t *subnet;
        node_t *via;
        ipv4_t dest;
@@ -395,6 +394,11 @@ static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
                return;
        }
 
+       if (!subnet->owner) {
+               broadcast_packet(source, packet);
+               return;
+       }
+
        if(subnet->owner == source) {
                logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
                return;
@@ -436,20 +440,6 @@ static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
        send_packet(subnet->owner, packet);
 }
 
-static void route_ipv4(node_t *source, vpn_packet_t *packet) {
-       if(!checklength(source, packet, ether_size + ip_size))
-               return;
-
-       if(broadcast_mode && (((packet->data[30] & 0xf0) == 0xe0) || (
-                       packet->data[30] == 255 &&
-                       packet->data[31] == 255 &&
-                       packet->data[32] == 255 &&
-                       packet->data[33] == 255)))
-               broadcast_packet(source, packet);
-       else
-               route_ipv4_unicast(source, packet);
-}
-
 /* RFC 2463 */
 
 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
@@ -530,7 +520,17 @@ static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, length_
        send_packet(source, packet);
 }
 
-static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
+static void route_neighborsol(node_t *source, vpn_packet_t *packet);
+
+static void route_ipv6(node_t *source, vpn_packet_t *packet) {
+       if(!checklength(source, packet, ether_size + ip6_size))
+               return;
+
+       if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
+               route_neighborsol(source, packet);
+               return;
+       }
+
        subnet_t *subnet;
        node_t *via;
        ipv6_t dest;
@@ -554,6 +554,11 @@ static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
                return;
        }
 
+       if (!subnet->owner) {
+               broadcast_packet(source, packet);
+               return;
+       }
+
        if(subnet->owner == source) {
                logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
                return;
@@ -728,21 +733,6 @@ static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
        send_packet(source, packet);
 }
 
-static void route_ipv6(node_t *source, vpn_packet_t *packet) {
-       if(!checklength(source, packet, ether_size + ip6_size))
-               return;
-
-       if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
-               route_neighborsol(source, packet);
-               return;
-       }
-
-       if(broadcast_mode && packet->data[38] == 255)
-               broadcast_packet(source, packet);
-       else
-               route_ipv6_unicast(source, packet);
-}
-
 /* RFC 826 */
 
 static void route_arp(node_t *source, vpn_packet_t *packet) {
@@ -826,7 +816,7 @@ static void route_mac(node_t *source, vpn_packet_t *packet) {
        memcpy(&dest, &packet->data[0], sizeof dest);
        subnet = lookup_subnet_mac(NULL, &dest);
 
-       if(!subnet) {
+       if(!subnet || !subnet->owner) {
                broadcast_packet(source, packet);
                return;
        }
@@ -839,6 +829,11 @@ static void route_mac(node_t *source, vpn_packet_t *packet) {
        if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
                return;
 
+       uint16_t type = packet->data[12] << 8 | packet->data[13];
+
+       if(priorityinheritance && type == ETH_P_IP && packet->len >= ether_size + ip_size)
+               packet->priority = packet->data[15];
+
        // Handle packets larger than PMTU
 
        node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
@@ -848,7 +843,6 @@ static void route_mac(node_t *source, vpn_packet_t *packet) {
 
        if(via && packet->len > via->mtu && via != myself) {
                logger(DEBUG_TRAFFIC, LOG_INFO, "Packet for %s (%s) length %d larger than MTU %d", subnet->owner->name, subnet->owner->hostname, packet->len, via->mtu);
-               uint16_t type = packet->data[12] << 8 | packet->data[13];
                length_t ethlen = 14;
 
                if(type == ETH_P_8021Q) {