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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "connection.h"
35 rmode_t routing_mode = RMODE_ROUTER;
36 bool priorityinheritance = false;
38 bool overwrite_mac = false;
39 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
41 /* Sizes of various headers */
43 static const size_t ether_size = sizeof(struct ether_header);
44 static const size_t arp_size = sizeof(struct ether_arp);
45 static const size_t ip_size = sizeof(struct ip);
46 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
47 static const size_t ip6_size = sizeof(struct ip6_hdr);
48 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
49 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
50 static const size_t opt_size = sizeof(struct nd_opt_hdr);
51 #define max(a, b) ((a) > (b) ? (a) : (b))
55 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
57 uint32_t checksum = prevsum ^ 0xFFFF;
65 checksum += *(uint8_t *)p;
68 checksum = (checksum & 0xFFFF) + (checksum >> 16);
73 static bool ratelimit(int frequency) {
74 static time_t lasttime = 0;
78 if(++count > frequency)
88 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
89 if(packet->len < length) {
90 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
96 static void swap_mac_addresses(vpn_packet_t *packet) {
98 memcpy(&tmp, &packet->data[0], sizeof tmp);
99 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
100 memcpy(&packet->data[6], &tmp, sizeof tmp);
103 static void learn_mac(mac_t *address) {
108 subnet = lookup_subnet_mac(myself, address);
110 /* If we don't know this MAC address yet, store it */
113 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
114 address->x[0], address->x[1], address->x[2], address->x[3],
115 address->x[4], address->x[5]);
117 subnet = new_subnet();
118 subnet->type = SUBNET_MAC;
119 subnet->expires = now + macexpire;
120 subnet->net.mac.address = *address;
122 subnet_add(myself, subnet);
124 /* And tell all other tinc daemons it's our MAC */
126 for(node = connection_tree->head; node; node = node->next) {
129 send_add_subnet(c, subnet);
134 subnet->expires = now + macexpire;
137 void age_subnets(void) {
140 avl_node_t *node, *next, *node2;
142 for(node = myself->subnet_tree->head; node; node = next) {
145 if(s->expires && s->expires < now) {
147 char netstr[MAXNETSTR];
148 if(net2str(netstr, sizeof netstr, s))
149 logger(LOG_INFO, "Subnet %s expired", netstr);
152 for(node2 = connection_tree->head; node2; node2 = node2->next) {
155 send_del_subnet(c, s);
158 subnet_del(myself, s);
165 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
167 struct icmp icmp = {0};
169 struct in_addr ip_src;
170 struct in_addr ip_dst;
176 /* Swap Ethernet source and destination addresses */
178 swap_mac_addresses(packet);
180 /* Copy headers from packet into properly aligned structs on the stack */
182 memcpy(&ip, packet->data + ether_size, ip_size);
184 /* Remember original source and destination */
189 oldlen = packet->len - ether_size;
191 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
192 icmp.icmp_nextmtu = htons(packet->len - ether_size);
194 if(oldlen >= IP_MSS - ip_size - icmp_size)
195 oldlen = IP_MSS - ip_size - icmp_size;
197 /* Copy first part of original contents to ICMP message */
199 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
201 /* Fill in IPv4 header */
204 ip.ip_hl = ip_size / 4;
206 ip.ip_len = htons(ip_size + icmp_size + oldlen);
210 ip.ip_p = IPPROTO_ICMP;
215 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
217 /* Fill in ICMP header */
219 icmp.icmp_type = type;
220 icmp.icmp_code = code;
223 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
224 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
226 /* Copy structs on stack back to packet */
228 memcpy(packet->data + ether_size, &ip, ip_size);
229 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
231 packet->len = ether_size + ip_size + icmp_size + oldlen;
233 send_packet(source, packet);
238 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
240 vpn_packet_t fragment;
241 int len, maxlen, todo;
243 uint16_t ip_off, origf;
245 memcpy(&ip, packet->data + ether_size, ip_size);
246 fragment.priority = packet->priority;
248 if(ip.ip_hl != ip_size / 4)
251 todo = ntohs(ip.ip_len) - ip_size;
253 if(ether_size + ip_size + todo != packet->len) {
254 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%zd)", packet->len, ether_size + ip_size + todo);
258 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
260 offset = packet->data + ether_size + ip_size;
261 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
262 ip_off = ntohs(ip.ip_off);
263 origf = ip_off & ~IP_OFFMASK;
264 ip_off &= IP_OFFMASK;
267 len = todo > maxlen ? maxlen : todo;
268 memcpy(fragment.data + ether_size + ip_size, offset, len);
272 ip.ip_len = htons(ip_size + len);
273 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
275 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
276 memcpy(fragment.data, packet->data, ether_size);
277 memcpy(fragment.data + ether_size, &ip, ip_size);
278 fragment.len = ether_size + ip_size + len;
280 send_packet(dest, &fragment);
286 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
291 memcpy(&dest, &packet->data[30], sizeof dest);
292 subnet = lookup_subnet_ipv4(&dest);
295 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
296 source->name, source->hostname,
302 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
306 if(subnet->owner == source) {
307 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
311 if(!subnet->owner->status.reachable)
312 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
314 if(priorityinheritance)
315 packet->priority = packet->data[15];
317 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
319 if(via && packet->len > max(via->mtu, 590) && via != myself) {
320 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);
321 if(packet->data[20] & 0x40) {
322 packet->len = max(via->mtu, 590);
323 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
325 fragment_ipv4_packet(via, packet);
331 send_packet(subnet->owner, packet);
334 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
335 if(!checklength(source, packet, ether_size + ip_size))
338 if(((packet->data[30] & 0xf0) == 0xe0) || (
339 packet->data[30] == 255 &&
340 packet->data[31] == 255 &&
341 packet->data[32] == 255 &&
342 packet->data[33] == 255))
343 broadcast_packet(source, packet);
345 route_ipv4_unicast(source, packet);
350 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
352 struct icmp6_hdr icmp6 = {0};
356 struct in6_addr ip6_src; /* source address */
357 struct in6_addr ip6_dst; /* destination address */
365 /* Swap Ethernet source and destination addresses */
367 swap_mac_addresses(packet);
369 /* Copy headers from packet to structs on the stack */
371 memcpy(&ip6, packet->data + ether_size, ip6_size);
373 /* Remember original source and destination */
375 pseudo.ip6_src = ip6.ip6_dst;
376 pseudo.ip6_dst = ip6.ip6_src;
378 pseudo.length = packet->len - ether_size;
380 if(type == ICMP6_PACKET_TOO_BIG)
381 icmp6.icmp6_mtu = htonl(pseudo.length);
383 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
384 pseudo.length = IP_MSS - ip6_size - icmp6_size;
386 /* Copy first part of original contents to ICMP message */
388 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
390 /* Fill in IPv6 header */
392 ip6.ip6_flow = htonl(0x60000000UL);
393 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
394 ip6.ip6_nxt = IPPROTO_ICMPV6;
396 ip6.ip6_src = pseudo.ip6_src;
397 ip6.ip6_dst = pseudo.ip6_dst;
399 /* Fill in ICMP header */
401 icmp6.icmp6_type = type;
402 icmp6.icmp6_code = code;
403 icmp6.icmp6_cksum = 0;
405 /* Create pseudo header */
407 pseudo.length = htonl(icmp6_size + pseudo.length);
408 pseudo.next = htonl(IPPROTO_ICMPV6);
410 /* Generate checksum */
412 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
413 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
414 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
416 icmp6.icmp6_cksum = checksum;
418 /* Copy structs on stack back to packet */
420 memcpy(packet->data + ether_size, &ip6, ip6_size);
421 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
423 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
425 send_packet(source, packet);
428 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
433 memcpy(&dest, &packet->data[38], sizeof dest);
434 subnet = lookup_subnet_ipv6(&dest);
437 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
438 source->name, source->hostname,
448 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
452 if(subnet->owner == source) {
453 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
457 if(!subnet->owner->status.reachable)
458 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
460 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
462 if(via && packet->len > max(via->mtu, 1294) && via != myself) {
463 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);
464 packet->len = max(via->mtu, 1294);
465 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
469 send_packet(subnet->owner, packet);
474 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
476 struct nd_neighbor_solicit ns;
477 struct nd_opt_hdr opt;
483 struct in6_addr ip6_src; /* source address */
484 struct in6_addr ip6_dst; /* destination address */
489 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
492 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
494 if(source != myself) {
495 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
499 /* Copy headers from packet to structs on the stack */
501 memcpy(&ip6, packet->data + ether_size, ip6_size);
502 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
504 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
506 /* First, snatch the source address from the neighbor solicitation packet */
509 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
511 /* Check if this is a valid neighbor solicitation request */
513 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
514 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
515 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
519 /* Create pseudo header */
521 pseudo.ip6_src = ip6.ip6_src;
522 pseudo.ip6_dst = ip6.ip6_dst;
524 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
526 pseudo.length = htonl(ns_size);
527 pseudo.next = htonl(IPPROTO_ICMPV6);
529 /* Generate checksum */
531 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
532 checksum = inet_checksum(&ns, ns_size, checksum);
534 checksum = inet_checksum(&opt, opt_size, checksum);
535 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
539 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
543 /* Check if the IPv6 address exists on the VPN */
545 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
548 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
549 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
550 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
551 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
552 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
553 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
554 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
555 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
556 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
561 /* Check if it is for our own subnet */
563 if(subnet->owner == myself)
564 return; /* silently ignore */
566 /* Create neighbor advertation reply */
568 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
569 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
571 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
572 ip6.ip6_src = ns.nd_ns_target;
575 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
578 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
579 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
580 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
582 /* Create pseudo header */
584 pseudo.ip6_src = ip6.ip6_src;
585 pseudo.ip6_dst = ip6.ip6_dst;
587 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
589 pseudo.length = htonl(ns_size);
590 pseudo.next = htonl(IPPROTO_ICMPV6);
592 /* Generate checksum */
594 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
595 checksum = inet_checksum(&ns, ns_size, checksum);
597 checksum = inet_checksum(&opt, opt_size, checksum);
598 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
601 ns.nd_ns_hdr.icmp6_cksum = checksum;
603 /* Copy structs on stack back to packet */
605 memcpy(packet->data + ether_size, &ip6, ip6_size);
606 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
608 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
610 send_packet(source, packet);
613 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
614 if(!checklength(source, packet, ether_size + ip6_size))
617 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
618 route_neighborsol(source, packet);
622 if(packet->data[38] == 255)
623 broadcast_packet(source, packet);
625 route_ipv6_unicast(source, packet);
630 static void route_arp(node_t *source, vpn_packet_t *packet) {
631 struct ether_arp arp;
635 if(!checklength(source, packet, ether_size + arp_size))
638 if(source != myself) {
639 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
643 /* First, snatch the source address from the ARP packet */
646 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
648 /* Copy headers from packet to structs on the stack */
650 memcpy(&arp, packet->data + ether_size, arp_size);
652 /* Check if this is a valid ARP request */
654 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
655 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
656 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
660 /* Check if the IPv4 address exists on the VPN */
662 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
665 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
666 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
671 /* Check if it is for our own subnet */
673 if(subnet->owner == myself)
674 return; /* silently ignore */
676 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
677 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
679 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
680 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
681 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
683 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
684 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
685 arp.arp_op = htons(ARPOP_REPLY);
687 /* Copy structs on stack back to packet */
689 memcpy(packet->data + ether_size, &arp, arp_size);
691 send_packet(source, packet);
694 static void route_mac(node_t *source, vpn_packet_t *packet) {
698 /* Learn source address */
700 if(source == myself) {
702 memcpy(&src, &packet->data[6], sizeof src);
706 /* Lookup destination address */
708 memcpy(&dest, &packet->data[0], sizeof dest);
709 subnet = lookup_subnet_mac(NULL, &dest);
712 broadcast_packet(source, packet);
716 if(subnet->owner == source) {
717 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
721 // Handle packets larger than PMTU
723 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
725 if(via && packet->len > via->mtu && via != myself) {
726 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);
727 uint16_t type = packet->data[12] << 8 | packet->data[13];
728 if(type == ETH_P_IP && packet->len > 590) {
729 if(packet->data[20] & 0x40) {
730 packet->len = via->mtu;
731 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
733 fragment_ipv4_packet(via, packet);
736 } else if(type == ETH_P_IPV6 && packet->len > 1294) {
737 packet->len = via->mtu;
738 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
743 send_packet(subnet->owner, packet);
746 void route(node_t *source, vpn_packet_t *packet) {
747 if(!checklength(source, packet, ether_size))
750 switch (routing_mode) {
753 uint16_t type = packet->data[12] << 8 | packet->data[13];
757 route_arp(source, packet);
761 route_ipv4(source, packet);
765 route_ipv6(source, packet);
769 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
776 route_mac(source, packet);
780 broadcast_packet(source, packet);