3 Copyright (C) 2000-2003 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2003 Guus Sliepen <guus@sliepen.eu.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: route.c,v 1.1.2.70 2003/12/12 19:52:25 guus Exp $
25 #ifdef HAVE_NET_ETHERNET_H
26 #include <net/ethernet.h>
28 #ifdef HAVE_NET_IF_ARP_H
29 #include <net/if_arp.h>
31 #ifdef HAVE_NETINET_IP_ICMP_H
32 #include <netinet/ip_icmp.h>
34 #ifdef HAVE_NETINET_ICMP6_H
35 #include <netinet/icmp6.h>
37 #ifdef HAVE_NETINET_IF_ETHER_H
38 #include <netinet/if_ether.h>
42 #include "connection.h"
53 rmode_t routing_mode = RMODE_ROUTER;
54 bool priorityinheritance = false;
56 int multicastexpire = 375;
57 bool overwrite_mac = false;
58 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
60 /* Sizes of various headers */
62 static const size_t ether_size = sizeof(struct ether_header);
63 static const size_t arp_size = sizeof(struct ether_arp);
64 static const size_t ip_size = sizeof(struct ip);
65 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
66 static const size_t ip6_size = sizeof(struct ip6_hdr);
67 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
68 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
69 static const size_t opt_size = sizeof(struct nd_opt_hdr);
73 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum)
76 uint32_t checksum = prevsum ^ 0xFFFF;
84 checksum += *(uint8_t *)p;
87 checksum = (checksum & 0xFFFF) + (checksum >> 16);
92 static bool ratelimit(int frequency) {
93 static time_t lasttime = 0;
97 if(++count > frequency)
107 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
108 if(packet->len < length) {
109 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got too short packet from %s (%s)"), source->name, source->hostname);
115 static void learn_mac(mac_t *address)
123 subnet = lookup_subnet_mac(address);
125 /* If we don't know this MAC address yet, store it */
128 ifdebug(TRAFFIC) logger(LOG_INFO, _("Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx"),
129 address->x[0], address->x[1], address->x[2], address->x[3],
130 address->x[4], address->x[5]);
132 subnet = new_subnet();
133 subnet->type = SUBNET_MAC;
134 subnet->expires = now + macexpire;
135 subnet->net.mac.address = *address;
136 subnet_add(myself, subnet);
138 /* And tell all other tinc daemons it's our MAC */
140 for(node = connection_tree->head; node; node = node->next) {
143 send_add_subnet(c, subnet);
148 subnet->expires = now + macexpire;
151 void age_subnets(void)
155 avl_node_t *node, *next, *node2;
159 for(node = myself->subnet_tree->head; node; node = next) {
162 if(s->expires && s->expires < now) {
164 char netstr[MAXNETSTR];
165 if(net2str(netstr, sizeof netstr, s))
166 logger(LOG_INFO, _("Subnet %s expired"), netstr);
169 for(node2 = connection_tree->head; node2; node2 = node2->next) {
172 send_del_subnet(c, s);
175 subnet_del(myself, s);
180 static void route_mac(node_t *source, vpn_packet_t *packet)
186 /* Learn source address */
189 learn_mac((mac_t *)(&packet->data[6]));
191 /* Lookup destination address */
193 subnet = lookup_subnet_mac((mac_t *)(&packet->data[0]));
196 broadcast_packet(source, packet);
200 if(subnet->owner == source) {
201 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
205 send_packet(subnet->owner, packet);
210 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t code)
213 struct icmp icmp = {0};
215 struct in_addr ip_src;
216 struct in_addr ip_dst;
224 /* Copy headers from packet into properly aligned structs on the stack */
226 memcpy(&ip, packet->data + ether_size, ip_size);
228 /* Remember original source and destination */
233 oldlen = packet->len - ether_size;
235 if(oldlen >= IP_MSS - ip_size - icmp_size)
236 oldlen = IP_MSS - ip_size - icmp_size;
238 /* Copy first part of original contents to ICMP message */
240 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
242 /* Fill in IPv4 header */
245 ip.ip_hl = ip_size / 4;
247 ip.ip_len = htons(ip_size + icmp_size + oldlen);
251 ip.ip_p = IPPROTO_ICMP;
256 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
258 /* Fill in ICMP header */
260 icmp.icmp_type = ICMP_DEST_UNREACH;
261 icmp.icmp_code = code;
264 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
265 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
267 /* Copy structs on stack back to packet */
269 memcpy(packet->data + ether_size, &ip, ip_size);
270 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
272 packet->len = ether_size + ip_size + icmp_size + oldlen;
274 send_packet(source, packet);
277 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet)
283 subnet = lookup_subnet_ipv4((ipv4_t *) &packet->data[30]);
286 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d"),
287 source->name, source->hostname,
293 route_ipv4_unreachable(source, packet, ICMP_NET_UNKNOWN);
297 if(subnet->owner == source) {
298 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
302 if(!subnet->owner->status.reachable)
303 route_ipv4_unreachable(source, packet, ICMP_NET_UNREACH);
305 if(priorityinheritance)
306 packet->priority = packet->data[15];
308 send_packet(subnet->owner, packet);
311 static void route_ipv4(node_t *source, vpn_packet_t *packet)
315 if(!checklength(source, packet, ether_size + ip_size))
319 if(packet->data[30] & 0xf0 == 0xe0) {
320 route_ipv4_multicast(source, packet);
325 route_ipv4_unicast(source, packet);
330 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t code)
333 struct icmp6_hdr icmp6 = {0};
337 struct in6_addr ip6_src; /* source address */
338 struct in6_addr ip6_dst; /* destination address */
348 /* Copy headers from packet to structs on the stack */
350 memcpy(&ip6, packet->data + ether_size, ip6_size);
352 /* Remember original source and destination */
354 pseudo.ip6_src = ip6.ip6_dst;
355 pseudo.ip6_dst = ip6.ip6_src;
357 pseudo.length = packet->len - ether_size;
359 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
360 pseudo.length = IP_MSS - ip6_size - icmp6_size;
362 /* Copy first part of original contents to ICMP message */
364 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
366 /* Fill in IPv6 header */
368 ip6.ip6_flow = htonl(0x60000000UL);
369 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
370 ip6.ip6_nxt = IPPROTO_ICMPV6;
372 ip6.ip6_src = pseudo.ip6_src;
373 ip6.ip6_dst = pseudo.ip6_dst;
375 /* Fill in ICMP header */
377 icmp6.icmp6_type = ICMP6_DST_UNREACH;
378 icmp6.icmp6_code = code;
379 icmp6.icmp6_cksum = 0;
381 /* Create pseudo header */
383 pseudo.length = htonl(icmp6_size + pseudo.length);
384 pseudo.next = htonl(IPPROTO_ICMPV6);
386 /* Generate checksum */
388 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
389 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
390 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
392 icmp6.icmp6_cksum = checksum;
394 /* Copy structs on stack back to packet */
396 memcpy(packet->data + ether_size, &ip6, ip6_size);
397 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
399 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
401 send_packet(source, packet);
404 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet)
410 subnet = lookup_subnet_ipv6((ipv6_t *) &packet->data[38]);
413 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
414 source->name, source->hostname,
415 ntohs(*(uint16_t *) &packet->data[38]),
416 ntohs(*(uint16_t *) &packet->data[40]),
417 ntohs(*(uint16_t *) &packet->data[42]),
418 ntohs(*(uint16_t *) &packet->data[44]),
419 ntohs(*(uint16_t *) &packet->data[46]),
420 ntohs(*(uint16_t *) &packet->data[48]),
421 ntohs(*(uint16_t *) &packet->data[50]),
422 ntohs(*(uint16_t *) &packet->data[52]));
424 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH_ADDR);
428 if(subnet->owner == source) {
429 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
433 if(!subnet->owner->status.reachable)
434 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH_NOROUTE);
436 send_packet(subnet->owner, packet);
439 #ifdef ENABLE_MULTICAST
440 static void route_ipv6_multicast(node_t *source, vpn_packet_t *packet)
443 subnet_t *subnet, search = {0};
447 search.type = SUBNET_IPV6;
448 search.net.ipv6.address = *(ipv6_t *)(packet->data + ether_size + ip6_size + icmp6_size);
449 search.net.ipv6.prefixlength = 128;
452 ifdebug(TRAFFIC) logger(LOG_INFO, _("Multicasting packet of %d bytes from %s (%s)"), packet->len, source->name, source->hostname);
454 for(node = avl_search_closest_smaller_node(myself->subnet_tree, &search); node; node = node->next) {
457 if(subnet->type != SUBNET_IPV6 || memcmp(&subnet->net.ipv6.address, packet->data + ether_size + ip6_size + icmp6_size, sizeof(ipv6_t)))
460 if(subnet->owner != source)
461 send_packet(subnet->owner, packet);
468 static void route_neighborsol(node_t *source, vpn_packet_t *packet)
471 struct nd_neighbor_solicit ns;
472 struct nd_opt_hdr opt;
477 struct in6_addr ip6_src; /* source address */
478 struct in6_addr ip6_dst; /* destination address */
485 if(!checklength(source, packet, ether_size + ip6_size + ns_size + opt_size + ETH_ALEN))
488 if(source != myself) {
489 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got neighbor solicitation request from %s (%s) while in router mode!"), source->name, source->hostname);
493 /* Copy headers from packet to structs on the stack */
495 memcpy(&ip6, packet->data + ether_size, ip6_size);
496 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
497 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
499 /* First, snatch the source address from the neighbor solicitation packet */
502 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
504 /* Check if this is a valid neighbor solicitation request */
506 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
507 opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR) {
508 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
512 /* Create pseudo header */
514 pseudo.ip6_src = ip6.ip6_src;
515 pseudo.ip6_dst = ip6.ip6_dst;
516 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
517 pseudo.next = htonl(IPPROTO_ICMPV6);
519 /* Generate checksum */
521 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
522 checksum = inet_checksum(&ns, ns_size, checksum);
523 checksum = inet_checksum(&opt, opt_size, checksum);
524 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
527 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
531 /* Check if the IPv6 address exists on the VPN */
533 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
536 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
537 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
538 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
539 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
540 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
541 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
542 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
543 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
544 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
549 /* Check if it is for our own subnet */
551 if(subnet->owner == myself)
552 return; /* silently ignore */
554 /* Create neighbor advertation reply */
556 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
557 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
559 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
560 ip6.ip6_src = ns.nd_ns_target;
562 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
565 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
566 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
567 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
569 /* Create pseudo header */
571 pseudo.ip6_src = ip6.ip6_src;
572 pseudo.ip6_dst = ip6.ip6_dst;
573 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
574 pseudo.next = htonl(IPPROTO_ICMPV6);
576 /* Generate checksum */
578 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
579 checksum = inet_checksum(&ns, ns_size, checksum);
580 checksum = inet_checksum(&opt, opt_size, checksum);
581 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
583 ns.nd_ns_hdr.icmp6_cksum = checksum;
585 /* Copy structs on stack back to packet */
587 memcpy(packet->data + ether_size, &ip6, ip6_size);
588 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
589 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
591 send_packet(source, packet);
596 #ifdef ENABLE_MULTICAST
597 static void route_membershipreport(node_t *source, vpn_packet_t *packet)
600 struct icmp6_hdr icmp6;
601 subnet_t *subnet, search = {0};
605 struct in6_addr ip6_src; /* source address */
606 struct in6_addr ip6_dst; /* destination address */
613 if(!checklength(source, packet, ether_size + ip6_size + icmp6_size + sizeof(ipv6_t)))
616 if(source != myself) {
617 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got membership report from %s (%s) while in router mode!"), source->name, source->hostname);
621 /* Copy headers from packet to structs on the stack */
623 memcpy(&ip6, packet->data + ether_size, ip6_size);
624 memcpy(&icmp6, packet->data + ether_size + ip6_size + 8, icmp6_size);
626 /* Create pseudo header */
628 pseudo.ip6_src = ip6.ip6_src;
629 pseudo.ip6_dst = ip6.ip6_dst;
630 pseudo.length = htonl(icmp6_size + sizeof(ipv6_t));
631 pseudo.next = htonl(IPPROTO_ICMPV6);
633 /* Generate checksum */
635 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
636 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
637 checksum = inet_checksum(packet->data + ether_size + ip6_size + 8 + icmp6_size, sizeof(ipv6_t), checksum);
640 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for membership report"));
644 /* Check if the IPv6 address exists on the VPN */
646 search.type = SUBNET_IPV6;
647 search.net.ipv6.address = *(ipv6_t *)(packet->data + ether_size + ip6_size + 8 + icmp6_size);
648 search.net.ipv6.prefixlength = 128;
649 search.owner = myself;
651 subnet = avl_search(myself->subnet_tree, &search);
657 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Learned new IPv6 multicast address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
658 ntohs(*(uint16_t *) &packet->data[70]),
659 ntohs(*(uint16_t *) &packet->data[72]),
660 ntohs(*(uint16_t *) &packet->data[74]),
661 ntohs(*(uint16_t *) &packet->data[76]),
662 ntohs(*(uint16_t *) &packet->data[78]),
663 ntohs(*(uint16_t *) &packet->data[80]),
664 ntohs(*(uint16_t *) &packet->data[82]),
665 ntohs(*(uint16_t *) &packet->data[84]));
667 subnet = new_subnet();
668 subnet->type = SUBNET_IPV6;
669 subnet->net.ipv6.address = *(ipv6_t *)(packet->data + ether_size + ip6_size + 8 + icmp6_size);
670 subnet->net.ipv6.prefixlength = 128;
671 subnet->expires = now + multicastexpire;
672 subnet_add(myself, subnet);
674 /* And tell all other tinc daemons it's ours */
676 for(node = connection_tree->head; node; node = node->next) {
679 send_add_subnet(c, subnet);
684 subnet->expires = now + multicastexpire;
688 static void route_ipv6(node_t *source, vpn_packet_t *packet)
692 if(!checklength(source, packet, ether_size + ip6_size))
695 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
696 route_neighborsol(source, packet);
700 #ifdef ENABLE_MULTICAST
701 if(packet->data[20] == IPPROTO_HOPOPTS && checklength(source, packet, ether_size + ip6_size + 8)
702 && packet->data[54] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + 8 + icmp6_size)
703 && packet->data[62] == ICMP6_MEMBERSHIP_REPORT) {
704 route_membershipreport(source, packet);
708 if(packet->data[38] == 0xff && packet->data[39] & 0x0c) {
709 route_ipv6_multicast(source, packet);
714 route_ipv6_unicast(source, packet);
719 static void route_arp(node_t *source, vpn_packet_t *packet)
721 struct ether_arp arp;
727 if(!checklength(source, packet, ether_size + arp_size))
730 if(source != myself) {
731 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got ARP request from %s (%s) while in router mode!"), source->name, source->hostname);
735 /* First, snatch the source address from the ARP packet */
738 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
740 /* Copy headers from packet to structs on the stack */
742 memcpy(&arp, packet->data + ether_size, arp_size);
744 /* Check if this is a valid ARP request */
746 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
747 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
748 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
752 /* Check if the IPv4 address exists on the VPN */
754 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
757 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
758 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
763 /* Check if it is for our own subnet */
765 if(subnet->owner == myself)
766 return; /* silently ignore */
768 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
769 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
771 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
772 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
773 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
775 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
776 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
777 arp.arp_op = htons(ARPOP_REPLY);
779 /* Copy structs on stack back to packet */
781 memcpy(packet->data + ether_size, &arp, arp_size);
783 send_packet(source, packet);
786 void route(node_t *source, vpn_packet_t *packet)
790 if(!checklength(source, packet, ether_size))
793 switch (routing_mode) {
798 type = ntohs(*((uint16_t *)(&packet->data[12])));
801 route_arp(source, packet);
805 route_ipv4(source, packet);
809 route_ipv6(source, packet);
813 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown type %hx"), source->name, source->hostname, type);
820 route_mac(source, packet);
824 broadcast_packet(source, packet);