+ ifdebug(TRAFFIC) logger(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 = now + macexpire;
+ subnet->net.mac.address = *address;
+ subnet->weight = 10;
+ subnet_add(myself, subnet);
+ subnet_update(myself, subnet, true);
+
+ /* And tell all other tinc daemons it's our MAC */
+
+ for(node = connection_tree->head; node; node = node->next) {
+ c = node->data;
+
+ 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_update(myself, s, false);
+ subnet_del(myself, s);
+ }
+ }
+}
+
+static void route_broadcast(node_t *source, vpn_packet_t *packet) {
+ if(decrement_ttl && source != myself)
+ if(!do_decrement_ttl(source, packet)) {
+ return;
+ }
+
+ broadcast_packet(source, packet);
+}
+
+/* RFC 791 */
+
+static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t ether_size) {
+ struct ip ip;
+ vpn_packet_t fragment;
+ int len, maxlen, todo;
+ uint8_t *offset;
+ uint16_t ip_off, origf;
+
+ memcpy(&ip, packet->data + ether_size, ip_size);
+ fragment.priority = packet->priority;
+
+ if(ip.ip_hl != ip_size / 4) {
+ return;
+ }
+
+ todo = ntohs(ip.ip_len) - ip_size;
+
+ if(ether_size + ip_size + todo != packet->len) {
+ ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%d)", packet->len, (int)(ether_size + ip_size + todo));
+ return;
+ }
+
+ ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
+
+ offset = packet->data + ether_size + ip_size;
+ maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
+ ip_off = ntohs(ip.ip_off);
+ origf = ip_off & ~IP_OFFMASK;
+ ip_off &= IP_OFFMASK;
+
+ while(todo) {
+ len = todo > maxlen ? maxlen : todo;
+ memcpy(fragment.data + ether_size + ip_size, offset, len);
+ todo -= len;
+ offset += len;
+
+ ip.ip_len = htons(ip_size + len);
+ ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
+ ip.ip_sum = 0;
+ ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
+ memcpy(fragment.data, packet->data, ether_size);
+ memcpy(fragment.data + ether_size, &ip, ip_size);
+ fragment.len = ether_size + ip_size + len;
+
+ send_packet(dest, &fragment);
+
+ ip_off += len / 8;
+ }
+}
+
+static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
+ subnet_t *subnet;
+ node_t *via;
+ ipv4_t dest;
+
+ memcpy(&dest, &packet->data[30], sizeof(dest));
+ subnet = lookup_subnet_ipv4(&dest);
+
+ if(!subnet) {
+ ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
+ source->name, source->hostname,
+ dest.x[0],
+ dest.x[1],
+ dest.x[2],
+ dest.x[3]);
+
+ route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
+ return;
+ }
+
+ if(subnet->owner == source) {
+ ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
+ return;
+ }
+
+ if(!subnet->owner->status.reachable) {
+ return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
+ }
+
+ if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
+ return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
+ }
+
+ if(decrement_ttl && source != myself && subnet->owner != myself)
+ if(!do_decrement_ttl(source, packet)) {
+ return;
+ }
+
+ if(priorityinheritance) {
+ packet->priority = packet->data[15];
+ }
+
+ via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
+
+ if(via == source) {
+ ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
+ return;
+ }
+
+ if(directonly && subnet->owner != via) {
+ return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
+ }
+
+ if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
+ ifdebug(TRAFFIC) logger(LOG_INFO, "Packet for %s (%s) length %d larger than MTU %d", subnet->owner->name, subnet->owner->hostname, packet->len, via->mtu);
+
+ if(packet->data[20] & 0x40) {
+ packet->len = MAX(via->mtu, 590);
+ route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
+ } else {
+ fragment_ipv4_packet(via, packet, ether_size);
+ }
+
+ return;
+ }
+
+ clamp_mss(source, via, 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))) {
+ route_broadcast(source, packet);
+ } else {
+ route_ipv4_unicast(source, packet);
+ }
+}
+
+static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
+ subnet_t *subnet;
+ node_t *via;
+ ipv6_t dest;
+
+ memcpy(&dest, &packet->data[38], sizeof(dest));
+ subnet = lookup_subnet_ipv6(&dest);