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.
23 #include "splay_tree.h"
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);
52 static struct event age_subnets_event;
56 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
58 uint32_t checksum = prevsum ^ 0xFFFF;
66 checksum += *(uint8_t *)p;
69 checksum = (checksum & 0xFFFF) + (checksum >> 16);
74 static bool ratelimit(int frequency) {
75 static time_t lasttime = 0;
77 time_t now = time(NULL);
80 if(++count > frequency)
90 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
91 if(packet->len < length) {
92 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
98 static void swap_mac_addresses(vpn_packet_t *packet) {
100 memcpy(&tmp, &packet->data[0], sizeof tmp);
101 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
102 memcpy(&packet->data[6], &tmp, sizeof tmp);
105 static void age_subnets(int fd, short events, void *data) {
108 splay_node_t *node, *next, *node2;
110 time_t now = time(NULL);
112 for(node = myself->subnet_tree->head; node; node = next) {
115 if(s->expires && s->expires < now) {
117 char netstr[MAXNETSTR];
118 if(net2str(netstr, sizeof netstr, s))
119 logger(LOG_INFO, "Subnet %s expired", netstr);
122 for(node2 = connection_tree->head; node2; node2 = node2->next) {
125 send_del_subnet(c, s);
128 subnet_del(myself, s);
136 event_add(&age_subnets_event, &(struct timeval){10, 0});
139 static void learn_mac(mac_t *address) {
144 subnet = lookup_subnet_mac(myself, address);
146 /* If we don't know this MAC address yet, store it */
149 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
150 address->x[0], address->x[1], address->x[2], address->x[3],
151 address->x[4], address->x[5]);
153 subnet = new_subnet();
154 subnet->type = SUBNET_MAC;
155 subnet->expires = time(NULL) + macexpire;
156 subnet->net.mac.address = *address;
158 subnet_add(myself, subnet);
160 /* And tell all other tinc daemons it's our MAC */
162 for(node = connection_tree->head; node; node = node->next) {
165 send_add_subnet(c, subnet);
168 if(!timeout_initialized(&age_subnets_event))
169 timeout_set(&age_subnets_event, age_subnets, NULL);
170 event_add(&age_subnets_event, &(struct timeval){10, 0});
173 subnet->expires = time(NULL) + macexpire;
179 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
181 struct icmp icmp = {0};
183 struct in_addr ip_src;
184 struct in_addr ip_dst;
190 /* Swap Ethernet source and destination addresses */
192 swap_mac_addresses(packet);
194 /* Copy headers from packet into properly aligned structs on the stack */
196 memcpy(&ip, packet->data + ether_size, ip_size);
198 /* Remember original source and destination */
203 oldlen = packet->len - ether_size;
205 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
206 icmp.icmp_nextmtu = htons(packet->len - ether_size);
208 if(oldlen >= IP_MSS - ip_size - icmp_size)
209 oldlen = IP_MSS - ip_size - icmp_size;
211 /* Copy first part of original contents to ICMP message */
213 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
215 /* Fill in IPv4 header */
218 ip.ip_hl = ip_size / 4;
220 ip.ip_len = htons(ip_size + icmp_size + oldlen);
224 ip.ip_p = IPPROTO_ICMP;
229 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
231 /* Fill in ICMP header */
233 icmp.icmp_type = type;
234 icmp.icmp_code = code;
237 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
238 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
240 /* Copy structs on stack back to packet */
242 memcpy(packet->data + ether_size, &ip, ip_size);
243 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
245 packet->len = ether_size + ip_size + icmp_size + oldlen;
247 send_packet(source, packet);
252 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
254 vpn_packet_t fragment;
255 int len, maxlen, todo;
257 uint16_t ip_off, origf;
259 memcpy(&ip, packet->data + ether_size, ip_size);
260 fragment.priority = packet->priority;
262 if(ip.ip_hl != ip_size / 4)
265 todo = ntohs(ip.ip_len) - ip_size;
267 if(ether_size + ip_size + todo != packet->len) {
268 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%zd)", packet->len, ether_size + ip_size + todo);
272 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
274 offset = packet->data + ether_size + ip_size;
275 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
276 ip_off = ntohs(ip.ip_off);
277 origf = ip_off & ~IP_OFFMASK;
278 ip_off &= IP_OFFMASK;
281 len = todo > maxlen ? maxlen : todo;
282 memcpy(fragment.data + ether_size + ip_size, offset, len);
286 ip.ip_len = htons(ip_size + len);
287 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
289 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
290 memcpy(fragment.data, packet->data, ether_size);
291 memcpy(fragment.data + ether_size, &ip, ip_size);
292 fragment.len = ether_size + ip_size + len;
294 send_packet(dest, &fragment);
300 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
305 memcpy(&dest, &packet->data[30], sizeof dest);
306 subnet = lookup_subnet_ipv4(&dest);
309 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
310 source->name, source->hostname,
316 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
320 if(subnet->owner == source) {
321 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
325 if(!subnet->owner->status.reachable)
326 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
328 if(priorityinheritance)
329 packet->priority = packet->data[15];
331 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
333 if(via && packet->len > via->mtu && via != myself) {
334 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);
335 if(packet->data[20] & 0x40) {
336 packet->len = via->mtu;
337 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
339 fragment_ipv4_packet(via, packet);
345 send_packet(subnet->owner, packet);
348 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
349 if(!checklength(source, packet, ether_size + ip_size))
352 if(((packet->data[30] & 0xf0) == 0xe0) || (
353 packet->data[30] == 255 &&
354 packet->data[31] == 255 &&
355 packet->data[32] == 255 &&
356 packet->data[33] == 255))
357 broadcast_packet(source, packet);
359 route_ipv4_unicast(source, packet);
364 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
366 struct icmp6_hdr icmp6 = {0};
370 struct in6_addr ip6_src; /* source address */
371 struct in6_addr ip6_dst; /* destination address */
379 /* Swap Ethernet source and destination addresses */
381 swap_mac_addresses(packet);
383 /* Copy headers from packet to structs on the stack */
385 memcpy(&ip6, packet->data + ether_size, ip6_size);
387 /* Remember original source and destination */
389 pseudo.ip6_src = ip6.ip6_dst;
390 pseudo.ip6_dst = ip6.ip6_src;
392 pseudo.length = packet->len - ether_size;
394 if(type == ICMP6_PACKET_TOO_BIG)
395 icmp6.icmp6_mtu = htonl(pseudo.length);
397 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
398 pseudo.length = IP_MSS - ip6_size - icmp6_size;
400 /* Copy first part of original contents to ICMP message */
402 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
404 /* Fill in IPv6 header */
406 ip6.ip6_flow = htonl(0x60000000UL);
407 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
408 ip6.ip6_nxt = IPPROTO_ICMPV6;
410 ip6.ip6_src = pseudo.ip6_src;
411 ip6.ip6_dst = pseudo.ip6_dst;
413 /* Fill in ICMP header */
415 icmp6.icmp6_type = type;
416 icmp6.icmp6_code = code;
417 icmp6.icmp6_cksum = 0;
419 /* Create pseudo header */
421 pseudo.length = htonl(icmp6_size + pseudo.length);
422 pseudo.next = htonl(IPPROTO_ICMPV6);
424 /* Generate checksum */
426 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
427 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
428 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
430 icmp6.icmp6_cksum = checksum;
432 /* Copy structs on stack back to packet */
434 memcpy(packet->data + ether_size, &ip6, ip6_size);
435 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
437 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
439 send_packet(source, packet);
442 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 */
503 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
506 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
508 if(source != myself) {
509 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
513 /* Copy headers from packet to structs on the stack */
515 memcpy(&ip6, packet->data + ether_size, ip6_size);
516 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
518 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
520 /* First, snatch the source address from the neighbor solicitation packet */
523 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
525 /* Check if this is a valid neighbor solicitation request */
527 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
528 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
529 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
533 /* Create pseudo header */
535 pseudo.ip6_src = ip6.ip6_src;
536 pseudo.ip6_dst = ip6.ip6_dst;
538 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
540 pseudo.length = htonl(ns_size);
541 pseudo.next = htonl(IPPROTO_ICMPV6);
543 /* Generate checksum */
545 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
546 checksum = inet_checksum(&ns, ns_size, checksum);
548 checksum = inet_checksum(&opt, opt_size, checksum);
549 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
553 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
557 /* Check if the IPv6 address exists on the VPN */
559 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
562 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
563 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
564 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
565 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
566 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
567 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
568 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
569 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
570 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
575 /* Check if it is for our own subnet */
577 if(subnet->owner == myself)
578 return; /* silently ignore */
580 /* Create neighbor advertation reply */
582 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
583 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
585 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
586 ip6.ip6_src = ns.nd_ns_target;
589 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
592 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
593 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
594 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
596 /* Create pseudo header */
598 pseudo.ip6_src = ip6.ip6_src;
599 pseudo.ip6_dst = ip6.ip6_dst;
601 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
603 pseudo.length = htonl(ns_size);
604 pseudo.next = htonl(IPPROTO_ICMPV6);
606 /* Generate checksum */
608 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
609 checksum = inet_checksum(&ns, ns_size, checksum);
611 checksum = inet_checksum(&opt, opt_size, checksum);
612 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
615 ns.nd_ns_hdr.icmp6_cksum = checksum;
617 /* Copy structs on stack back to packet */
619 memcpy(packet->data + ether_size, &ip6, ip6_size);
620 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
622 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
624 send_packet(source, packet);
627 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
628 if(!checklength(source, packet, ether_size + ip6_size))
631 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
632 route_neighborsol(source, packet);
636 if(packet->data[38] == 255)
637 broadcast_packet(source, packet);
639 route_ipv6_unicast(source, packet);
644 static void route_arp(node_t *source, vpn_packet_t *packet) {
645 struct ether_arp arp;
649 if(!checklength(source, packet, ether_size + arp_size))
652 if(source != myself) {
653 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
657 /* First, snatch the source address from the ARP packet */
660 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
662 /* Copy headers from packet to structs on the stack */
664 memcpy(&arp, packet->data + ether_size, arp_size);
666 /* Check if this is a valid ARP request */
668 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
669 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof addr || ntohs(arp.arp_op) != ARPOP_REQUEST) {
670 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
674 /* Check if the IPv4 address exists on the VPN */
676 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
679 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
680 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
685 /* Check if it is for our own subnet */
687 if(subnet->owner == myself)
688 return; /* silently ignore */
690 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
691 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
693 memcpy(&addr, arp.arp_tpa, sizeof addr); /* save protocol addr */
694 memcpy(arp.arp_tpa, arp.arp_spa, sizeof addr); /* swap destination and source protocol address */
695 memcpy(arp.arp_spa, &addr, sizeof addr); /* ... */
697 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
698 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
699 arp.arp_op = htons(ARPOP_REPLY);
701 /* Copy structs on stack back to packet */
703 memcpy(packet->data + ether_size, &arp, arp_size);
705 send_packet(source, packet);
708 static void route_mac(node_t *source, vpn_packet_t *packet) {
712 /* Learn source address */
714 if(source == myself) {
716 memcpy(&src, &packet->data[6], sizeof src);
720 /* Lookup destination address */
722 memcpy(&dest, &packet->data[0], sizeof dest);
723 subnet = lookup_subnet_mac(NULL, &dest);
726 broadcast_packet(source, packet);
730 if(subnet->owner == source) {
731 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
735 // Handle packets larger than PMTU
737 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
739 if(via && packet->len > via->mtu && via != myself) {
740 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);
741 uint16_t type = packet->data[12] << 8 | packet->data[13];
742 if(type == ETH_P_IP) {
743 if(packet->data[20] & 0x40) {
744 packet->len = via->mtu;
745 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
747 fragment_ipv4_packet(via, packet);
750 } else if(type == ETH_P_IPV6) {
751 packet->len = via->mtu;
752 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
757 send_packet(subnet->owner, packet);
760 void route(node_t *source, vpn_packet_t *packet) {
761 if(!checklength(source, packet, ether_size))
764 switch (routing_mode) {
767 uint16_t type = packet->data[12] << 8 | packet->data[13];
771 route_arp(source, packet);
775 route_ipv4(source, packet);
779 route_ipv6(source, packet);
783 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
790 route_mac(source, packet);
794 broadcast_packet(source, packet);