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 directonly = false;
38 bool priorityinheritance = false;
40 bool overwrite_mac = false;
41 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
43 /* Sizes of various headers */
45 static const size_t ether_size = sizeof(struct ether_header);
46 static const size_t arp_size = sizeof(struct ether_arp);
47 static const size_t ip_size = sizeof(struct ip);
48 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
49 static const size_t ip6_size = sizeof(struct ip6_hdr);
50 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
51 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
52 static const size_t opt_size = sizeof(struct nd_opt_hdr);
53 #define max(a, b) ((a) > (b) ? (a) : (b))
57 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
59 uint32_t checksum = prevsum ^ 0xFFFF;
67 checksum += *(uint8_t *)p;
70 checksum = (checksum & 0xFFFF) + (checksum >> 16);
75 static bool ratelimit(int frequency) {
76 static time_t lasttime = 0;
80 if(++count > frequency)
90 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
91 if(packet->len < length) {
92 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
98 static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
99 if(!source || !via || !(via->options & OPTION_CLAMP_MSS))
102 uint16_t mtu = source->mtu;
103 if(via != myself && via->mtu < mtu)
106 /* Find TCP header */
108 uint16_t type = packet->data[12] << 8 | packet->data[13];
110 if(type == ETH_P_IP && packet->data[23] == 6)
111 start = 14 + (packet->data[14] & 0xf) * 4;
112 else if(type == ETH_P_IPV6 && packet->data[20] == 6)
115 if(!start || packet->len <= start + 20)
118 /* Use data offset field to calculate length of options field */
119 int len = ((packet->data[start + 12] >> 4) - 5) * 4;
121 if(packet->len < start + 20 + len)
124 /* Search for MSS option header */
125 for(int i = 0; i < len;) {
126 if(packet->data[start + 20 + i] == 0)
129 if(packet->data[start + 20 + i] == 1) {
134 if(i > len - 2 || i > len - packet->data[start + 21 + i])
137 if(packet->data[start + 20 + i] != 2) {
138 if(packet->data[start + 21 + i] < 2)
140 i += packet->data[start + 21 + i];
144 if(packet->data[start + 21] != 4)
148 uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
149 uint16_t newmss = mtu - start - 20;
150 uint16_t csum = packet->data[start + 16] << 8 | packet->data[start + 17];
155 ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
157 /* Update the MSS value and the checksum */
158 packet->data[start + 22 + i] = newmss >> 8;
159 packet->data[start + 23 + i] = newmss & 0xff;
164 packet->data[start + 16] = csum >> 8;
165 packet->data[start + 17] = csum & 0xff;
170 static void swap_mac_addresses(vpn_packet_t *packet) {
172 memcpy(&tmp, &packet->data[0], sizeof tmp);
173 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
174 memcpy(&packet->data[6], &tmp, sizeof tmp);
177 static void learn_mac(mac_t *address) {
182 subnet = lookup_subnet_mac(myself, address);
184 /* If we don't know this MAC address yet, store it */
187 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
188 address->x[0], address->x[1], address->x[2], address->x[3],
189 address->x[4], address->x[5]);
191 subnet = new_subnet();
192 subnet->type = SUBNET_MAC;
193 subnet->expires = now + macexpire;
194 subnet->net.mac.address = *address;
196 subnet_add(myself, subnet);
197 subnet_update(myself, subnet, true);
199 /* And tell all other tinc daemons it's our MAC */
201 for(node = connection_tree->head; node; node = node->next) {
204 send_add_subnet(c, subnet);
209 subnet->expires = now + macexpire;
212 void age_subnets(void) {
215 avl_node_t *node, *next, *node2;
217 for(node = myself->subnet_tree->head; node; node = next) {
220 if(s->expires && s->expires < now) {
222 char netstr[MAXNETSTR];
223 if(net2str(netstr, sizeof netstr, s))
224 logger(LOG_INFO, "Subnet %s expired", netstr);
227 for(node2 = connection_tree->head; node2; node2 = node2->next) {
230 send_del_subnet(c, s);
233 subnet_update(myself, s, false);
234 subnet_del(myself, s);
241 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
243 struct icmp icmp = {0};
245 struct in_addr ip_src;
246 struct in_addr ip_dst;
252 /* Swap Ethernet source and destination addresses */
254 swap_mac_addresses(packet);
256 /* Copy headers from packet into properly aligned structs on the stack */
258 memcpy(&ip, packet->data + ether_size, ip_size);
260 /* Remember original source and destination */
265 oldlen = packet->len - ether_size;
267 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
268 icmp.icmp_nextmtu = htons(packet->len - ether_size);
270 if(oldlen >= IP_MSS - ip_size - icmp_size)
271 oldlen = IP_MSS - ip_size - icmp_size;
273 /* Copy first part of original contents to ICMP message */
275 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
277 /* Fill in IPv4 header */
280 ip.ip_hl = ip_size / 4;
282 ip.ip_len = htons(ip_size + icmp_size + oldlen);
286 ip.ip_p = IPPROTO_ICMP;
291 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
293 /* Fill in ICMP header */
295 icmp.icmp_type = type;
296 icmp.icmp_code = code;
299 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
300 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
302 /* Copy structs on stack back to packet */
304 memcpy(packet->data + ether_size, &ip, ip_size);
305 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
307 packet->len = ether_size + ip_size + icmp_size + oldlen;
309 send_packet(source, packet);
314 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
316 vpn_packet_t fragment;
317 int len, maxlen, todo;
319 uint16_t ip_off, origf;
321 memcpy(&ip, packet->data + ether_size, ip_size);
322 fragment.priority = packet->priority;
324 if(ip.ip_hl != ip_size / 4)
327 todo = ntohs(ip.ip_len) - ip_size;
329 if(ether_size + ip_size + todo != packet->len) {
330 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%zd)", packet->len, ether_size + ip_size + todo);
334 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
336 offset = packet->data + ether_size + ip_size;
337 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
338 ip_off = ntohs(ip.ip_off);
339 origf = ip_off & ~IP_OFFMASK;
340 ip_off &= IP_OFFMASK;
343 len = todo > maxlen ? maxlen : todo;
344 memcpy(fragment.data + ether_size + ip_size, offset, len);
348 ip.ip_len = htons(ip_size + len);
349 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
351 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
352 memcpy(fragment.data, packet->data, ether_size);
353 memcpy(fragment.data + ether_size, &ip, ip_size);
354 fragment.len = ether_size + ip_size + len;
356 send_packet(dest, &fragment);
362 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
367 memcpy(&dest, &packet->data[30], sizeof dest);
368 subnet = lookup_subnet_ipv4(&dest);
371 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
372 source->name, source->hostname,
378 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
382 if(subnet->owner == source) {
383 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
387 if(!subnet->owner->status.reachable)
388 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
390 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
391 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
393 if(priorityinheritance)
394 packet->priority = packet->data[15];
396 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
398 if(directonly && subnet->owner != via)
399 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
401 if(via && packet->len > max(via->mtu, 590) && via != myself) {
402 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);
403 if(packet->data[20] & 0x40) {
404 packet->len = max(via->mtu, 590);
405 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
407 fragment_ipv4_packet(via, packet);
413 clamp_mss(source, via, packet);
415 send_packet(subnet->owner, packet);
418 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
419 if(!checklength(source, packet, ether_size + ip_size))
422 if(((packet->data[30] & 0xf0) == 0xe0) || (
423 packet->data[30] == 255 &&
424 packet->data[31] == 255 &&
425 packet->data[32] == 255 &&
426 packet->data[33] == 255))
427 broadcast_packet(source, packet);
429 route_ipv4_unicast(source, packet);
434 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
436 struct icmp6_hdr icmp6 = {0};
440 struct in6_addr ip6_src; /* source address */
441 struct in6_addr ip6_dst; /* destination address */
449 /* Swap Ethernet source and destination addresses */
451 swap_mac_addresses(packet);
453 /* Copy headers from packet to structs on the stack */
455 memcpy(&ip6, packet->data + ether_size, ip6_size);
457 /* Remember original source and destination */
459 pseudo.ip6_src = ip6.ip6_dst;
460 pseudo.ip6_dst = ip6.ip6_src;
462 pseudo.length = packet->len - ether_size;
464 if(type == ICMP6_PACKET_TOO_BIG)
465 icmp6.icmp6_mtu = htonl(pseudo.length);
467 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
468 pseudo.length = IP_MSS - ip6_size - icmp6_size;
470 /* Copy first part of original contents to ICMP message */
472 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
474 /* Fill in IPv6 header */
476 ip6.ip6_flow = htonl(0x60000000UL);
477 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
478 ip6.ip6_nxt = IPPROTO_ICMPV6;
480 ip6.ip6_src = pseudo.ip6_src;
481 ip6.ip6_dst = pseudo.ip6_dst;
483 /* Fill in ICMP header */
485 icmp6.icmp6_type = type;
486 icmp6.icmp6_code = code;
487 icmp6.icmp6_cksum = 0;
489 /* Create pseudo header */
491 pseudo.length = htonl(icmp6_size + pseudo.length);
492 pseudo.next = htonl(IPPROTO_ICMPV6);
494 /* Generate checksum */
496 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
497 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
498 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
500 icmp6.icmp6_cksum = checksum;
502 /* Copy structs on stack back to packet */
504 memcpy(packet->data + ether_size, &ip6, ip6_size);
505 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
507 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
509 send_packet(source, packet);
512 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
517 memcpy(&dest, &packet->data[38], sizeof dest);
518 subnet = lookup_subnet_ipv6(&dest);
521 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
522 source->name, source->hostname,
532 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
536 if(subnet->owner == source) {
537 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
541 if(!subnet->owner->status.reachable)
542 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
544 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
545 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
547 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
549 if(directonly && subnet->owner != via)
550 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
552 if(via && packet->len > max(via->mtu, 1294) && via != myself) {
553 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);
554 packet->len = max(via->mtu, 1294);
555 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
559 clamp_mss(source, via, packet);
561 send_packet(subnet->owner, packet);
566 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
568 struct nd_neighbor_solicit ns;
569 struct nd_opt_hdr opt;
575 struct in6_addr ip6_src; /* source address */
576 struct in6_addr ip6_dst; /* destination address */
581 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
584 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
586 if(source != myself) {
587 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
591 /* Copy headers from packet to structs on the stack */
593 memcpy(&ip6, packet->data + ether_size, ip6_size);
594 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
596 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
598 /* First, snatch the source address from the neighbor solicitation packet */
601 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
603 /* Check if this is a valid neighbor solicitation request */
605 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
606 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
607 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
611 /* Create pseudo header */
613 pseudo.ip6_src = ip6.ip6_src;
614 pseudo.ip6_dst = ip6.ip6_dst;
616 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
618 pseudo.length = htonl(ns_size);
619 pseudo.next = htonl(IPPROTO_ICMPV6);
621 /* Generate checksum */
623 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
624 checksum = inet_checksum(&ns, ns_size, checksum);
626 checksum = inet_checksum(&opt, opt_size, checksum);
627 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
631 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
635 /* Check if the IPv6 address exists on the VPN */
637 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
640 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
641 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
642 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
643 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
644 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
645 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
646 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
647 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
648 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
653 /* Check if it is for our own subnet */
655 if(subnet->owner == myself)
656 return; /* silently ignore */
658 /* Create neighbor advertation reply */
660 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
661 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
663 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
664 ip6.ip6_src = ns.nd_ns_target;
667 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
670 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
671 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
672 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
674 /* Create pseudo header */
676 pseudo.ip6_src = ip6.ip6_src;
677 pseudo.ip6_dst = ip6.ip6_dst;
679 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
681 pseudo.length = htonl(ns_size);
682 pseudo.next = htonl(IPPROTO_ICMPV6);
684 /* Generate checksum */
686 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
687 checksum = inet_checksum(&ns, ns_size, checksum);
689 checksum = inet_checksum(&opt, opt_size, checksum);
690 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
693 ns.nd_ns_hdr.icmp6_cksum = checksum;
695 /* Copy structs on stack back to packet */
697 memcpy(packet->data + ether_size, &ip6, ip6_size);
698 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
700 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
702 send_packet(source, packet);
705 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
706 if(!checklength(source, packet, ether_size + ip6_size))
709 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
710 route_neighborsol(source, packet);
714 if(packet->data[38] == 255)
715 broadcast_packet(source, packet);
717 route_ipv6_unicast(source, packet);
722 static void route_arp(node_t *source, vpn_packet_t *packet) {
723 struct ether_arp arp;
727 if(!checklength(source, packet, ether_size + arp_size))
730 if(source != myself) {
731 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
735 /* First, snatch the source address from the ARP packet */
738 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
740 /* Copy headers from packet to structs on the stack */
742 memcpy(&arp, packet->data + ether_size, arp_size);
744 /* Check if this is a valid ARP request */
746 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
747 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
748 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
752 /* Check if the IPv4 address exists on the VPN */
754 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
757 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
758 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
763 /* Check if it is for our own subnet */
765 if(subnet->owner == myself)
766 return; /* silently ignore */
768 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
769 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
771 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
772 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
773 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
775 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
776 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
777 arp.arp_op = htons(ARPOP_REPLY);
779 /* Copy structs on stack back to packet */
781 memcpy(packet->data + ether_size, &arp, arp_size);
783 send_packet(source, packet);
786 static void route_mac(node_t *source, vpn_packet_t *packet) {
790 /* Learn source address */
792 if(source == myself) {
794 memcpy(&src, &packet->data[6], sizeof src);
798 /* Lookup destination address */
800 memcpy(&dest, &packet->data[0], sizeof dest);
801 subnet = lookup_subnet_mac(NULL, &dest);
804 broadcast_packet(source, packet);
808 if(subnet->owner == source) {
809 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
813 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
816 // Handle packets larger than PMTU
818 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
820 if(directonly && subnet->owner != via)
823 if(via && packet->len > via->mtu && via != myself) {
824 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);
825 uint16_t type = packet->data[12] << 8 | packet->data[13];
826 if(type == ETH_P_IP && packet->len > 590) {
827 if(packet->data[20] & 0x40) {
828 packet->len = via->mtu;
829 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
831 fragment_ipv4_packet(via, packet);
834 } else if(type == ETH_P_IPV6 && packet->len > 1294) {
835 packet->len = via->mtu;
836 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
841 clamp_mss(source, via, packet);
843 send_packet(subnet->owner, packet);
846 void route(node_t *source, vpn_packet_t *packet) {
847 if(forwarding_mode == FMODE_KERNEL && source != myself) {
848 send_packet(myself, packet);
852 if(!checklength(source, packet, ether_size))
855 switch (routing_mode) {
858 uint16_t type = packet->data[12] << 8 | packet->data[13];
862 route_arp(source, packet);
866 route_ipv4(source, packet);
870 route_ipv6(source, packet);
874 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
881 route_mac(source, packet);
885 broadcast_packet(source, packet);