3 Copyright (C) 2000-2005 Ivo Timmermans,
4 2000-2014 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 fmode_t forwarding_mode = FMODE_INTERNAL;
37 bmode_t broadcast_mode = BMODE_MST;
38 bool decrement_ttl = false;
39 bool directonly = false;
40 bool priorityinheritance = false;
42 bool overwrite_mac = false;
43 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
45 /* Sizes of various headers */
47 static const size_t ether_size = sizeof(struct ether_header);
48 static const size_t arp_size = sizeof(struct ether_arp);
49 static const size_t ip_size = sizeof(struct ip);
50 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
51 static const size_t ip6_size = sizeof(struct ip6_hdr);
52 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
53 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
54 static const size_t opt_size = sizeof(struct nd_opt_hdr);
57 #define MAX(a, b) ((a) > (b) ? (a) : (b))
62 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
64 uint32_t checksum = prevsum ^ 0xFFFF;
72 checksum += *(uint8_t *)p;
75 checksum = (checksum & 0xFFFF) + (checksum >> 16);
80 static bool ratelimit(int frequency) {
81 static time_t lasttime = 0;
85 if(count >= frequency)
96 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
97 if(packet->len < length) {
98 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
104 static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
105 if(!source || !via || !(via->options & OPTION_CLAMP_MSS))
108 uint16_t mtu = source->mtu;
109 if(via != myself && via->mtu < mtu)
112 /* Find TCP header */
113 int start = ether_size;
114 uint16_t type = packet->data[12] << 8 | packet->data[13];
116 if(type == ETH_P_8021Q) {
118 type = packet->data[16] << 8 | packet->data[17];
121 if(type == ETH_P_IP && packet->data[start + 9] == 6)
122 start += (packet->data[start] & 0xf) * 4;
123 else if(type == ETH_P_IPV6 && packet->data[start + 6] == 6)
128 if(packet->len <= start + 20)
131 /* Use data offset field to calculate length of options field */
132 int len = ((packet->data[start + 12] >> 4) - 5) * 4;
134 if(packet->len < start + 20 + len)
137 /* Search for MSS option header */
138 for(int i = 0; i < len;) {
139 if(packet->data[start + 20 + i] == 0)
142 if(packet->data[start + 20 + i] == 1) {
147 if(i > len - 2 || i > len - packet->data[start + 21 + i])
150 if(packet->data[start + 20 + i] != 2) {
151 if(packet->data[start + 21 + i] < 2)
153 i += packet->data[start + 21 + i];
157 if(packet->data[start + 21] != 4)
161 uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
162 uint16_t newmss = mtu - start - 20;
163 uint32_t csum = packet->data[start + 16] << 8 | packet->data[start + 17];
168 ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
170 /* Update the MSS value and the checksum */
171 packet->data[start + 22 + i] = newmss >> 8;
172 packet->data[start + 23 + i] = newmss & 0xff;
174 csum += oldmss ^ 0xffff;
176 csum = (csum & 0xffff) + (csum >> 16);
179 packet->data[start + 16] = csum >> 8;
180 packet->data[start + 17] = csum;
185 static void swap_mac_addresses(vpn_packet_t *packet) {
187 memcpy(&tmp, &packet->data[0], sizeof tmp);
188 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
189 memcpy(&packet->data[6], &tmp, sizeof tmp);
192 static void learn_mac(mac_t *address) {
197 subnet = lookup_subnet_mac(myself, address);
199 /* If we don't know this MAC address yet, store it */
202 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %x:%x:%x:%x:%x:%x",
203 address->x[0], address->x[1], address->x[2], address->x[3],
204 address->x[4], address->x[5]);
206 subnet = new_subnet();
207 subnet->type = SUBNET_MAC;
208 subnet->expires = now + macexpire;
209 subnet->net.mac.address = *address;
211 subnet_add(myself, subnet);
212 subnet_update(myself, subnet, true);
214 /* And tell all other tinc daemons it's our MAC */
216 for(node = connection_tree->head; node; node = node->next) {
219 send_add_subnet(c, subnet);
224 subnet->expires = now + macexpire;
227 void age_subnets(void) {
230 avl_node_t *node, *next, *node2;
232 for(node = myself->subnet_tree->head; node; node = next) {
235 if(s->expires && s->expires <= now) {
237 char netstr[MAXNETSTR];
238 if(net2str(netstr, sizeof netstr, s))
239 logger(LOG_INFO, "Subnet %s expired", netstr);
242 for(node2 = connection_tree->head; node2; node2 = node2->next) {
245 send_del_subnet(c, s);
248 subnet_update(myself, s, false);
249 subnet_del(myself, s);
256 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
258 struct icmp icmp = {0};
260 struct in_addr ip_src;
261 struct in_addr ip_dst;
267 /* Swap Ethernet source and destination addresses */
269 swap_mac_addresses(packet);
271 /* Copy headers from packet into properly aligned structs on the stack */
273 memcpy(&ip, packet->data + ether_size, ip_size);
275 /* Remember original source and destination */
280 /* Try to reply with an IP address assigned to the local machine */
282 if (type == ICMP_TIME_EXCEEDED && code == ICMP_EXC_TTL) {
283 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
285 struct sockaddr_in addr;
286 memset(&addr, 0, sizeof(addr));
287 addr.sin_family = AF_INET;
288 addr.sin_addr = ip.ip_src;
289 if (!connect(sockfd, (const struct sockaddr*) &addr, sizeof(addr))) {
290 memset(&addr, 0, sizeof(addr));
291 addr.sin_family = AF_INET;
292 socklen_t addrlen = sizeof(addr);
293 if (!getsockname(sockfd, (struct sockaddr*) &addr, &addrlen) && addrlen <= sizeof(addr)) {
294 ip_dst = addr.sin_addr;
301 oldlen = packet->len - ether_size;
303 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
304 icmp.icmp_nextmtu = htons(packet->len - ether_size);
306 if(oldlen >= IP_MSS - ip_size - icmp_size)
307 oldlen = IP_MSS - ip_size - icmp_size;
309 /* Copy first part of original contents to ICMP message */
311 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
313 /* Fill in IPv4 header */
316 ip.ip_hl = ip_size / 4;
318 ip.ip_len = htons(ip_size + icmp_size + oldlen);
322 ip.ip_p = IPPROTO_ICMP;
327 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
329 /* Fill in ICMP header */
331 icmp.icmp_type = type;
332 icmp.icmp_code = code;
335 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
336 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
338 /* Copy structs on stack back to packet */
340 memcpy(packet->data + ether_size, &ip, ip_size);
341 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
343 packet->len = ether_size + ip_size + icmp_size + oldlen;
345 send_packet(source, packet);
350 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t ether_size) {
352 vpn_packet_t fragment;
353 int len, maxlen, todo;
355 uint16_t ip_off, origf;
357 memcpy(&ip, packet->data + ether_size, ip_size);
358 fragment.priority = packet->priority;
360 if(ip.ip_hl != ip_size / 4)
363 todo = ntohs(ip.ip_len) - ip_size;
365 if(ether_size + ip_size + todo != packet->len) {
366 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%d)", packet->len, (int)(ether_size + ip_size + todo));
370 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
372 offset = packet->data + ether_size + ip_size;
373 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
374 ip_off = ntohs(ip.ip_off);
375 origf = ip_off & ~IP_OFFMASK;
376 ip_off &= IP_OFFMASK;
379 len = todo > maxlen ? maxlen : todo;
380 memcpy(fragment.data + ether_size + ip_size, offset, len);
384 ip.ip_len = htons(ip_size + len);
385 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
387 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
388 memcpy(fragment.data, packet->data, ether_size);
389 memcpy(fragment.data + ether_size, &ip, ip_size);
390 fragment.len = ether_size + ip_size + len;
392 send_packet(dest, &fragment);
398 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
403 memcpy(&dest, &packet->data[30], sizeof dest);
404 subnet = lookup_subnet_ipv4(&dest);
407 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
408 source->name, source->hostname,
414 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
418 if(subnet->owner == source) {
419 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
423 if(!subnet->owner->status.reachable)
424 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
426 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
427 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
429 if(priorityinheritance)
430 packet->priority = packet->data[15];
432 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
435 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
439 if(directonly && subnet->owner != via)
440 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
442 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
443 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);
444 if(packet->data[20] & 0x40) {
445 packet->len = MAX(via->mtu, 590);
446 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
448 fragment_ipv4_packet(via, packet, ether_size);
454 clamp_mss(source, via, packet);
456 send_packet(subnet->owner, packet);
459 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
460 if(!checklength(source, packet, ether_size + ip_size))
463 if(broadcast_mode && (((packet->data[30] & 0xf0) == 0xe0) || (
464 packet->data[30] == 255 &&
465 packet->data[31] == 255 &&
466 packet->data[32] == 255 &&
467 packet->data[33] == 255)))
468 broadcast_packet(source, packet);
470 route_ipv4_unicast(source, packet);
475 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
477 struct icmp6_hdr icmp6 = {0};
481 struct in6_addr ip6_src; /* source address */
482 struct in6_addr ip6_dst; /* destination address */
490 /* Swap Ethernet source and destination addresses */
492 swap_mac_addresses(packet);
494 /* Copy headers from packet to structs on the stack */
496 memcpy(&ip6, packet->data + ether_size, ip6_size);
498 /* Remember original source and destination */
500 pseudo.ip6_src = ip6.ip6_dst;
501 pseudo.ip6_dst = ip6.ip6_src;
503 /* Try to reply with an IP address assigned to the local machine */
505 if (type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT) {
506 int sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
508 struct sockaddr_in6 addr;
509 memset(&addr, 0, sizeof(addr));
510 addr.sin6_family = AF_INET6;
511 addr.sin6_addr = ip6.ip6_src;
512 if (!connect(sockfd, (const struct sockaddr*) &addr, sizeof(addr))) {
513 memset(&addr, 0, sizeof(addr));
514 addr.sin6_family = AF_INET6;
515 socklen_t addrlen = sizeof(addr);
516 if (!getsockname(sockfd, (struct sockaddr*) &addr, &addrlen) && addrlen <= sizeof(addr)) {
517 pseudo.ip6_src = addr.sin6_addr;
524 pseudo.length = packet->len - ether_size;
526 if(type == ICMP6_PACKET_TOO_BIG)
527 icmp6.icmp6_mtu = htonl(pseudo.length);
529 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
530 pseudo.length = IP_MSS - ip6_size - icmp6_size;
532 /* Copy first part of original contents to ICMP message */
534 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
536 /* Fill in IPv6 header */
538 ip6.ip6_flow = htonl(0x60000000UL);
539 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
540 ip6.ip6_nxt = IPPROTO_ICMPV6;
542 ip6.ip6_src = pseudo.ip6_src;
543 ip6.ip6_dst = pseudo.ip6_dst;
545 /* Fill in ICMP header */
547 icmp6.icmp6_type = type;
548 icmp6.icmp6_code = code;
549 icmp6.icmp6_cksum = 0;
551 /* Create pseudo header */
553 pseudo.length = htonl(icmp6_size + pseudo.length);
554 pseudo.next = htonl(IPPROTO_ICMPV6);
556 /* Generate checksum */
558 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
559 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
560 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
562 icmp6.icmp6_cksum = checksum;
564 /* Copy structs on stack back to packet */
566 memcpy(packet->data + ether_size, &ip6, ip6_size);
567 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
569 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
571 send_packet(source, packet);
574 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
579 memcpy(&dest, &packet->data[38], sizeof dest);
580 subnet = lookup_subnet_ipv6(&dest);
583 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
584 source->name, source->hostname,
594 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
598 if(subnet->owner == source) {
599 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
603 if(!subnet->owner->status.reachable)
604 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
606 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
607 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
609 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
612 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
616 if(directonly && subnet->owner != via)
617 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
619 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
620 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);
621 packet->len = MAX(via->mtu, 1294);
622 route_ipv6_unreachable(source, packet, ether_size, ICMP6_PACKET_TOO_BIG, 0);
626 clamp_mss(source, via, packet);
628 send_packet(subnet->owner, packet);
633 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
635 struct nd_neighbor_solicit ns;
636 struct nd_opt_hdr opt;
642 struct in6_addr ip6_src; /* source address */
643 struct in6_addr ip6_dst; /* destination address */
648 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
651 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
653 if(source != myself) {
654 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
658 /* Copy headers from packet to structs on the stack */
660 memcpy(&ip6, packet->data + ether_size, ip6_size);
661 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
663 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
665 /* First, snatch the source address from the neighbor solicitation packet */
668 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
670 /* Check if this is a valid neighbor solicitation request */
672 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
673 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
674 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
678 /* Create pseudo header */
680 pseudo.ip6_src = ip6.ip6_src;
681 pseudo.ip6_dst = ip6.ip6_dst;
683 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
685 pseudo.length = htonl(ns_size);
686 pseudo.next = htonl(IPPROTO_ICMPV6);
688 /* Generate checksum */
690 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
691 checksum = inet_checksum(&ns, ns_size, checksum);
693 checksum = inet_checksum(&opt, opt_size, checksum);
694 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
698 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
702 /* Check if the IPv6 address exists on the VPN */
704 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
707 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
708 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
709 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
710 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
711 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
712 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
713 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
714 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
715 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
720 /* Check if it is for our own subnet */
722 if(subnet->owner == myself)
723 return; /* silently ignore */
725 /* Create neighbor advertation reply */
727 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
728 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
730 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
731 ip6.ip6_src = ns.nd_ns_target;
734 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
737 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
738 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
739 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
741 /* Create pseudo header */
743 pseudo.ip6_src = ip6.ip6_src;
744 pseudo.ip6_dst = ip6.ip6_dst;
746 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
748 pseudo.length = htonl(ns_size);
749 pseudo.next = htonl(IPPROTO_ICMPV6);
751 /* Generate checksum */
753 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
754 checksum = inet_checksum(&ns, ns_size, checksum);
756 checksum = inet_checksum(&opt, opt_size, checksum);
757 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
760 ns.nd_ns_hdr.icmp6_cksum = checksum;
762 /* Copy structs on stack back to packet */
764 memcpy(packet->data + ether_size, &ip6, ip6_size);
765 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
767 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
769 send_packet(source, packet);
772 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
773 if(!checklength(source, packet, ether_size + ip6_size))
776 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
777 route_neighborsol(source, packet);
781 if(broadcast_mode && packet->data[38] == 255)
782 broadcast_packet(source, packet);
784 route_ipv6_unicast(source, packet);
789 static void route_arp(node_t *source, vpn_packet_t *packet) {
790 struct ether_arp arp;
794 if(!checklength(source, packet, ether_size + arp_size))
797 if(source != myself) {
798 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
802 /* First, snatch the source address from the ARP packet */
805 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
807 /* Copy headers from packet to structs on the stack */
809 memcpy(&arp, packet->data + ether_size, arp_size);
811 /* Check if this is a valid ARP request */
813 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
814 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
815 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
819 /* Check if the IPv4 address exists on the VPN */
821 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
824 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
825 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
830 /* Check if it is for our own subnet */
832 if(subnet->owner == myself)
833 return; /* silently ignore */
835 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
836 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
838 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
839 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
840 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
842 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
843 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
844 arp.arp_op = htons(ARPOP_REPLY);
846 /* Copy structs on stack back to packet */
848 memcpy(packet->data + ether_size, &arp, arp_size);
850 send_packet(source, packet);
853 static void route_mac(node_t *source, vpn_packet_t *packet) {
857 /* Learn source address */
859 if(source == myself) {
861 memcpy(&src, &packet->data[6], sizeof src);
865 /* Lookup destination address */
867 memcpy(&dest, &packet->data[0], sizeof dest);
868 subnet = lookup_subnet_mac(NULL, &dest);
871 broadcast_packet(source, packet);
875 if(subnet->owner == source) {
876 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
880 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
883 uint16_t type = packet->data[12] << 8 | packet->data[13];
885 if(priorityinheritance && type == ETH_P_IP && packet->len >= ether_size + ip_size)
886 packet->priority = packet->data[15];
888 // Handle packets larger than PMTU
890 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
892 if(directonly && subnet->owner != via)
895 if(via && packet->len > via->mtu && via != myself) {
896 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);
897 length_t ethlen = 14;
899 if(type == ETH_P_8021Q) {
900 type = packet->data[16] << 8 | packet->data[17];
904 if(type == ETH_P_IP && packet->len > 576 + ethlen) {
905 if(packet->data[6 + ethlen] & 0x40) {
906 packet->len = via->mtu;
907 route_ipv4_unreachable(source, packet, ethlen, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
909 fragment_ipv4_packet(via, packet, ethlen);
912 } else if(type == ETH_P_IPV6 && packet->len > 1280 + ethlen) {
913 packet->len = via->mtu;
914 route_ipv6_unreachable(source, packet, ethlen, ICMP6_PACKET_TOO_BIG, 0);
919 clamp_mss(source, via, packet);
921 send_packet(subnet->owner, packet);
924 static bool do_decrement_ttl(node_t *source, vpn_packet_t *packet) {
925 uint16_t type = packet->data[12] << 8 | packet->data[13];
926 length_t ethlen = ether_size;
928 if(type == ETH_P_8021Q) {
929 type = packet->data[16] << 8 | packet->data[17];
935 if(!checklength(source, packet, ethlen + ip_size))
938 if(packet->data[ethlen + 8] <= 1) {
939 if(packet->data[ethlen + 11] != IPPROTO_ICMP || packet->data[ethlen + 32] != ICMP_TIME_EXCEEDED)
940 route_ipv4_unreachable(source, packet, ethlen, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL);
944 uint16_t old = packet->data[ethlen + 8] << 8 | packet->data[ethlen + 9];
945 packet->data[ethlen + 8]--;
946 uint16_t new = packet->data[ethlen + 8] << 8 | packet->data[ethlen + 9];
948 uint32_t checksum = packet->data[ethlen + 10] << 8 | packet->data[ethlen + 11];
949 checksum += old + (~new & 0xFFFF);
950 while(checksum >> 16)
951 checksum = (checksum & 0xFFFF) + (checksum >> 16);
952 packet->data[ethlen + 10] = checksum >> 8;
953 packet->data[ethlen + 11] = checksum & 0xff;
958 if(!checklength(source, packet, ethlen + ip6_size))
961 if(packet->data[ethlen + 7] <= 1) {
962 if(packet->data[ethlen + 6] != IPPROTO_ICMPV6 || packet->data[ethlen + 40] != ICMP6_TIME_EXCEEDED)
963 route_ipv6_unreachable(source, packet, ethlen, ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT);
967 packet->data[ethlen + 7]--;
976 void route(node_t *source, vpn_packet_t *packet) {
977 if(forwarding_mode == FMODE_KERNEL && source != myself) {
978 send_packet(myself, packet);
982 if(!checklength(source, packet, ether_size))
985 if(decrement_ttl && source != myself)
986 if(!do_decrement_ttl(source, packet))
989 switch (routing_mode) {
992 uint16_t type = packet->data[12] << 8 | packet->data[13];
996 route_arp(source, packet);
1000 route_ipv4(source, packet);
1004 route_ipv6(source, packet);
1008 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
1015 route_mac(source, packet);
1019 broadcast_packet(source, packet);