3 Copyright (C) 2000-2005 Ivo Timmermans,
4 2000-2010 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 bool decrement_ttl = true;
38 bool directonly = false;
39 bool priorityinheritance = false;
41 bool overwrite_mac = false;
42 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
44 /* Sizes of various headers */
46 static const size_t ether_size = sizeof(struct ether_header);
47 static const size_t arp_size = sizeof(struct ether_arp);
48 static const size_t ip_size = sizeof(struct ip);
49 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
50 static const size_t ip6_size = sizeof(struct ip6_hdr);
51 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
52 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
53 static const size_t opt_size = sizeof(struct nd_opt_hdr);
56 #define MAX(a, b) ((a) > (b) ? (a) : (b))
61 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
63 uint32_t checksum = prevsum ^ 0xFFFF;
71 checksum += *(uint8_t *)p;
74 checksum = (checksum & 0xFFFF) + (checksum >> 16);
79 static bool ratelimit(int frequency) {
80 static time_t lasttime = 0;
84 if(++count > frequency)
94 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
95 if(packet->len < length) {
96 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
102 static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
103 if(!source || !via || !(via->options & OPTION_CLAMP_MSS))
106 uint16_t mtu = source->mtu;
107 if(via != myself && via->mtu < mtu)
110 /* Find TCP header */
112 uint16_t type = packet->data[12] << 8 | packet->data[13];
114 if(type == ETH_P_IP && packet->data[23] == 6)
115 start = 14 + (packet->data[14] & 0xf) * 4;
116 else if(type == ETH_P_IPV6 && packet->data[20] == 6)
119 if(!start || packet->len <= start + 20)
122 /* Use data offset field to calculate length of options field */
123 int len = ((packet->data[start + 12] >> 4) - 5) * 4;
125 if(packet->len < start + 20 + len)
128 /* Search for MSS option header */
129 for(int i = 0; i < len;) {
130 if(packet->data[start + 20 + i] == 0)
133 if(packet->data[start + 20 + i] == 1) {
138 if(i > len - 2 || i > len - packet->data[start + 21 + i])
141 if(packet->data[start + 20 + i] != 2) {
142 if(packet->data[start + 21 + i] < 2)
144 i += packet->data[start + 21 + i];
148 if(packet->data[start + 21] != 4)
152 uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
153 uint16_t newmss = mtu - start - 20;
154 uint16_t csum = packet->data[start + 16] << 8 | packet->data[start + 17];
159 ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
161 /* Update the MSS value and the checksum */
162 packet->data[start + 22 + i] = newmss >> 8;
163 packet->data[start + 23 + i] = newmss & 0xff;
168 packet->data[start + 16] = csum >> 8;
169 packet->data[start + 17] = csum & 0xff;
174 static void swap_mac_addresses(vpn_packet_t *packet) {
176 memcpy(&tmp, &packet->data[0], sizeof tmp);
177 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
178 memcpy(&packet->data[6], &tmp, sizeof tmp);
181 static void learn_mac(mac_t *address) {
186 subnet = lookup_subnet_mac(myself, address);
188 /* If we don't know this MAC address yet, store it */
191 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
192 address->x[0], address->x[1], address->x[2], address->x[3],
193 address->x[4], address->x[5]);
195 subnet = new_subnet();
196 subnet->type = SUBNET_MAC;
197 subnet->expires = now + macexpire;
198 subnet->net.mac.address = *address;
200 subnet_add(myself, subnet);
201 subnet_update(myself, subnet, true);
203 /* And tell all other tinc daemons it's our MAC */
205 for(node = connection_tree->head; node; node = node->next) {
208 send_add_subnet(c, subnet);
213 subnet->expires = now + macexpire;
216 void age_subnets(void) {
219 avl_node_t *node, *next, *node2;
221 for(node = myself->subnet_tree->head; node; node = next) {
224 if(s->expires && s->expires <= now) {
226 char netstr[MAXNETSTR];
227 if(net2str(netstr, sizeof netstr, s))
228 logger(LOG_INFO, "Subnet %s expired", netstr);
231 for(node2 = connection_tree->head; node2; node2 = node2->next) {
234 send_del_subnet(c, s);
237 subnet_update(myself, s, false);
238 subnet_del(myself, s);
245 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
247 struct icmp icmp = {0};
249 struct in_addr ip_src;
250 struct in_addr ip_dst;
256 /* Swap Ethernet source and destination addresses */
258 swap_mac_addresses(packet);
260 /* Copy headers from packet into properly aligned structs on the stack */
262 memcpy(&ip, packet->data + ether_size, ip_size);
264 /* Remember original source and destination */
269 oldlen = packet->len - ether_size;
271 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
272 icmp.icmp_nextmtu = htons(packet->len - ether_size);
274 if(oldlen >= IP_MSS - ip_size - icmp_size)
275 oldlen = IP_MSS - ip_size - icmp_size;
277 /* Copy first part of original contents to ICMP message */
279 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
281 /* Fill in IPv4 header */
284 ip.ip_hl = ip_size / 4;
286 ip.ip_len = htons(ip_size + icmp_size + oldlen);
290 ip.ip_p = IPPROTO_ICMP;
295 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
297 /* Fill in ICMP header */
299 icmp.icmp_type = type;
300 icmp.icmp_code = code;
303 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
304 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
306 /* Copy structs on stack back to packet */
308 memcpy(packet->data + ether_size, &ip, ip_size);
309 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
311 packet->len = ether_size + ip_size + icmp_size + oldlen;
313 send_packet(source, packet);
318 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
320 vpn_packet_t fragment;
321 int len, maxlen, todo;
323 uint16_t ip_off, origf;
325 memcpy(&ip, packet->data + ether_size, ip_size);
326 fragment.priority = packet->priority;
328 if(ip.ip_hl != ip_size / 4)
331 todo = ntohs(ip.ip_len) - ip_size;
333 if(ether_size + ip_size + todo != packet->len) {
334 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%zd)", packet->len, ether_size + ip_size + todo);
338 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
340 offset = packet->data + ether_size + ip_size;
341 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
342 ip_off = ntohs(ip.ip_off);
343 origf = ip_off & ~IP_OFFMASK;
344 ip_off &= IP_OFFMASK;
347 len = todo > maxlen ? maxlen : todo;
348 memcpy(fragment.data + ether_size + ip_size, offset, len);
352 ip.ip_len = htons(ip_size + len);
353 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
355 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
356 memcpy(fragment.data, packet->data, ether_size);
357 memcpy(fragment.data + ether_size, &ip, ip_size);
358 fragment.len = ether_size + ip_size + len;
360 send_packet(dest, &fragment);
366 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
371 memcpy(&dest, &packet->data[30], sizeof dest);
372 subnet = lookup_subnet_ipv4(&dest);
375 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
376 source->name, source->hostname,
382 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
386 if(subnet->owner == source) {
387 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
391 if(!subnet->owner->status.reachable)
392 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
394 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
395 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
397 if(priorityinheritance)
398 packet->priority = packet->data[15];
400 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
402 if(directonly && subnet->owner != via)
403 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
405 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
406 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);
407 if(packet->data[20] & 0x40) {
408 packet->len = MAX(via->mtu, 590);
409 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
411 fragment_ipv4_packet(via, packet);
417 clamp_mss(source, via, packet);
419 send_packet(subnet->owner, packet);
422 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
423 if(!checklength(source, packet, ether_size + ip_size))
426 if(((packet->data[30] & 0xf0) == 0xe0) || (
427 packet->data[30] == 255 &&
428 packet->data[31] == 255 &&
429 packet->data[32] == 255 &&
430 packet->data[33] == 255))
431 broadcast_packet(source, packet);
433 route_ipv4_unicast(source, packet);
438 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
440 struct icmp6_hdr icmp6 = {0};
444 struct in6_addr ip6_src; /* source address */
445 struct in6_addr ip6_dst; /* destination address */
453 /* Swap Ethernet source and destination addresses */
455 swap_mac_addresses(packet);
457 /* Copy headers from packet to structs on the stack */
459 memcpy(&ip6, packet->data + ether_size, ip6_size);
461 /* Remember original source and destination */
463 pseudo.ip6_src = ip6.ip6_dst;
464 pseudo.ip6_dst = ip6.ip6_src;
466 pseudo.length = packet->len - ether_size;
468 if(type == ICMP6_PACKET_TOO_BIG)
469 icmp6.icmp6_mtu = htonl(pseudo.length);
471 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
472 pseudo.length = IP_MSS - ip6_size - icmp6_size;
474 /* Copy first part of original contents to ICMP message */
476 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
478 /* Fill in IPv6 header */
480 ip6.ip6_flow = htonl(0x60000000UL);
481 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
482 ip6.ip6_nxt = IPPROTO_ICMPV6;
484 ip6.ip6_src = pseudo.ip6_src;
485 ip6.ip6_dst = pseudo.ip6_dst;
487 /* Fill in ICMP header */
489 icmp6.icmp6_type = type;
490 icmp6.icmp6_code = code;
491 icmp6.icmp6_cksum = 0;
493 /* Create pseudo header */
495 pseudo.length = htonl(icmp6_size + pseudo.length);
496 pseudo.next = htonl(IPPROTO_ICMPV6);
498 /* Generate checksum */
500 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
501 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
502 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
504 icmp6.icmp6_cksum = checksum;
506 /* Copy structs on stack back to packet */
508 memcpy(packet->data + ether_size, &ip6, ip6_size);
509 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
511 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
513 send_packet(source, packet);
516 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
521 memcpy(&dest, &packet->data[38], sizeof dest);
522 subnet = lookup_subnet_ipv6(&dest);
525 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
526 source->name, source->hostname,
536 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
540 if(subnet->owner == source) {
541 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
545 if(!subnet->owner->status.reachable)
546 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
548 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
549 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
551 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
553 if(directonly && subnet->owner != via)
554 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
556 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
557 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);
558 packet->len = MAX(via->mtu, 1294);
559 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
563 clamp_mss(source, via, packet);
565 send_packet(subnet->owner, packet);
570 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
572 struct nd_neighbor_solicit ns;
573 struct nd_opt_hdr opt;
579 struct in6_addr ip6_src; /* source address */
580 struct in6_addr ip6_dst; /* destination address */
585 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
588 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
590 if(source != myself) {
591 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
595 /* Copy headers from packet to structs on the stack */
597 memcpy(&ip6, packet->data + ether_size, ip6_size);
598 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
600 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
602 /* First, snatch the source address from the neighbor solicitation packet */
605 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
607 /* Check if this is a valid neighbor solicitation request */
609 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
610 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
611 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
615 /* Create pseudo header */
617 pseudo.ip6_src = ip6.ip6_src;
618 pseudo.ip6_dst = ip6.ip6_dst;
620 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
622 pseudo.length = htonl(ns_size);
623 pseudo.next = htonl(IPPROTO_ICMPV6);
625 /* Generate checksum */
627 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
628 checksum = inet_checksum(&ns, ns_size, checksum);
630 checksum = inet_checksum(&opt, opt_size, checksum);
631 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
635 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
639 /* Check if the IPv6 address exists on the VPN */
641 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
644 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
645 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
646 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
647 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
648 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
649 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
650 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
651 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
652 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
657 /* Check if it is for our own subnet */
659 if(subnet->owner == myself)
660 return; /* silently ignore */
662 /* Create neighbor advertation reply */
664 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
665 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
667 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
668 ip6.ip6_src = ns.nd_ns_target;
671 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
674 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
675 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
676 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
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);
697 ns.nd_ns_hdr.icmp6_cksum = checksum;
699 /* Copy structs on stack back to packet */
701 memcpy(packet->data + ether_size, &ip6, ip6_size);
702 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
704 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
706 send_packet(source, packet);
709 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
710 if(!checklength(source, packet, ether_size + ip6_size))
713 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
714 route_neighborsol(source, packet);
718 if(packet->data[38] == 255)
719 broadcast_packet(source, packet);
721 route_ipv6_unicast(source, packet);
726 static void route_arp(node_t *source, vpn_packet_t *packet) {
727 struct ether_arp arp;
731 if(!checklength(source, packet, ether_size + arp_size))
734 if(source != myself) {
735 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
739 /* First, snatch the source address from the ARP packet */
742 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
744 /* Copy headers from packet to structs on the stack */
746 memcpy(&arp, packet->data + ether_size, arp_size);
748 /* Check if this is a valid ARP request */
750 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
751 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
752 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
756 /* Check if the IPv4 address exists on the VPN */
758 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
761 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
762 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
767 /* Check if it is for our own subnet */
769 if(subnet->owner == myself)
770 return; /* silently ignore */
772 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
773 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
775 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
776 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
777 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
779 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
780 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
781 arp.arp_op = htons(ARPOP_REPLY);
783 /* Copy structs on stack back to packet */
785 memcpy(packet->data + ether_size, &arp, arp_size);
787 send_packet(source, packet);
790 static void route_mac(node_t *source, vpn_packet_t *packet) {
794 /* Learn source address */
796 if(source == myself) {
798 memcpy(&src, &packet->data[6], sizeof src);
802 /* Lookup destination address */
804 memcpy(&dest, &packet->data[0], sizeof dest);
805 subnet = lookup_subnet_mac(NULL, &dest);
808 broadcast_packet(source, packet);
812 if(subnet->owner == source) {
813 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
817 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
820 // Handle packets larger than PMTU
822 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
824 if(directonly && subnet->owner != via)
827 if(via && packet->len > via->mtu && via != myself) {
828 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);
829 uint16_t type = packet->data[12] << 8 | packet->data[13];
830 if(type == ETH_P_IP && packet->len > 590) {
831 if(packet->data[20] & 0x40) {
832 packet->len = via->mtu;
833 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
835 fragment_ipv4_packet(via, packet);
838 } else if(type == ETH_P_IPV6 && packet->len > 1294) {
839 packet->len = via->mtu;
840 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
845 clamp_mss(source, via, packet);
847 send_packet(subnet->owner, packet);
850 static bool do_decrement_ttl(node_t *source, vpn_packet_t *packet) {
851 uint16_t type = packet->data[12] << 8 | packet->data[13];
855 if(!checklength(source, packet, 14 + 32))
858 if(packet->data[22] < 1) {
859 route_ipv4_unreachable(source, packet, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL);
863 uint16_t old = packet->data[22] << 8 | packet->data[23];
865 uint16_t new = packet->data[22] << 8 | packet->data[23];
867 uint32_t checksum = packet->data[24] << 8 | packet->data[25];
868 checksum += old + (~new & 0xFFFF);
869 while(checksum >> 16)
870 checksum = (checksum & 0xFFFF) + (checksum >> 16);
871 packet->data[24] = checksum >> 8;
872 packet->data[25] = checksum & 0xff;
877 if(!checklength(source, packet, 14 + 40))
880 if(packet->data[21] < 1) {
881 route_ipv6_unreachable(source, packet, ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT);
894 void route(node_t *source, vpn_packet_t *packet) {
895 if(forwarding_mode == FMODE_KERNEL && source != myself) {
896 send_packet(myself, packet);
900 if(!checklength(source, packet, ether_size))
903 if(decrement_ttl && source != myself)
904 if(!do_decrement_ttl(source, packet))
907 switch (routing_mode) {
910 uint16_t type = packet->data[12] << 8 | packet->data[13];
914 route_arp(source, packet);
918 route_ipv4(source, packet);
922 route_ipv6(source, packet);
926 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
933 route_mac(source, packet);
937 broadcast_packet(source, packet);