3 Copyright (C) 2000-2005 Ivo Timmermans,
4 2000-2009 Guus Sliepen <guus@tinc-vpn.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.
25 #include "splay_tree.h"
26 #include "connection.h"
37 rmode_t routing_mode = RMODE_ROUTER;
38 bool priorityinheritance = false;
40 bool overwrite_mac = false;
41 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
43 /* Sizes of various headers */
45 static const size_t ether_size = sizeof(struct ether_header);
46 static const size_t arp_size = sizeof(struct ether_arp);
47 static const size_t ip_size = sizeof(struct ip);
48 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
49 static const size_t ip6_size = sizeof(struct ip6_hdr);
50 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
51 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
52 static const size_t opt_size = sizeof(struct nd_opt_hdr);
54 static struct event age_subnets_event;
58 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum)
61 uint32_t checksum = prevsum ^ 0xFFFF;
69 checksum += *(uint8_t *)p;
72 checksum = (checksum & 0xFFFF) + (checksum >> 16);
77 static bool ratelimit(int frequency) {
78 static time_t lasttime = 0;
80 time_t now = time(NULL);
83 if(++count > frequency)
93 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
94 if(packet->len < length) {
95 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got too short packet from %s (%s)"), source->name, source->hostname);
101 static void swap_mac_addresses(vpn_packet_t *packet) {
103 memcpy(&tmp, &packet->data[0], sizeof tmp);
104 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
105 memcpy(&packet->data[6], &tmp, sizeof tmp);
108 static void age_subnets(int fd, short events, void *data)
112 splay_node_t *node, *next, *node2;
114 time_t now = time(NULL);
118 for(node = myself->subnet_tree->head; node; node = next) {
121 if(s->expires && s->expires < now) {
123 char netstr[MAXNETSTR];
124 if(net2str(netstr, sizeof netstr, s))
125 logger(LOG_INFO, _("Subnet %s expired"), netstr);
128 for(node2 = connection_tree->head; node2; node2 = node2->next) {
131 send_del_subnet(c, s);
134 subnet_del(myself, s);
142 event_add(&age_subnets_event, &(struct timeval){10, 0});
145 static void learn_mac(mac_t *address)
153 subnet = lookup_subnet_mac(address);
155 /* If we don't know this MAC address yet, store it */
158 ifdebug(TRAFFIC) logger(LOG_INFO, _("Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx"),
159 address->x[0], address->x[1], address->x[2], address->x[3],
160 address->x[4], address->x[5]);
162 subnet = new_subnet();
163 subnet->type = SUBNET_MAC;
164 subnet->expires = time(NULL) + macexpire;
165 subnet->net.mac.address = *address;
166 subnet_add(myself, subnet);
168 /* And tell all other tinc daemons it's our MAC */
170 for(node = connection_tree->head; node; node = node->next) {
173 send_add_subnet(c, subnet);
176 if(!timeout_initialized(&age_subnets_event))
177 timeout_set(&age_subnets_event, age_subnets, NULL);
178 event_add(&age_subnets_event, &(struct timeval){10, 0});
181 subnet->expires = time(NULL) + macexpire;
187 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code)
190 struct icmp icmp = {0};
192 struct in_addr ip_src;
193 struct in_addr ip_dst;
201 /* Swap Ethernet source and destination addresses */
203 swap_mac_addresses(packet);
205 /* Copy headers from packet into properly aligned structs on the stack */
207 memcpy(&ip, packet->data + ether_size, ip_size);
209 /* Remember original source and destination */
214 oldlen = packet->len - ether_size;
216 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
217 icmp.icmp_nextmtu = htons(packet->len - ether_size);
219 if(oldlen >= IP_MSS - ip_size - icmp_size)
220 oldlen = IP_MSS - ip_size - icmp_size;
222 /* Copy first part of original contents to ICMP message */
224 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
226 /* Fill in IPv4 header */
229 ip.ip_hl = ip_size / 4;
231 ip.ip_len = htons(ip_size + icmp_size + oldlen);
235 ip.ip_p = IPPROTO_ICMP;
240 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
242 /* Fill in ICMP header */
244 icmp.icmp_type = type;
245 icmp.icmp_code = code;
248 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
249 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
251 /* Copy structs on stack back to packet */
253 memcpy(packet->data + ether_size, &ip, ip_size);
254 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
256 packet->len = ether_size + ip_size + icmp_size + oldlen;
258 send_packet(source, packet);
263 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
265 vpn_packet_t fragment;
266 int len, maxlen, todo;
268 uint16_t ip_off, origf;
272 memcpy(&ip, packet->data + ether_size, ip_size);
273 fragment.priority = packet->priority;
275 if(ip.ip_hl != ip_size / 4)
278 todo = ntohs(ip.ip_len) - ip_size;
280 if(ether_size + ip_size + todo != packet->len) {
281 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Length of packet (%d) doesn't match length in IPv4 header (%zd)"), packet->len, ether_size + ip_size + todo);
285 ifdebug(TRAFFIC) logger(LOG_INFO, _("Fragmenting packet of %d bytes to %s (%s)"), packet->len, dest->name, dest->hostname);
287 offset = packet->data + ether_size + ip_size;
288 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
289 ip_off = ntohs(ip.ip_off);
290 origf = ip_off & ~IP_OFFMASK;
291 ip_off &= IP_OFFMASK;
294 len = todo > maxlen ? maxlen : todo;
295 memcpy(fragment.data + ether_size + ip_size, offset, len);
299 ip.ip_len = htons(ip_size + len);
300 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
302 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
303 memcpy(fragment.data, packet->data, ether_size);
304 memcpy(fragment.data + ether_size, &ip, ip_size);
305 fragment.len = ether_size + ip_size + len;
307 send_packet(dest, &fragment);
313 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet)
321 memcpy(&dest, &packet->data[30], sizeof dest);
322 subnet = lookup_subnet_ipv4(&dest);
325 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d"),
326 source->name, source->hostname,
332 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
336 if(subnet->owner == source) {
337 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
341 if(!subnet->owner->status.reachable)
342 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
344 if(priorityinheritance)
345 packet->priority = packet->data[15];
347 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
349 if(via && packet->len > via->mtu && via != myself) {
350 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);
351 if(packet->data[20] & 0x40) {
352 packet->len = via->mtu;
353 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
355 fragment_ipv4_packet(via, packet);
361 send_packet(subnet->owner, packet);
364 static void route_ipv4(node_t *source, vpn_packet_t *packet)
368 if(!checklength(source, packet, ether_size + ip_size))
371 if(((packet->data[30] & 0xf0) == 0xe0) || (
372 packet->data[30] == 255 &&
373 packet->data[31] == 255 &&
374 packet->data[32] == 255 &&
375 packet->data[33] == 255))
376 broadcast_packet(source, packet);
378 route_ipv4_unicast(source, packet);
383 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code)
386 struct icmp6_hdr icmp6 = {0};
390 struct in6_addr ip6_src; /* source address */
391 struct in6_addr ip6_dst; /* destination address */
401 /* Swap Ethernet source and destination addresses */
403 swap_mac_addresses(packet);
405 /* Copy headers from packet to structs on the stack */
407 memcpy(&ip6, packet->data + ether_size, ip6_size);
409 /* Remember original source and destination */
411 pseudo.ip6_src = ip6.ip6_dst;
412 pseudo.ip6_dst = ip6.ip6_src;
414 pseudo.length = packet->len - ether_size;
416 if(type == ICMP6_PACKET_TOO_BIG)
417 icmp6.icmp6_mtu = htonl(pseudo.length);
419 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
420 pseudo.length = IP_MSS - ip6_size - icmp6_size;
422 /* Copy first part of original contents to ICMP message */
424 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
426 /* Fill in IPv6 header */
428 ip6.ip6_flow = htonl(0x60000000UL);
429 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
430 ip6.ip6_nxt = IPPROTO_ICMPV6;
432 ip6.ip6_src = pseudo.ip6_src;
433 ip6.ip6_dst = pseudo.ip6_dst;
435 /* Fill in ICMP header */
437 icmp6.icmp6_type = type;
438 icmp6.icmp6_code = code;
439 icmp6.icmp6_cksum = 0;
441 /* Create pseudo header */
443 pseudo.length = htonl(icmp6_size + pseudo.length);
444 pseudo.next = htonl(IPPROTO_ICMPV6);
446 /* Generate checksum */
448 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
449 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
450 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
452 icmp6.icmp6_cksum = checksum;
454 /* Copy structs on stack back to packet */
456 memcpy(packet->data + ether_size, &ip6, ip6_size);
457 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
459 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
461 send_packet(source, packet);
464 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet)
472 memcpy(&dest, &packet->data[38], sizeof dest);
473 subnet = lookup_subnet_ipv6(&dest);
476 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
477 source->name, source->hostname,
487 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
491 if(subnet->owner == source) {
492 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
496 if(!subnet->owner->status.reachable)
497 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
499 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
501 if(via && packet->len > via->mtu && via != myself) {
502 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);
503 packet->len = via->mtu;
504 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
508 send_packet(subnet->owner, packet);
513 static void route_neighborsol(node_t *source, vpn_packet_t *packet)
516 struct nd_neighbor_solicit ns;
517 struct nd_opt_hdr opt;
523 struct in6_addr ip6_src; /* source address */
524 struct in6_addr ip6_dst; /* destination address */
531 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
534 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
536 if(source != myself) {
537 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got neighbor solicitation request from %s (%s) while in router mode!"), source->name, source->hostname);
541 /* Copy headers from packet to structs on the stack */
543 memcpy(&ip6, packet->data + ether_size, ip6_size);
544 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
546 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
548 /* First, snatch the source address from the neighbor solicitation packet */
551 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
553 /* Check if this is a valid neighbor solicitation request */
555 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
556 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
557 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
561 /* Create pseudo header */
563 pseudo.ip6_src = ip6.ip6_src;
564 pseudo.ip6_dst = ip6.ip6_dst;
566 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
568 pseudo.length = htonl(ns_size);
569 pseudo.next = htonl(IPPROTO_ICMPV6);
571 /* Generate checksum */
573 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
574 checksum = inet_checksum(&ns, ns_size, checksum);
576 checksum = inet_checksum(&opt, opt_size, checksum);
577 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
581 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
585 /* Check if the IPv6 address exists on the VPN */
587 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
590 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
591 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
592 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
593 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
594 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
595 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
596 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
597 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
598 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
603 /* Check if it is for our own subnet */
605 if(subnet->owner == myself)
606 return; /* silently ignore */
608 /* Create neighbor advertation reply */
610 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
611 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
613 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
614 ip6.ip6_src = ns.nd_ns_target;
617 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
620 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
621 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
622 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
624 /* Create pseudo header */
626 pseudo.ip6_src = ip6.ip6_src;
627 pseudo.ip6_dst = ip6.ip6_dst;
629 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
631 pseudo.length = htonl(ns_size);
632 pseudo.next = htonl(IPPROTO_ICMPV6);
634 /* Generate checksum */
636 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
637 checksum = inet_checksum(&ns, ns_size, checksum);
639 checksum = inet_checksum(&opt, opt_size, checksum);
640 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
643 ns.nd_ns_hdr.icmp6_cksum = checksum;
645 /* Copy structs on stack back to packet */
647 memcpy(packet->data + ether_size, &ip6, ip6_size);
648 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
650 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
652 send_packet(source, packet);
655 static void route_ipv6(node_t *source, vpn_packet_t *packet)
659 if(!checklength(source, packet, ether_size + ip6_size))
662 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
663 route_neighborsol(source, packet);
667 if(packet->data[38] == 255)
668 broadcast_packet(source, packet);
670 route_ipv6_unicast(source, packet);
675 static void route_arp(node_t *source, vpn_packet_t *packet)
677 struct ether_arp arp;
683 if(!checklength(source, packet, ether_size + arp_size))
686 if(source != myself) {
687 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got ARP request from %s (%s) while in router mode!"), source->name, source->hostname);
691 /* First, snatch the source address from the ARP packet */
694 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
696 /* Copy headers from packet to structs on the stack */
698 memcpy(&arp, packet->data + ether_size, arp_size);
700 /* Check if this is a valid ARP request */
702 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
703 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof addr || ntohs(arp.arp_op) != ARPOP_REQUEST) {
704 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
708 /* Check if the IPv4 address exists on the VPN */
710 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
713 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
714 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
719 /* Check if it is for our own subnet */
721 if(subnet->owner == myself)
722 return; /* silently ignore */
724 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
725 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
727 memcpy(&addr, arp.arp_tpa, sizeof addr); /* save protocol addr */
728 memcpy(arp.arp_tpa, arp.arp_spa, sizeof addr); /* swap destination and source protocol address */
729 memcpy(arp.arp_spa, &addr, sizeof addr); /* ... */
731 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
732 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
733 arp.arp_op = htons(ARPOP_REPLY);
735 /* Copy structs on stack back to packet */
737 memcpy(packet->data + ether_size, &arp, arp_size);
739 send_packet(source, packet);
742 static void route_mac(node_t *source, vpn_packet_t *packet)
750 /* Learn source address */
752 if(source == myself) {
754 memcpy(&src, &packet->data[6], sizeof src);
758 /* Lookup destination address */
760 memcpy(&dest, &packet->data[0], sizeof dest);
761 subnet = lookup_subnet_mac(&dest);
764 broadcast_packet(source, packet);
768 if(subnet->owner == source) {
769 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
773 // Handle packets larger than PMTU
775 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
777 if(via && packet->len > via->mtu && via != myself) {
778 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);
779 uint16_t type = packet->data[12] << 8 | packet->data[13];
780 if(type == ETH_P_IP) {
781 if(packet->data[20] & 0x40) {
782 packet->len = via->mtu;
783 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
785 fragment_ipv4_packet(via, packet);
788 } else if(type == ETH_P_IPV6) {
789 packet->len = via->mtu;
790 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
795 send_packet(subnet->owner, packet);
799 void route(node_t *source, vpn_packet_t *packet)
803 if(!checklength(source, packet, ether_size))
806 switch (routing_mode) {
809 uint16_t type = packet->data[12] << 8 | packet->data[13];
813 route_arp(source, packet);
817 route_ipv4(source, packet);
821 route_ipv6(source, packet);
825 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown type %hx"), source->name, source->hostname, type);
832 route_mac(source, packet);
836 broadcast_packet(source, packet);