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 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);
51 #define max(a, b) ((a) > (b) ? (a) : (b))
55 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 clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
97 if(!source || !via || !(via->options & OPTION_CLAMP_MSS))
100 uint16_t mtu = source->mtu;
101 if(via != myself && via->mtu < mtu)
104 /* Find TCP header */
106 uint16_t type = packet->data[12] << 8 | packet->data[13];
108 if(type == ETH_P_IP && packet->data[23] == 6)
109 start = 14 + (packet->data[14] & 0xf) * 4;
110 else if(type == ETH_P_IPV6 && packet->data[20] == 6)
113 if(!start || packet->len <= start + 20)
116 /* Use data offset field to calculate length of options field */
117 int len = ((packet->data[start + 12] >> 4) - 5) * 4;
119 if(packet->len < start + 20 + len)
122 /* Search for MSS option header */
123 for(int i = 0; i < len;) {
124 if(packet->data[start + 20 + i] == 0)
127 if(packet->data[start + 20 + i] == 1) {
132 if(i > len - 2 || i > len - packet->data[start + 21 + i])
135 if(packet->data[start + 20 + i] != 2) {
136 if(packet->data[start + 21 + i] < 2)
138 i += packet->data[start + 21 + i];
142 if(packet->data[start + 21] != 4)
146 uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
147 uint16_t newmss = mtu - start - 20;
148 uint16_t csum = packet->data[start + 16] << 8 | packet->data[start + 17];
153 ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
155 /* Update the MSS value and the checksum */
156 packet->data[start + 22 + i] = newmss >> 8;
157 packet->data[start + 23 + i] = newmss & 0xff;
162 packet->data[start + 16] = csum >> 8;
163 packet->data[start + 17] = csum & 0xff;
168 static void swap_mac_addresses(vpn_packet_t *packet) {
170 memcpy(&tmp, &packet->data[0], sizeof tmp);
171 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
172 memcpy(&packet->data[6], &tmp, sizeof tmp);
175 static void learn_mac(mac_t *address) {
180 subnet = lookup_subnet_mac(myself, address);
182 /* If we don't know this MAC address yet, store it */
185 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
186 address->x[0], address->x[1], address->x[2], address->x[3],
187 address->x[4], address->x[5]);
189 subnet = new_subnet();
190 subnet->type = SUBNET_MAC;
191 subnet->expires = now + macexpire;
192 subnet->net.mac.address = *address;
194 subnet_add(myself, subnet);
195 subnet_update(myself, subnet, true);
197 /* And tell all other tinc daemons it's our MAC */
199 for(node = connection_tree->head; node; node = node->next) {
202 send_add_subnet(c, subnet);
207 subnet->expires = now + macexpire;
210 void age_subnets(void) {
213 avl_node_t *node, *next, *node2;
215 for(node = myself->subnet_tree->head; node; node = next) {
218 if(s->expires && s->expires < now) {
220 char netstr[MAXNETSTR];
221 if(net2str(netstr, sizeof netstr, s))
222 logger(LOG_INFO, "Subnet %s expired", netstr);
225 for(node2 = connection_tree->head; node2; node2 = node2->next) {
228 send_del_subnet(c, s);
231 subnet_update(myself, s, false);
232 subnet_del(myself, s);
239 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
241 struct icmp icmp = {0};
243 struct in_addr ip_src;
244 struct in_addr ip_dst;
250 /* Swap Ethernet source and destination addresses */
252 swap_mac_addresses(packet);
254 /* Copy headers from packet into properly aligned structs on the stack */
256 memcpy(&ip, packet->data + ether_size, ip_size);
258 /* Remember original source and destination */
263 oldlen = packet->len - ether_size;
265 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
266 icmp.icmp_nextmtu = htons(packet->len - ether_size);
268 if(oldlen >= IP_MSS - ip_size - icmp_size)
269 oldlen = IP_MSS - ip_size - icmp_size;
271 /* Copy first part of original contents to ICMP message */
273 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
275 /* Fill in IPv4 header */
278 ip.ip_hl = ip_size / 4;
280 ip.ip_len = htons(ip_size + icmp_size + oldlen);
284 ip.ip_p = IPPROTO_ICMP;
289 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
291 /* Fill in ICMP header */
293 icmp.icmp_type = type;
294 icmp.icmp_code = code;
297 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
298 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
300 /* Copy structs on stack back to packet */
302 memcpy(packet->data + ether_size, &ip, ip_size);
303 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
305 packet->len = ether_size + ip_size + icmp_size + oldlen;
307 send_packet(source, packet);
312 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
314 vpn_packet_t fragment;
315 int len, maxlen, todo;
317 uint16_t ip_off, origf;
319 memcpy(&ip, packet->data + ether_size, ip_size);
320 fragment.priority = packet->priority;
322 if(ip.ip_hl != ip_size / 4)
325 todo = ntohs(ip.ip_len) - ip_size;
327 if(ether_size + ip_size + todo != packet->len) {
328 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%zd)", packet->len, ether_size + ip_size + todo);
332 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
334 offset = packet->data + ether_size + ip_size;
335 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
336 ip_off = ntohs(ip.ip_off);
337 origf = ip_off & ~IP_OFFMASK;
338 ip_off &= IP_OFFMASK;
341 len = todo > maxlen ? maxlen : todo;
342 memcpy(fragment.data + ether_size + ip_size, offset, len);
346 ip.ip_len = htons(ip_size + len);
347 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
349 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
350 memcpy(fragment.data, packet->data, ether_size);
351 memcpy(fragment.data + ether_size, &ip, ip_size);
352 fragment.len = ether_size + ip_size + len;
354 send_packet(dest, &fragment);
360 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
365 memcpy(&dest, &packet->data[30], sizeof dest);
366 subnet = lookup_subnet_ipv4(&dest);
369 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
370 source->name, source->hostname,
376 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
380 if(subnet->owner == source) {
381 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
385 if(!subnet->owner->status.reachable)
386 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
388 if(priorityinheritance)
389 packet->priority = packet->data[15];
391 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
393 if(via && packet->len > max(via->mtu, 590) && via != myself) {
394 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);
395 if(packet->data[20] & 0x40) {
396 packet->len = max(via->mtu, 590);
397 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
399 fragment_ipv4_packet(via, packet);
405 clamp_mss(source, via, packet);
407 send_packet(subnet->owner, packet);
410 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
411 if(!checklength(source, packet, ether_size + ip_size))
414 if(((packet->data[30] & 0xf0) == 0xe0) || (
415 packet->data[30] == 255 &&
416 packet->data[31] == 255 &&
417 packet->data[32] == 255 &&
418 packet->data[33] == 255))
419 broadcast_packet(source, packet);
421 route_ipv4_unicast(source, packet);
426 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
428 struct icmp6_hdr icmp6 = {0};
432 struct in6_addr ip6_src; /* source address */
433 struct in6_addr ip6_dst; /* destination address */
441 /* Swap Ethernet source and destination addresses */
443 swap_mac_addresses(packet);
445 /* Copy headers from packet to structs on the stack */
447 memcpy(&ip6, packet->data + ether_size, ip6_size);
449 /* Remember original source and destination */
451 pseudo.ip6_src = ip6.ip6_dst;
452 pseudo.ip6_dst = ip6.ip6_src;
454 pseudo.length = packet->len - ether_size;
456 if(type == ICMP6_PACKET_TOO_BIG)
457 icmp6.icmp6_mtu = htonl(pseudo.length);
459 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
460 pseudo.length = IP_MSS - ip6_size - icmp6_size;
462 /* Copy first part of original contents to ICMP message */
464 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
466 /* Fill in IPv6 header */
468 ip6.ip6_flow = htonl(0x60000000UL);
469 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
470 ip6.ip6_nxt = IPPROTO_ICMPV6;
472 ip6.ip6_src = pseudo.ip6_src;
473 ip6.ip6_dst = pseudo.ip6_dst;
475 /* Fill in ICMP header */
477 icmp6.icmp6_type = type;
478 icmp6.icmp6_code = code;
479 icmp6.icmp6_cksum = 0;
481 /* Create pseudo header */
483 pseudo.length = htonl(icmp6_size + pseudo.length);
484 pseudo.next = htonl(IPPROTO_ICMPV6);
486 /* Generate checksum */
488 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
489 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
490 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
492 icmp6.icmp6_cksum = checksum;
494 /* Copy structs on stack back to packet */
496 memcpy(packet->data + ether_size, &ip6, ip6_size);
497 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
499 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
501 send_packet(source, packet);
504 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
509 memcpy(&dest, &packet->data[38], sizeof dest);
510 subnet = lookup_subnet_ipv6(&dest);
513 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
514 source->name, source->hostname,
524 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
528 if(subnet->owner == source) {
529 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
533 if(!subnet->owner->status.reachable)
534 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
536 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
538 if(via && packet->len > max(via->mtu, 1294) && via != myself) {
539 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);
540 packet->len = max(via->mtu, 1294);
541 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
545 clamp_mss(source, via, packet);
547 send_packet(subnet->owner, packet);
552 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
554 struct nd_neighbor_solicit ns;
555 struct nd_opt_hdr opt;
561 struct in6_addr ip6_src; /* source address */
562 struct in6_addr ip6_dst; /* destination address */
567 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
570 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
572 if(source != myself) {
573 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
577 /* Copy headers from packet to structs on the stack */
579 memcpy(&ip6, packet->data + ether_size, ip6_size);
580 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
582 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
584 /* First, snatch the source address from the neighbor solicitation packet */
587 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
589 /* Check if this is a valid neighbor solicitation request */
591 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
592 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
593 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
597 /* Create pseudo header */
599 pseudo.ip6_src = ip6.ip6_src;
600 pseudo.ip6_dst = ip6.ip6_dst;
602 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
604 pseudo.length = htonl(ns_size);
605 pseudo.next = htonl(IPPROTO_ICMPV6);
607 /* Generate checksum */
609 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
610 checksum = inet_checksum(&ns, ns_size, checksum);
612 checksum = inet_checksum(&opt, opt_size, checksum);
613 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
617 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
621 /* Check if the IPv6 address exists on the VPN */
623 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
626 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
627 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
628 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
629 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
630 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
631 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
632 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
633 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
634 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
639 /* Check if it is for our own subnet */
641 if(subnet->owner == myself)
642 return; /* silently ignore */
644 /* Create neighbor advertation reply */
646 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
647 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
649 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
650 ip6.ip6_src = ns.nd_ns_target;
653 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
656 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
657 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
658 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
660 /* Create pseudo header */
662 pseudo.ip6_src = ip6.ip6_src;
663 pseudo.ip6_dst = ip6.ip6_dst;
665 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
667 pseudo.length = htonl(ns_size);
668 pseudo.next = htonl(IPPROTO_ICMPV6);
670 /* Generate checksum */
672 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
673 checksum = inet_checksum(&ns, ns_size, checksum);
675 checksum = inet_checksum(&opt, opt_size, checksum);
676 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
679 ns.nd_ns_hdr.icmp6_cksum = checksum;
681 /* Copy structs on stack back to packet */
683 memcpy(packet->data + ether_size, &ip6, ip6_size);
684 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
686 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
688 send_packet(source, packet);
691 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
692 if(!checklength(source, packet, ether_size + ip6_size))
695 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
696 route_neighborsol(source, packet);
700 if(packet->data[38] == 255)
701 broadcast_packet(source, packet);
703 route_ipv6_unicast(source, packet);
708 static void route_arp(node_t *source, vpn_packet_t *packet) {
709 struct ether_arp arp;
713 if(!checklength(source, packet, ether_size + arp_size))
716 if(source != myself) {
717 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
721 /* First, snatch the source address from the ARP packet */
724 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
726 /* Copy headers from packet to structs on the stack */
728 memcpy(&arp, packet->data + ether_size, arp_size);
730 /* Check if this is a valid ARP request */
732 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
733 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
734 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
738 /* Check if the IPv4 address exists on the VPN */
740 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
743 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
744 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
749 /* Check if it is for our own subnet */
751 if(subnet->owner == myself)
752 return; /* silently ignore */
754 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
755 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
757 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
758 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
759 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
761 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
762 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
763 arp.arp_op = htons(ARPOP_REPLY);
765 /* Copy structs on stack back to packet */
767 memcpy(packet->data + ether_size, &arp, arp_size);
769 send_packet(source, packet);
772 static void route_mac(node_t *source, vpn_packet_t *packet) {
776 /* Learn source address */
778 if(source == myself) {
780 memcpy(&src, &packet->data[6], sizeof src);
784 /* Lookup destination address */
786 memcpy(&dest, &packet->data[0], sizeof dest);
787 subnet = lookup_subnet_mac(NULL, &dest);
790 broadcast_packet(source, packet);
794 if(subnet->owner == source) {
795 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
799 // Handle packets larger than PMTU
801 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
803 if(via && packet->len > via->mtu && via != myself) {
804 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);
805 uint16_t type = packet->data[12] << 8 | packet->data[13];
806 if(type == ETH_P_IP && packet->len > 590) {
807 if(packet->data[20] & 0x40) {
808 packet->len = via->mtu;
809 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
811 fragment_ipv4_packet(via, packet);
814 } else if(type == ETH_P_IPV6 && packet->len > 1294) {
815 packet->len = via->mtu;
816 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
821 clamp_mss(source, via, packet);
823 send_packet(subnet->owner, packet);
826 void route(node_t *source, vpn_packet_t *packet) {
827 if(!checklength(source, packet, ether_size))
830 switch (routing_mode) {
833 uint16_t type = packet->data[12] << 8 | packet->data[13];
837 route_arp(source, packet);
841 route_ipv4(source, packet);
845 route_ipv6(source, packet);
849 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
856 route_mac(source, packet);
860 broadcast_packet(source, packet);