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;
121 subnet_add(myself, subnet);
123 /* And tell all other tinc daemons it's our MAC */
125 for(node = connection_tree->head; node; node = node->next) {
128 send_add_subnet(c, subnet);
133 subnet->expires = now + macexpire;
136 void age_subnets(void) {
139 avl_node_t *node, *next, *node2;
141 for(node = myself->subnet_tree->head; node; node = next) {
144 if(s->expires && s->expires < now) {
146 char netstr[MAXNETSTR];
147 if(net2str(netstr, sizeof netstr, s))
148 logger(LOG_INFO, "Subnet %s expired", netstr);
151 for(node2 = connection_tree->head; node2; node2 = node2->next) {
154 send_del_subnet(c, s);
157 subnet_del(myself, s);
164 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
166 struct icmp icmp = {0};
168 struct in_addr ip_src;
169 struct in_addr ip_dst;
175 /* Swap Ethernet source and destination addresses */
177 swap_mac_addresses(packet);
179 /* Copy headers from packet into properly aligned structs on the stack */
181 memcpy(&ip, packet->data + ether_size, ip_size);
183 /* Remember original source and destination */
188 oldlen = packet->len - ether_size;
190 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
191 icmp.icmp_nextmtu = htons(packet->len - ether_size);
193 if(oldlen >= IP_MSS - ip_size - icmp_size)
194 oldlen = IP_MSS - ip_size - icmp_size;
196 /* Copy first part of original contents to ICMP message */
198 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
200 /* Fill in IPv4 header */
203 ip.ip_hl = ip_size / 4;
205 ip.ip_len = htons(ip_size + icmp_size + oldlen);
209 ip.ip_p = IPPROTO_ICMP;
214 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
216 /* Fill in ICMP header */
218 icmp.icmp_type = type;
219 icmp.icmp_code = code;
222 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
223 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
225 /* Copy structs on stack back to packet */
227 memcpy(packet->data + ether_size, &ip, ip_size);
228 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
230 packet->len = ether_size + ip_size + icmp_size + oldlen;
232 send_packet(source, packet);
237 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
239 vpn_packet_t fragment;
240 int len, maxlen, todo;
242 uint16_t ip_off, origf;
244 memcpy(&ip, packet->data + ether_size, ip_size);
245 fragment.priority = packet->priority;
247 if(ip.ip_hl != ip_size / 4)
250 todo = ntohs(ip.ip_len) - ip_size;
252 if(ether_size + ip_size + todo != packet->len) {
253 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%zd)", packet->len, ether_size + ip_size + todo);
257 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
259 offset = packet->data + ether_size + ip_size;
260 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
261 ip_off = ntohs(ip.ip_off);
262 origf = ip_off & ~IP_OFFMASK;
263 ip_off &= IP_OFFMASK;
266 len = todo > maxlen ? maxlen : todo;
267 memcpy(fragment.data + ether_size + ip_size, offset, len);
271 ip.ip_len = htons(ip_size + len);
272 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
274 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
275 memcpy(fragment.data, packet->data, ether_size);
276 memcpy(fragment.data + ether_size, &ip, ip_size);
277 fragment.len = ether_size + ip_size + len;
279 send_packet(dest, &fragment);
285 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
290 memcpy(&dest, &packet->data[30], sizeof dest);
291 subnet = lookup_subnet_ipv4(&dest);
294 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
295 source->name, source->hostname,
301 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
305 if(subnet->owner == source) {
306 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
310 if(!subnet->owner->status.reachable)
311 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
313 if(priorityinheritance)
314 packet->priority = packet->data[15];
316 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
318 if(via && packet->len > via->mtu && via != myself) {
319 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);
320 if(packet->data[20] & 0x40) {
321 packet->len = via->mtu;
322 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
324 fragment_ipv4_packet(via, packet);
330 send_packet(subnet->owner, packet);
333 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
334 if(!checklength(source, packet, ether_size + ip_size))
337 if(((packet->data[30] & 0xf0) == 0xe0) || (
338 packet->data[30] == 255 &&
339 packet->data[31] == 255 &&
340 packet->data[32] == 255 &&
341 packet->data[33] == 255))
342 broadcast_packet(source, packet);
344 route_ipv4_unicast(source, packet);
349 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
351 struct icmp6_hdr icmp6 = {0};
355 struct in6_addr ip6_src; /* source address */
356 struct in6_addr ip6_dst; /* destination address */
364 /* Swap Ethernet source and destination addresses */
366 swap_mac_addresses(packet);
368 /* Copy headers from packet to structs on the stack */
370 memcpy(&ip6, packet->data + ether_size, ip6_size);
372 /* Remember original source and destination */
374 pseudo.ip6_src = ip6.ip6_dst;
375 pseudo.ip6_dst = ip6.ip6_src;
377 pseudo.length = packet->len - ether_size;
379 if(type == ICMP6_PACKET_TOO_BIG)
380 icmp6.icmp6_mtu = htonl(pseudo.length);
382 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
383 pseudo.length = IP_MSS - ip6_size - icmp6_size;
385 /* Copy first part of original contents to ICMP message */
387 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
389 /* Fill in IPv6 header */
391 ip6.ip6_flow = htonl(0x60000000UL);
392 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
393 ip6.ip6_nxt = IPPROTO_ICMPV6;
395 ip6.ip6_src = pseudo.ip6_src;
396 ip6.ip6_dst = pseudo.ip6_dst;
398 /* Fill in ICMP header */
400 icmp6.icmp6_type = type;
401 icmp6.icmp6_code = code;
402 icmp6.icmp6_cksum = 0;
404 /* Create pseudo header */
406 pseudo.length = htonl(icmp6_size + pseudo.length);
407 pseudo.next = htonl(IPPROTO_ICMPV6);
409 /* Generate checksum */
411 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
412 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
413 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
415 icmp6.icmp6_cksum = checksum;
417 /* Copy structs on stack back to packet */
419 memcpy(packet->data + ether_size, &ip6, ip6_size);
420 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
422 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
424 send_packet(source, packet);
427 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
432 memcpy(&dest, &packet->data[38], sizeof dest);
433 subnet = lookup_subnet_ipv6(&dest);
436 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
437 source->name, source->hostname,
447 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
451 if(subnet->owner == source) {
452 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
456 if(!subnet->owner->status.reachable)
457 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
459 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
461 if(via && packet->len > via->mtu && via != myself) {
462 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);
463 packet->len = via->mtu;
464 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
468 send_packet(subnet->owner, packet);
473 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
475 struct nd_neighbor_solicit ns;
476 struct nd_opt_hdr opt;
482 struct in6_addr ip6_src; /* source address */
483 struct in6_addr ip6_dst; /* destination address */
488 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
491 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
493 if(source != myself) {
494 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
498 /* Copy headers from packet to structs on the stack */
500 memcpy(&ip6, packet->data + ether_size, ip6_size);
501 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
503 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
505 /* First, snatch the source address from the neighbor solicitation packet */
508 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
510 /* Check if this is a valid neighbor solicitation request */
512 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
513 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
514 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
518 /* Create pseudo header */
520 pseudo.ip6_src = ip6.ip6_src;
521 pseudo.ip6_dst = ip6.ip6_dst;
523 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
525 pseudo.length = htonl(ns_size);
526 pseudo.next = htonl(IPPROTO_ICMPV6);
528 /* Generate checksum */
530 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
531 checksum = inet_checksum(&ns, ns_size, checksum);
533 checksum = inet_checksum(&opt, opt_size, checksum);
534 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
538 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
542 /* Check if the IPv6 address exists on the VPN */
544 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
547 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
548 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
549 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
550 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
551 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
552 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
553 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
554 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
555 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
560 /* Check if it is for our own subnet */
562 if(subnet->owner == myself)
563 return; /* silently ignore */
565 /* Create neighbor advertation reply */
567 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
568 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
570 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
571 ip6.ip6_src = ns.nd_ns_target;
574 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
577 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
578 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
579 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
581 /* Create pseudo header */
583 pseudo.ip6_src = ip6.ip6_src;
584 pseudo.ip6_dst = ip6.ip6_dst;
586 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
588 pseudo.length = htonl(ns_size);
589 pseudo.next = htonl(IPPROTO_ICMPV6);
591 /* Generate checksum */
593 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
594 checksum = inet_checksum(&ns, ns_size, checksum);
596 checksum = inet_checksum(&opt, opt_size, checksum);
597 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
600 ns.nd_ns_hdr.icmp6_cksum = checksum;
602 /* Copy structs on stack back to packet */
604 memcpy(packet->data + ether_size, &ip6, ip6_size);
605 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
607 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
609 send_packet(source, packet);
612 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
613 if(!checklength(source, packet, ether_size + ip6_size))
616 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
617 route_neighborsol(source, packet);
621 if(packet->data[38] == 255)
622 broadcast_packet(source, packet);
624 route_ipv6_unicast(source, packet);
629 static void route_arp(node_t *source, vpn_packet_t *packet) {
630 struct ether_arp arp;
634 if(!checklength(source, packet, ether_size + arp_size))
637 if(source != myself) {
638 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
642 /* First, snatch the source address from the ARP packet */
645 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
647 /* Copy headers from packet to structs on the stack */
649 memcpy(&arp, packet->data + ether_size, arp_size);
651 /* Check if this is a valid ARP request */
653 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
654 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
655 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
659 /* Check if the IPv4 address exists on the VPN */
661 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
664 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
665 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
670 /* Check if it is for our own subnet */
672 if(subnet->owner == myself)
673 return; /* silently ignore */
675 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
676 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
678 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
679 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
680 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
682 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
683 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
684 arp.arp_op = htons(ARPOP_REPLY);
686 /* Copy structs on stack back to packet */
688 memcpy(packet->data + ether_size, &arp, arp_size);
690 send_packet(source, packet);
693 static void route_mac(node_t *source, vpn_packet_t *packet) {
697 /* Learn source address */
699 if(source == myself) {
701 memcpy(&src, &packet->data[6], sizeof src);
705 /* Lookup destination address */
707 memcpy(&dest, &packet->data[0], sizeof dest);
708 subnet = lookup_subnet_mac(&dest);
711 broadcast_packet(source, packet);
715 if(subnet->owner == source) {
716 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
720 // Handle packets larger than PMTU
722 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
724 if(via && packet->len > via->mtu && via != myself) {
725 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);
726 uint16_t type = packet->data[12] << 8 | packet->data[13];
727 if(type == ETH_P_IP) {
728 if(packet->data[20] & 0x40) {
729 packet->len = via->mtu;
730 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
732 fragment_ipv4_packet(via, packet);
735 } else if(type == ETH_P_IPV6) {
736 packet->len = via->mtu;
737 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
742 send_packet(subnet->owner, packet);
745 void route(node_t *source, vpn_packet_t *packet) {
746 if(!checklength(source, packet, ether_size))
749 switch (routing_mode) {
752 uint16_t type = packet->data[12] << 8 | packet->data[13];
756 route_arp(source, packet);
760 route_ipv4(source, packet);
764 route_ipv6(source, packet);
768 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
775 route_mac(source, packet);
779 broadcast_packet(source, packet);