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) {
107 subnet = lookup_subnet_mac(address);
109 /* If we don't know this MAC address yet, store it */
112 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
113 address->x[0], address->x[1], address->x[2], address->x[3],
114 address->x[4], address->x[5]);
116 subnet = new_subnet();
117 subnet->type = SUBNET_MAC;
118 subnet->expires = now + macexpire;
119 subnet->net.mac.address = *address;
120 subnet_add(myself, subnet);
122 /* And tell all other tinc daemons it's our MAC */
124 for(node = connection_tree->head; node; node = node->next) {
127 send_add_subnet(c, subnet);
132 subnet->expires = now + macexpire;
135 void age_subnets(void) {
138 avl_node_t *node, *next, *node2;
140 for(node = myself->subnet_tree->head; node; node = next) {
143 if(s->expires && s->expires < now) {
145 char netstr[MAXNETSTR];
146 if(net2str(netstr, sizeof netstr, s))
147 logger(LOG_INFO, "Subnet %s expired", netstr);
150 for(node2 = connection_tree->head; node2; node2 = node2->next) {
153 send_del_subnet(c, s);
156 subnet_del(myself, s);
163 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
165 struct icmp icmp = {0};
167 struct in_addr ip_src;
168 struct in_addr ip_dst;
174 /* Swap Ethernet source and destination addresses */
176 swap_mac_addresses(packet);
178 /* Copy headers from packet into properly aligned structs on the stack */
180 memcpy(&ip, packet->data + ether_size, ip_size);
182 /* Remember original source and destination */
187 oldlen = packet->len - ether_size;
189 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
190 icmp.icmp_nextmtu = htons(packet->len - ether_size);
192 if(oldlen >= IP_MSS - ip_size - icmp_size)
193 oldlen = IP_MSS - ip_size - icmp_size;
195 /* Copy first part of original contents to ICMP message */
197 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
199 /* Fill in IPv4 header */
202 ip.ip_hl = ip_size / 4;
204 ip.ip_len = htons(ip_size + icmp_size + oldlen);
208 ip.ip_p = IPPROTO_ICMP;
213 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
215 /* Fill in ICMP header */
217 icmp.icmp_type = type;
218 icmp.icmp_code = code;
221 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
222 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
224 /* Copy structs on stack back to packet */
226 memcpy(packet->data + ether_size, &ip, ip_size);
227 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
229 packet->len = ether_size + ip_size + icmp_size + oldlen;
231 send_packet(source, packet);
236 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
238 vpn_packet_t fragment;
239 int len, maxlen, todo;
241 uint16_t ip_off, origf;
243 memcpy(&ip, packet->data + ether_size, ip_size);
244 fragment.priority = packet->priority;
246 if(ip.ip_hl != ip_size / 4)
249 todo = ntohs(ip.ip_len) - ip_size;
251 if(ether_size + ip_size + todo != packet->len) {
252 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%zd)", packet->len, ether_size + ip_size + todo);
256 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
258 offset = packet->data + ether_size + ip_size;
259 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
260 ip_off = ntohs(ip.ip_off);
261 origf = ip_off & ~IP_OFFMASK;
262 ip_off &= IP_OFFMASK;
265 len = todo > maxlen ? maxlen : todo;
266 memcpy(fragment.data + ether_size + ip_size, offset, len);
270 ip.ip_len = htons(ip_size + len);
271 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
273 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
274 memcpy(fragment.data, packet->data, ether_size);
275 memcpy(fragment.data + ether_size, &ip, ip_size);
276 fragment.len = ether_size + ip_size + len;
278 send_packet(dest, &fragment);
284 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
289 memcpy(&dest, &packet->data[30], sizeof dest);
290 subnet = lookup_subnet_ipv4(&dest);
293 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
294 source->name, source->hostname,
300 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
304 if(subnet->owner == source) {
305 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
309 if(!subnet->owner->status.reachable)
310 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
312 if(priorityinheritance)
313 packet->priority = packet->data[15];
315 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
317 if(via && packet->len > via->mtu && via != myself) {
318 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);
319 if(packet->data[20] & 0x40) {
320 packet->len = via->mtu;
321 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
323 fragment_ipv4_packet(via, packet);
329 send_packet(subnet->owner, packet);
332 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
333 if(!checklength(source, packet, ether_size + ip_size))
336 if(((packet->data[30] & 0xf0) == 0xe0) || (
337 packet->data[30] == 255 &&
338 packet->data[31] == 255 &&
339 packet->data[32] == 255 &&
340 packet->data[33] == 255))
341 broadcast_packet(source, packet);
343 route_ipv4_unicast(source, packet);
348 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
350 struct icmp6_hdr icmp6 = {0};
354 struct in6_addr ip6_src; /* source address */
355 struct in6_addr ip6_dst; /* destination address */
363 /* Swap Ethernet source and destination addresses */
365 swap_mac_addresses(packet);
367 /* Copy headers from packet to structs on the stack */
369 memcpy(&ip6, packet->data + ether_size, ip6_size);
371 /* Remember original source and destination */
373 pseudo.ip6_src = ip6.ip6_dst;
374 pseudo.ip6_dst = ip6.ip6_src;
376 pseudo.length = packet->len - ether_size;
378 if(type == ICMP6_PACKET_TOO_BIG)
379 icmp6.icmp6_mtu = htonl(pseudo.length);
381 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
382 pseudo.length = IP_MSS - ip6_size - icmp6_size;
384 /* Copy first part of original contents to ICMP message */
386 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
388 /* Fill in IPv6 header */
390 ip6.ip6_flow = htonl(0x60000000UL);
391 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
392 ip6.ip6_nxt = IPPROTO_ICMPV6;
394 ip6.ip6_src = pseudo.ip6_src;
395 ip6.ip6_dst = pseudo.ip6_dst;
397 /* Fill in ICMP header */
399 icmp6.icmp6_type = type;
400 icmp6.icmp6_code = code;
401 icmp6.icmp6_cksum = 0;
403 /* Create pseudo header */
405 pseudo.length = htonl(icmp6_size + pseudo.length);
406 pseudo.next = htonl(IPPROTO_ICMPV6);
408 /* Generate checksum */
410 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
411 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
412 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
414 icmp6.icmp6_cksum = checksum;
416 /* Copy structs on stack back to packet */
418 memcpy(packet->data + ether_size, &ip6, ip6_size);
419 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
421 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
423 send_packet(source, packet);
426 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
431 memcpy(&dest, &packet->data[38], sizeof dest);
432 subnet = lookup_subnet_ipv6(&dest);
435 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
436 source->name, source->hostname,
446 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
450 if(subnet->owner == source) {
451 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
455 if(!subnet->owner->status.reachable)
456 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
458 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
460 if(via && packet->len > via->mtu && via != myself) {
461 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);
462 packet->len = via->mtu;
463 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
467 send_packet(subnet->owner, packet);
472 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
474 struct nd_neighbor_solicit ns;
475 struct nd_opt_hdr opt;
481 struct in6_addr ip6_src; /* source address */
482 struct in6_addr ip6_dst; /* destination address */
487 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
490 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
492 if(source != myself) {
493 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
497 /* Copy headers from packet to structs on the stack */
499 memcpy(&ip6, packet->data + ether_size, ip6_size);
500 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
502 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
504 /* First, snatch the source address from the neighbor solicitation packet */
507 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
509 /* Check if this is a valid neighbor solicitation request */
511 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
512 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
513 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
517 /* Create pseudo header */
519 pseudo.ip6_src = ip6.ip6_src;
520 pseudo.ip6_dst = ip6.ip6_dst;
522 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
524 pseudo.length = htonl(ns_size);
525 pseudo.next = htonl(IPPROTO_ICMPV6);
527 /* Generate checksum */
529 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
530 checksum = inet_checksum(&ns, ns_size, checksum);
532 checksum = inet_checksum(&opt, opt_size, checksum);
533 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
537 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
541 /* Check if the IPv6 address exists on the VPN */
543 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
546 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
547 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
548 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
549 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
550 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
551 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
552 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
553 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
554 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
559 /* Check if it is for our own subnet */
561 if(subnet->owner == myself)
562 return; /* silently ignore */
564 /* Create neighbor advertation reply */
566 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
567 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
569 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
570 ip6.ip6_src = ns.nd_ns_target;
573 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
576 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
577 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
578 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
580 /* Create pseudo header */
582 pseudo.ip6_src = ip6.ip6_src;
583 pseudo.ip6_dst = ip6.ip6_dst;
585 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
587 pseudo.length = htonl(ns_size);
588 pseudo.next = htonl(IPPROTO_ICMPV6);
590 /* Generate checksum */
592 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
593 checksum = inet_checksum(&ns, ns_size, checksum);
595 checksum = inet_checksum(&opt, opt_size, checksum);
596 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
599 ns.nd_ns_hdr.icmp6_cksum = checksum;
601 /* Copy structs on stack back to packet */
603 memcpy(packet->data + ether_size, &ip6, ip6_size);
604 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
606 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
608 send_packet(source, packet);
611 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
612 if(!checklength(source, packet, ether_size + ip6_size))
615 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
616 route_neighborsol(source, packet);
620 if(packet->data[38] == 255)
621 broadcast_packet(source, packet);
623 route_ipv6_unicast(source, packet);
628 static void route_arp(node_t *source, vpn_packet_t *packet) {
629 struct ether_arp arp;
633 if(!checklength(source, packet, ether_size + arp_size))
636 if(source != myself) {
637 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
641 /* First, snatch the source address from the ARP packet */
644 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
646 /* Copy headers from packet to structs on the stack */
648 memcpy(&arp, packet->data + ether_size, arp_size);
650 /* Check if this is a valid ARP request */
652 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
653 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
654 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
658 /* Check if the IPv4 address exists on the VPN */
660 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
663 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
664 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
669 /* Check if it is for our own subnet */
671 if(subnet->owner == myself)
672 return; /* silently ignore */
674 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
675 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
677 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
678 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
679 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
681 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
682 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
683 arp.arp_op = htons(ARPOP_REPLY);
685 /* Copy structs on stack back to packet */
687 memcpy(packet->data + ether_size, &arp, arp_size);
689 send_packet(source, packet);
692 static void route_mac(node_t *source, vpn_packet_t *packet) {
696 /* Learn source address */
698 if(source == myself) {
700 memcpy(&src, &packet->data[6], sizeof src);
704 /* Lookup destination address */
706 memcpy(&dest, &packet->data[0], sizeof dest);
707 subnet = lookup_subnet_mac(&dest);
710 broadcast_packet(source, packet);
714 if(subnet->owner == source) {
715 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
719 // Handle packets larger than PMTU
721 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
723 if(via && packet->len > via->mtu && via != myself) {
724 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);
725 uint16_t type = packet->data[12] << 8 | packet->data[13];
726 if(type == ETH_P_IP) {
727 if(packet->data[20] & 0x40) {
728 packet->len = via->mtu;
729 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
731 fragment_ipv4_packet(via, packet);
734 } else if(type == ETH_P_IPV6) {
735 packet->len = via->mtu;
736 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
741 send_packet(subnet->owner, packet);
744 void route(node_t *source, vpn_packet_t *packet) {
745 if(!checklength(source, packet, ether_size))
748 switch (routing_mode) {
751 uint16_t type = packet->data[12] << 8 | packet->data[13];
755 route_arp(source, packet);
759 route_ipv4(source, packet);
763 route_ipv6(source, packet);
767 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
774 route_mac(source, packet);
778 broadcast_packet(source, packet);