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);
54 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
56 uint32_t checksum = prevsum ^ 0xFFFF;
64 checksum += *(uint8_t *)p;
67 checksum = (checksum & 0xFFFF) + (checksum >> 16);
72 static bool ratelimit(int frequency) {
73 static time_t lasttime = 0;
77 if(++count > frequency)
87 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
88 if(packet->len < length) {
89 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got too short packet from %s (%s)"), source->name, source->hostname);
95 static void swap_mac_addresses(vpn_packet_t *packet) {
97 memcpy(&tmp, &packet->data[0], sizeof tmp);
98 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
99 memcpy(&packet->data[6], &tmp, sizeof tmp);
102 static void learn_mac(mac_t *address) {
109 subnet = lookup_subnet_mac(address);
111 /* If we don't know this MAC address yet, store it */
114 ifdebug(TRAFFIC) logger(LOG_INFO, _("Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx"),
115 address->x[0], address->x[1], address->x[2], address->x[3],
116 address->x[4], address->x[5]);
118 subnet = new_subnet();
119 subnet->type = SUBNET_MAC;
120 subnet->expires = now + macexpire;
121 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;
144 for(node = myself->subnet_tree->head; node; node = next) {
147 if(s->expires && s->expires < now) {
149 char netstr[MAXNETSTR];
150 if(net2str(netstr, sizeof netstr, s))
151 logger(LOG_INFO, _("Subnet %s expired"), netstr);
154 for(node2 = connection_tree->head; node2; node2 = node2->next) {
157 send_del_subnet(c, s);
160 subnet_del(myself, s);
167 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
169 struct icmp icmp = {0};
171 struct in_addr ip_src;
172 struct in_addr ip_dst;
180 /* Swap Ethernet source and destination addresses */
182 swap_mac_addresses(packet);
184 /* Copy headers from packet into properly aligned structs on the stack */
186 memcpy(&ip, packet->data + ether_size, ip_size);
188 /* Remember original source and destination */
193 oldlen = packet->len - ether_size;
195 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
196 icmp.icmp_nextmtu = htons(packet->len - ether_size);
198 if(oldlen >= IP_MSS - ip_size - icmp_size)
199 oldlen = IP_MSS - ip_size - icmp_size;
201 /* Copy first part of original contents to ICMP message */
203 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
205 /* Fill in IPv4 header */
208 ip.ip_hl = ip_size / 4;
210 ip.ip_len = htons(ip_size + icmp_size + oldlen);
214 ip.ip_p = IPPROTO_ICMP;
219 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
221 /* Fill in ICMP header */
223 icmp.icmp_type = type;
224 icmp.icmp_code = code;
227 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
228 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
230 /* Copy structs on stack back to packet */
232 memcpy(packet->data + ether_size, &ip, ip_size);
233 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
235 packet->len = ether_size + ip_size + icmp_size + oldlen;
237 send_packet(source, packet);
242 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
244 vpn_packet_t fragment;
245 int len, maxlen, todo;
247 uint16_t ip_off, origf;
251 memcpy(&ip, packet->data + ether_size, ip_size);
252 fragment.priority = packet->priority;
254 if(ip.ip_hl != ip_size / 4)
257 todo = ntohs(ip.ip_len) - ip_size;
259 if(ether_size + ip_size + todo != packet->len) {
260 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Length of packet (%d) doesn't match length in IPv4 header (%zd)"), packet->len, ether_size + ip_size + todo);
264 ifdebug(TRAFFIC) logger(LOG_INFO, _("Fragmenting packet of %d bytes to %s (%s)"), packet->len, dest->name, dest->hostname);
266 offset = packet->data + ether_size + ip_size;
267 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
268 ip_off = ntohs(ip.ip_off);
269 origf = ip_off & ~IP_OFFMASK;
270 ip_off &= IP_OFFMASK;
273 len = todo > maxlen ? maxlen : todo;
274 memcpy(fragment.data + ether_size + ip_size, offset, len);
278 ip.ip_len = htons(ip_size + len);
279 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
281 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
282 memcpy(fragment.data, packet->data, ether_size);
283 memcpy(fragment.data + ether_size, &ip, ip_size);
284 fragment.len = ether_size + ip_size + len;
286 send_packet(dest, &fragment);
292 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
299 memcpy(&dest, &packet->data[30], sizeof dest);
300 subnet = lookup_subnet_ipv4(&dest);
303 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d"),
304 source->name, source->hostname,
310 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
314 if(subnet->owner == source) {
315 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
319 if(!subnet->owner->status.reachable)
320 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
322 if(priorityinheritance)
323 packet->priority = packet->data[15];
325 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
327 if(via && packet->len > via->mtu && via != myself) {
328 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);
329 if(packet->data[20] & 0x40) {
330 packet->len = via->mtu;
331 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
333 fragment_ipv4_packet(via, packet);
339 send_packet(subnet->owner, packet);
342 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
345 if(!checklength(source, packet, ether_size + ip_size))
348 if(((packet->data[30] & 0xf0) == 0xe0) || (
349 packet->data[30] == 255 &&
350 packet->data[31] == 255 &&
351 packet->data[32] == 255 &&
352 packet->data[33] == 255))
353 broadcast_packet(source, packet);
355 route_ipv4_unicast(source, packet);
360 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
362 struct icmp6_hdr icmp6 = {0};
366 struct in6_addr ip6_src; /* source address */
367 struct in6_addr ip6_dst; /* destination address */
377 /* Swap Ethernet source and destination addresses */
379 swap_mac_addresses(packet);
381 /* Copy headers from packet to structs on the stack */
383 memcpy(&ip6, packet->data + ether_size, ip6_size);
385 /* Remember original source and destination */
387 pseudo.ip6_src = ip6.ip6_dst;
388 pseudo.ip6_dst = ip6.ip6_src;
390 pseudo.length = packet->len - ether_size;
392 if(type == ICMP6_PACKET_TOO_BIG)
393 icmp6.icmp6_mtu = htonl(pseudo.length);
395 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
396 pseudo.length = IP_MSS - ip6_size - icmp6_size;
398 /* Copy first part of original contents to ICMP message */
400 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
402 /* Fill in IPv6 header */
404 ip6.ip6_flow = htonl(0x60000000UL);
405 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
406 ip6.ip6_nxt = IPPROTO_ICMPV6;
408 ip6.ip6_src = pseudo.ip6_src;
409 ip6.ip6_dst = pseudo.ip6_dst;
411 /* Fill in ICMP header */
413 icmp6.icmp6_type = type;
414 icmp6.icmp6_code = code;
415 icmp6.icmp6_cksum = 0;
417 /* Create pseudo header */
419 pseudo.length = htonl(icmp6_size + pseudo.length);
420 pseudo.next = htonl(IPPROTO_ICMPV6);
422 /* Generate checksum */
424 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
425 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
426 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
428 icmp6.icmp6_cksum = checksum;
430 /* Copy structs on stack back to packet */
432 memcpy(packet->data + ether_size, &ip6, ip6_size);
433 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
435 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
437 send_packet(source, packet);
440 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
447 memcpy(&dest, &packet->data[38], sizeof dest);
448 subnet = lookup_subnet_ipv6(&dest);
451 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
452 source->name, source->hostname,
462 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
466 if(subnet->owner == source) {
467 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
471 if(!subnet->owner->status.reachable)
472 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
474 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
476 if(via && packet->len > via->mtu && via != myself) {
477 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);
478 packet->len = via->mtu;
479 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
483 send_packet(subnet->owner, packet);
488 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
490 struct nd_neighbor_solicit ns;
491 struct nd_opt_hdr opt;
497 struct in6_addr ip6_src; /* source address */
498 struct in6_addr ip6_dst; /* destination address */
505 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
508 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
510 if(source != myself) {
511 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got neighbor solicitation request from %s (%s) while in router mode!"), source->name, source->hostname);
515 /* Copy headers from packet to structs on the stack */
517 memcpy(&ip6, packet->data + ether_size, ip6_size);
518 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
520 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
522 /* First, snatch the source address from the neighbor solicitation packet */
525 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
527 /* Check if this is a valid neighbor solicitation request */
529 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
530 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
531 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
535 /* Create pseudo header */
537 pseudo.ip6_src = ip6.ip6_src;
538 pseudo.ip6_dst = ip6.ip6_dst;
540 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
542 pseudo.length = htonl(ns_size);
543 pseudo.next = htonl(IPPROTO_ICMPV6);
545 /* Generate checksum */
547 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
548 checksum = inet_checksum(&ns, ns_size, checksum);
550 checksum = inet_checksum(&opt, opt_size, checksum);
551 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
555 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
559 /* Check if the IPv6 address exists on the VPN */
561 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
564 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
565 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
566 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
567 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
568 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
569 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
570 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
571 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
572 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
577 /* Check if it is for our own subnet */
579 if(subnet->owner == myself)
580 return; /* silently ignore */
582 /* Create neighbor advertation reply */
584 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
585 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
587 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
588 ip6.ip6_src = ns.nd_ns_target;
591 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
594 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
595 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
596 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
598 /* Create pseudo header */
600 pseudo.ip6_src = ip6.ip6_src;
601 pseudo.ip6_dst = ip6.ip6_dst;
603 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
605 pseudo.length = htonl(ns_size);
606 pseudo.next = htonl(IPPROTO_ICMPV6);
608 /* Generate checksum */
610 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
611 checksum = inet_checksum(&ns, ns_size, checksum);
613 checksum = inet_checksum(&opt, opt_size, checksum);
614 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
617 ns.nd_ns_hdr.icmp6_cksum = checksum;
619 /* Copy structs on stack back to packet */
621 memcpy(packet->data + ether_size, &ip6, ip6_size);
622 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
624 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
626 send_packet(source, packet);
629 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
632 if(!checklength(source, packet, ether_size + ip6_size))
635 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
636 route_neighborsol(source, packet);
640 if(packet->data[38] == 255)
641 broadcast_packet(source, packet);
643 route_ipv6_unicast(source, packet);
648 static void route_arp(node_t *source, vpn_packet_t *packet) {
649 struct ether_arp arp;
655 if(!checklength(source, packet, ether_size + arp_size))
658 if(source != myself) {
659 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got ARP request from %s (%s) while in router mode!"), source->name, source->hostname);
663 /* First, snatch the source address from the ARP packet */
666 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
668 /* Copy headers from packet to structs on the stack */
670 memcpy(&arp, packet->data + ether_size, arp_size);
672 /* Check if this is a valid ARP request */
674 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
675 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
676 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
680 /* Check if the IPv4 address exists on the VPN */
682 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
685 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
686 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
691 /* Check if it is for our own subnet */
693 if(subnet->owner == myself)
694 return; /* silently ignore */
696 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
697 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
699 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
700 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
701 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
703 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
704 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
705 arp.arp_op = htons(ARPOP_REPLY);
707 /* Copy structs on stack back to packet */
709 memcpy(packet->data + ether_size, &arp, arp_size);
711 send_packet(source, packet);
714 static void route_mac(node_t *source, vpn_packet_t *packet) {
721 /* Learn source address */
723 if(source == myself) {
725 memcpy(&src, &packet->data[6], sizeof src);
729 /* Lookup destination address */
731 memcpy(&dest, &packet->data[0], sizeof dest);
732 subnet = lookup_subnet_mac(&dest);
735 broadcast_packet(source, packet);
739 if(subnet->owner == source) {
740 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
744 // Handle packets larger than PMTU
746 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
748 if(via && packet->len > via->mtu && via != myself) {
749 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);
750 uint16_t type = packet->data[12] << 8 | packet->data[13];
751 if(type == ETH_P_IP) {
752 if(packet->data[20] & 0x40) {
753 packet->len = via->mtu;
754 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
756 fragment_ipv4_packet(via, packet);
759 } else if(type == ETH_P_IPV6) {
760 packet->len = via->mtu;
761 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
766 send_packet(subnet->owner, packet);
770 void route(node_t *source, vpn_packet_t *packet) {
773 if(!checklength(source, packet, ether_size))
776 switch (routing_mode) {
779 uint16_t type = packet->data[12] << 8 | packet->data[13];
783 route_arp(source, packet);
787 route_ipv4(source, packet);
791 route_ipv6(source, packet);
795 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown type %hx"), source->name, source->hostname, type);
802 route_mac(source, packet);
806 broadcast_packet(source, packet);