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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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)
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)
111 subnet = lookup_subnet_mac(address);
113 /* If we don't know this MAC address yet, store it */
116 ifdebug(TRAFFIC) logger(LOG_INFO, _("Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx"),
117 address->x[0], address->x[1], address->x[2], address->x[3],
118 address->x[4], address->x[5]);
120 subnet = new_subnet();
121 subnet->type = SUBNET_MAC;
122 subnet->expires = now + macexpire;
123 subnet->net.mac.address = *address;
124 subnet_add(myself, subnet);
126 /* And tell all other tinc daemons it's our MAC */
128 for(node = connection_tree->head; node; node = node->next) {
131 send_add_subnet(c, subnet);
136 subnet->expires = now + macexpire;
139 void age_subnets(void)
143 avl_node_t *node, *next, *node2;
147 for(node = myself->subnet_tree->head; node; node = next) {
150 if(s->expires && s->expires < now) {
152 char netstr[MAXNETSTR];
153 if(net2str(netstr, sizeof netstr, s))
154 logger(LOG_INFO, _("Subnet %s expired"), netstr);
157 for(node2 = connection_tree->head; node2; node2 = node2->next) {
160 send_del_subnet(c, s);
163 subnet_del(myself, s);
170 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code)
173 struct icmp icmp = {0};
175 struct in_addr ip_src;
176 struct in_addr ip_dst;
184 /* Swap Ethernet source and destination addresses */
186 swap_mac_addresses(packet);
188 /* Copy headers from packet into properly aligned structs on the stack */
190 memcpy(&ip, packet->data + ether_size, ip_size);
192 /* Remember original source and destination */
197 oldlen = packet->len - ether_size;
199 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
200 icmp.icmp_nextmtu = htons(packet->len - ether_size);
202 if(oldlen >= IP_MSS - ip_size - icmp_size)
203 oldlen = IP_MSS - ip_size - icmp_size;
205 /* Copy first part of original contents to ICMP message */
207 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
209 /* Fill in IPv4 header */
212 ip.ip_hl = ip_size / 4;
214 ip.ip_len = htons(ip_size + icmp_size + oldlen);
218 ip.ip_p = IPPROTO_ICMP;
223 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
225 /* Fill in ICMP header */
227 icmp.icmp_type = type;
228 icmp.icmp_code = code;
231 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
232 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
234 /* Copy structs on stack back to packet */
236 memcpy(packet->data + ether_size, &ip, ip_size);
237 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
239 packet->len = ether_size + ip_size + icmp_size + oldlen;
241 send_packet(source, packet);
246 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
248 vpn_packet_t fragment;
249 int len, maxlen, todo;
251 uint16_t ip_off, origf;
255 memcpy(&ip, packet->data + ether_size, ip_size);
256 fragment.priority = packet->priority;
258 if(ip.ip_hl != ip_size / 4)
261 todo = ntohs(ip.ip_len) - ip_size;
263 if(ether_size + ip_size + todo != packet->len) {
264 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Length of packet (%d) doesn't match length in IPv4 header (%zd)"), packet->len, ether_size + ip_size + todo);
268 ifdebug(TRAFFIC) logger(LOG_INFO, _("Fragmenting packet of %d bytes to %s (%s)"), packet->len, dest->name, dest->hostname);
270 offset = packet->data + ether_size + ip_size;
271 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
272 ip_off = ntohs(ip.ip_off);
273 origf = ip_off & ~IP_OFFMASK;
274 ip_off &= IP_OFFMASK;
277 len = todo > maxlen ? maxlen : todo;
278 memcpy(fragment.data + ether_size + ip_size, offset, len);
282 ip.ip_len = htons(ip_size + len);
283 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
285 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
286 memcpy(fragment.data, packet->data, ether_size);
287 memcpy(fragment.data + ether_size, &ip, ip_size);
288 fragment.len = ether_size + ip_size + len;
290 send_packet(dest, &fragment);
296 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet)
304 memcpy(&dest, &packet->data[30], sizeof dest);
305 subnet = lookup_subnet_ipv4(&dest);
308 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d"),
309 source->name, source->hostname,
315 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
319 if(subnet->owner == source) {
320 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
324 if(!subnet->owner->status.reachable)
325 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
327 if(priorityinheritance)
328 packet->priority = packet->data[15];
330 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
332 if(via && packet->len > via->mtu && via != myself) {
333 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);
334 if(packet->data[20] & 0x40) {
335 packet->len = via->mtu;
336 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
338 fragment_ipv4_packet(via, packet);
344 send_packet(subnet->owner, packet);
347 static void route_ipv4(node_t *source, vpn_packet_t *packet)
351 if(!checklength(source, packet, ether_size + ip_size))
354 if(((packet->data[30] & 0xf0) == 0xe0) || (
355 packet->data[30] == 255 &&
356 packet->data[31] == 255 &&
357 packet->data[32] == 255 &&
358 packet->data[33] == 255))
359 broadcast_packet(source, packet);
361 route_ipv4_unicast(source, packet);
366 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code)
369 struct icmp6_hdr icmp6 = {0};
373 struct in6_addr ip6_src; /* source address */
374 struct in6_addr ip6_dst; /* destination address */
384 /* Swap Ethernet source and destination addresses */
386 swap_mac_addresses(packet);
388 /* Copy headers from packet to structs on the stack */
390 memcpy(&ip6, packet->data + ether_size, ip6_size);
392 /* Remember original source and destination */
394 pseudo.ip6_src = ip6.ip6_dst;
395 pseudo.ip6_dst = ip6.ip6_src;
397 pseudo.length = packet->len - ether_size;
399 if(type == ICMP6_PACKET_TOO_BIG)
400 icmp6.icmp6_mtu = htonl(pseudo.length);
402 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
403 pseudo.length = IP_MSS - ip6_size - icmp6_size;
405 /* Copy first part of original contents to ICMP message */
407 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
409 /* Fill in IPv6 header */
411 ip6.ip6_flow = htonl(0x60000000UL);
412 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
413 ip6.ip6_nxt = IPPROTO_ICMPV6;
415 ip6.ip6_src = pseudo.ip6_src;
416 ip6.ip6_dst = pseudo.ip6_dst;
418 /* Fill in ICMP header */
420 icmp6.icmp6_type = type;
421 icmp6.icmp6_code = code;
422 icmp6.icmp6_cksum = 0;
424 /* Create pseudo header */
426 pseudo.length = htonl(icmp6_size + pseudo.length);
427 pseudo.next = htonl(IPPROTO_ICMPV6);
429 /* Generate checksum */
431 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
432 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
433 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
435 icmp6.icmp6_cksum = checksum;
437 /* Copy structs on stack back to packet */
439 memcpy(packet->data + ether_size, &ip6, ip6_size);
440 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
442 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
444 send_packet(source, packet);
447 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet)
455 memcpy(&dest, &packet->data[38], sizeof dest);
456 subnet = lookup_subnet_ipv6(&dest);
459 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
460 source->name, source->hostname,
470 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
474 if(subnet->owner == source) {
475 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
479 if(!subnet->owner->status.reachable)
480 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
482 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
484 if(via && packet->len > via->mtu && via != myself) {
485 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);
486 packet->len = via->mtu;
487 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
491 send_packet(subnet->owner, packet);
496 static void route_neighborsol(node_t *source, vpn_packet_t *packet)
499 struct nd_neighbor_solicit ns;
500 struct nd_opt_hdr opt;
506 struct in6_addr ip6_src; /* source address */
507 struct in6_addr ip6_dst; /* destination address */
514 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
517 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
519 if(source != myself) {
520 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got neighbor solicitation request from %s (%s) while in router mode!"), source->name, source->hostname);
524 /* Copy headers from packet to structs on the stack */
526 memcpy(&ip6, packet->data + ether_size, ip6_size);
527 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
529 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
531 /* First, snatch the source address from the neighbor solicitation packet */
534 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
536 /* Check if this is a valid neighbor solicitation request */
538 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
539 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
540 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
544 /* Create pseudo header */
546 pseudo.ip6_src = ip6.ip6_src;
547 pseudo.ip6_dst = ip6.ip6_dst;
549 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
551 pseudo.length = htonl(ns_size);
552 pseudo.next = htonl(IPPROTO_ICMPV6);
554 /* Generate checksum */
556 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
557 checksum = inet_checksum(&ns, ns_size, checksum);
559 checksum = inet_checksum(&opt, opt_size, checksum);
560 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
564 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
568 /* Check if the IPv6 address exists on the VPN */
570 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
573 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
574 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
575 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
576 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
577 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
578 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
579 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
580 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
581 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
586 /* Check if it is for our own subnet */
588 if(subnet->owner == myself)
589 return; /* silently ignore */
591 /* Create neighbor advertation reply */
593 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
594 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
596 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
597 ip6.ip6_src = ns.nd_ns_target;
600 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
603 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
604 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
605 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
607 /* Create pseudo header */
609 pseudo.ip6_src = ip6.ip6_src;
610 pseudo.ip6_dst = ip6.ip6_dst;
612 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
614 pseudo.length = htonl(ns_size);
615 pseudo.next = htonl(IPPROTO_ICMPV6);
617 /* Generate checksum */
619 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
620 checksum = inet_checksum(&ns, ns_size, checksum);
622 checksum = inet_checksum(&opt, opt_size, checksum);
623 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
626 ns.nd_ns_hdr.icmp6_cksum = checksum;
628 /* Copy structs on stack back to packet */
630 memcpy(packet->data + ether_size, &ip6, ip6_size);
631 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
633 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
635 send_packet(source, packet);
638 static void route_ipv6(node_t *source, vpn_packet_t *packet)
642 if(!checklength(source, packet, ether_size + ip6_size))
645 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
646 route_neighborsol(source, packet);
650 if(packet->data[38] == 255)
651 broadcast_packet(source, packet);
653 route_ipv6_unicast(source, packet);
658 static void route_arp(node_t *source, vpn_packet_t *packet)
660 struct ether_arp arp;
666 if(!checklength(source, packet, ether_size + arp_size))
669 if(source != myself) {
670 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got ARP request from %s (%s) while in router mode!"), source->name, source->hostname);
674 /* First, snatch the source address from the ARP packet */
677 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
679 /* Copy headers from packet to structs on the stack */
681 memcpy(&arp, packet->data + ether_size, arp_size);
683 /* Check if this is a valid ARP request */
685 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
686 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
687 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
691 /* Check if the IPv4 address exists on the VPN */
693 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
696 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
697 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
702 /* Check if it is for our own subnet */
704 if(subnet->owner == myself)
705 return; /* silently ignore */
707 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
708 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
710 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
711 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
712 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
714 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
715 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
716 arp.arp_op = htons(ARPOP_REPLY);
718 /* Copy structs on stack back to packet */
720 memcpy(packet->data + ether_size, &arp, arp_size);
722 send_packet(source, packet);
725 static void route_mac(node_t *source, vpn_packet_t *packet)
733 /* Learn source address */
735 if(source == myself) {
737 memcpy(&src, &packet->data[6], sizeof src);
741 /* Lookup destination address */
743 memcpy(&dest, &packet->data[0], sizeof dest);
744 subnet = lookup_subnet_mac(&dest);
747 broadcast_packet(source, packet);
751 if(subnet->owner == source) {
752 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
756 // Handle packets larger than PMTU
758 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
760 if(via && packet->len > via->mtu && via != myself) {
761 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);
762 uint16_t type = packet->data[12] << 8 | packet->data[13];
763 if(type == ETH_P_IP) {
764 if(packet->data[20] & 0x40) {
765 packet->len = via->mtu;
766 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
768 fragment_ipv4_packet(via, packet);
771 } else if(type == ETH_P_IPV6) {
772 packet->len = via->mtu;
773 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
778 send_packet(subnet->owner, packet);
782 void route(node_t *source, vpn_packet_t *packet)
786 if(!checklength(source, packet, ether_size))
789 switch (routing_mode) {
792 uint16_t type = packet->data[12] << 8 | packet->data[13];
796 route_arp(source, packet);
800 route_ipv4(source, packet);
804 route_ipv6(source, packet);
808 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown type %hx"), source->name, source->hostname, type);
815 route_mac(source, packet);
819 broadcast_packet(source, packet);