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 bool broadcast = true;
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 */
114 uint16_t type = packet->data[12] << 8 | packet->data[13];
116 if(type == ETH_P_IP && packet->data[23] == 6)
117 start = 14 + (packet->data[14] & 0xf) * 4;
118 else if(type == ETH_P_IPV6 && packet->data[20] == 6)
121 if(!start || packet->len <= start + 20)
124 /* Use data offset field to calculate length of options field */
125 int len = ((packet->data[start + 12] >> 4) - 5) * 4;
127 if(packet->len < start + 20 + len)
130 /* Search for MSS option header */
131 for(int i = 0; i < len;) {
132 if(packet->data[start + 20 + i] == 0)
135 if(packet->data[start + 20 + i] == 1) {
140 if(i > len - 2 || i > len - packet->data[start + 21 + i])
143 if(packet->data[start + 20 + i] != 2) {
144 if(packet->data[start + 21 + i] < 2)
146 i += packet->data[start + 21 + i];
150 if(packet->data[start + 21] != 4)
154 uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
155 uint16_t newmss = mtu - start - 20;
156 uint16_t csum = packet->data[start + 16] << 8 | packet->data[start + 17];
161 ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
163 /* Update the MSS value and the checksum */
164 packet->data[start + 22 + i] = newmss >> 8;
165 packet->data[start + 23 + i] = newmss & 0xff;
170 packet->data[start + 16] = csum >> 8;
171 packet->data[start + 17] = csum & 0xff;
176 static void swap_mac_addresses(vpn_packet_t *packet) {
178 memcpy(&tmp, &packet->data[0], sizeof tmp);
179 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
180 memcpy(&packet->data[6], &tmp, sizeof tmp);
183 static void learn_mac(mac_t *address) {
188 subnet = lookup_subnet_mac(myself, address);
190 /* If we don't know this MAC address yet, store it */
193 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
194 address->x[0], address->x[1], address->x[2], address->x[3],
195 address->x[4], address->x[5]);
197 subnet = new_subnet();
198 subnet->type = SUBNET_MAC;
199 subnet->expires = now + macexpire;
200 subnet->net.mac.address = *address;
202 subnet_add(myself, subnet);
203 subnet_update(myself, subnet, true);
205 /* And tell all other tinc daemons it's our MAC */
207 for(node = connection_tree->head; node; node = node->next) {
210 send_add_subnet(c, subnet);
215 subnet->expires = now + macexpire;
218 void age_subnets(void) {
221 avl_node_t *node, *next, *node2;
223 for(node = myself->subnet_tree->head; node; node = next) {
226 if(s->expires && s->expires <= now) {
228 char netstr[MAXNETSTR];
229 if(net2str(netstr, sizeof netstr, s))
230 logger(LOG_INFO, "Subnet %s expired", netstr);
233 for(node2 = connection_tree->head; node2; node2 = node2->next) {
236 send_del_subnet(c, s);
239 subnet_update(myself, s, false);
240 subnet_del(myself, s);
247 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
249 struct icmp icmp = {0};
251 struct in_addr ip_src;
252 struct in_addr ip_dst;
258 /* Swap Ethernet source and destination addresses */
260 swap_mac_addresses(packet);
262 /* Copy headers from packet into properly aligned structs on the stack */
264 memcpy(&ip, packet->data + ether_size, ip_size);
266 /* Remember original source and destination */
271 oldlen = packet->len - ether_size;
273 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
274 icmp.icmp_nextmtu = htons(packet->len - ether_size);
276 if(oldlen >= IP_MSS - ip_size - icmp_size)
277 oldlen = IP_MSS - ip_size - icmp_size;
279 /* Copy first part of original contents to ICMP message */
281 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
283 /* Fill in IPv4 header */
286 ip.ip_hl = ip_size / 4;
288 ip.ip_len = htons(ip_size + icmp_size + oldlen);
292 ip.ip_p = IPPROTO_ICMP;
297 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
299 /* Fill in ICMP header */
301 icmp.icmp_type = type;
302 icmp.icmp_code = code;
305 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
306 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
308 /* Copy structs on stack back to packet */
310 memcpy(packet->data + ether_size, &ip, ip_size);
311 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
313 packet->len = ether_size + ip_size + icmp_size + oldlen;
315 send_packet(source, packet);
320 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
322 vpn_packet_t fragment;
323 int len, maxlen, todo;
325 uint16_t ip_off, origf;
327 memcpy(&ip, packet->data + ether_size, ip_size);
328 fragment.priority = packet->priority;
330 if(ip.ip_hl != ip_size / 4)
333 todo = ntohs(ip.ip_len) - ip_size;
335 if(ether_size + ip_size + todo != packet->len) {
336 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%zd)", packet->len, ether_size + ip_size + todo);
340 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
342 offset = packet->data + ether_size + ip_size;
343 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
344 ip_off = ntohs(ip.ip_off);
345 origf = ip_off & ~IP_OFFMASK;
346 ip_off &= IP_OFFMASK;
349 len = todo > maxlen ? maxlen : todo;
350 memcpy(fragment.data + ether_size + ip_size, offset, len);
354 ip.ip_len = htons(ip_size + len);
355 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
357 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
358 memcpy(fragment.data, packet->data, ether_size);
359 memcpy(fragment.data + ether_size, &ip, ip_size);
360 fragment.len = ether_size + ip_size + len;
362 send_packet(dest, &fragment);
368 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
373 memcpy(&dest, &packet->data[30], sizeof dest);
374 subnet = lookup_subnet_ipv4(&dest);
377 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
378 source->name, source->hostname,
384 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
388 if(subnet->owner == source) {
389 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
393 if(!subnet->owner->status.reachable)
394 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
396 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
397 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
399 if(priorityinheritance)
400 packet->priority = packet->data[15];
402 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
405 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
409 if(directonly && subnet->owner != via)
410 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
412 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
413 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);
414 if(packet->data[20] & 0x40) {
415 packet->len = MAX(via->mtu, 590);
416 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
418 fragment_ipv4_packet(via, packet);
424 clamp_mss(source, via, packet);
426 send_packet(subnet->owner, packet);
429 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
430 if(!checklength(source, packet, ether_size + ip_size))
433 if(broadcast && (((packet->data[30] & 0xf0) == 0xe0) || (
434 packet->data[30] == 255 &&
435 packet->data[31] == 255 &&
436 packet->data[32] == 255 &&
437 packet->data[33] == 255)))
438 broadcast_packet(source, packet);
440 route_ipv4_unicast(source, packet);
445 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
447 struct icmp6_hdr icmp6 = {0};
451 struct in6_addr ip6_src; /* source address */
452 struct in6_addr ip6_dst; /* destination address */
460 /* Swap Ethernet source and destination addresses */
462 swap_mac_addresses(packet);
464 /* Copy headers from packet to structs on the stack */
466 memcpy(&ip6, packet->data + ether_size, ip6_size);
468 /* Remember original source and destination */
470 pseudo.ip6_src = ip6.ip6_dst;
471 pseudo.ip6_dst = ip6.ip6_src;
473 pseudo.length = packet->len - ether_size;
475 if(type == ICMP6_PACKET_TOO_BIG)
476 icmp6.icmp6_mtu = htonl(pseudo.length);
478 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
479 pseudo.length = IP_MSS - ip6_size - icmp6_size;
481 /* Copy first part of original contents to ICMP message */
483 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
485 /* Fill in IPv6 header */
487 ip6.ip6_flow = htonl(0x60000000UL);
488 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
489 ip6.ip6_nxt = IPPROTO_ICMPV6;
491 ip6.ip6_src = pseudo.ip6_src;
492 ip6.ip6_dst = pseudo.ip6_dst;
494 /* Fill in ICMP header */
496 icmp6.icmp6_type = type;
497 icmp6.icmp6_code = code;
498 icmp6.icmp6_cksum = 0;
500 /* Create pseudo header */
502 pseudo.length = htonl(icmp6_size + pseudo.length);
503 pseudo.next = htonl(IPPROTO_ICMPV6);
505 /* Generate checksum */
507 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
508 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
509 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
511 icmp6.icmp6_cksum = checksum;
513 /* Copy structs on stack back to packet */
515 memcpy(packet->data + ether_size, &ip6, ip6_size);
516 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
518 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
520 send_packet(source, packet);
523 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
528 memcpy(&dest, &packet->data[38], sizeof dest);
529 subnet = lookup_subnet_ipv6(&dest);
532 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
533 source->name, source->hostname,
543 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
547 if(subnet->owner == source) {
548 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
552 if(!subnet->owner->status.reachable)
553 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
555 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
556 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
558 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
561 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
565 if(directonly && subnet->owner != via)
566 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
568 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
569 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);
570 packet->len = MAX(via->mtu, 1294);
571 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
575 clamp_mss(source, via, packet);
577 send_packet(subnet->owner, packet);
582 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
584 struct nd_neighbor_solicit ns;
585 struct nd_opt_hdr opt;
591 struct in6_addr ip6_src; /* source address */
592 struct in6_addr ip6_dst; /* destination address */
597 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
600 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
602 if(source != myself) {
603 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
607 /* Copy headers from packet to structs on the stack */
609 memcpy(&ip6, packet->data + ether_size, ip6_size);
610 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
612 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
614 /* First, snatch the source address from the neighbor solicitation packet */
617 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
619 /* Check if this is a valid neighbor solicitation request */
621 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
622 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
623 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
627 /* Create pseudo header */
629 pseudo.ip6_src = ip6.ip6_src;
630 pseudo.ip6_dst = ip6.ip6_dst;
632 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
634 pseudo.length = htonl(ns_size);
635 pseudo.next = htonl(IPPROTO_ICMPV6);
637 /* Generate checksum */
639 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
640 checksum = inet_checksum(&ns, ns_size, checksum);
642 checksum = inet_checksum(&opt, opt_size, checksum);
643 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
647 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
651 /* Check if the IPv6 address exists on the VPN */
653 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
656 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
657 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
658 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
659 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
660 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
661 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
662 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
663 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
664 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
669 /* Check if it is for our own subnet */
671 if(subnet->owner == myself)
672 return; /* silently ignore */
674 /* Create neighbor advertation reply */
676 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
677 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
679 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
680 ip6.ip6_src = ns.nd_ns_target;
683 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
686 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
687 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
688 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
690 /* Create pseudo header */
692 pseudo.ip6_src = ip6.ip6_src;
693 pseudo.ip6_dst = ip6.ip6_dst;
695 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
697 pseudo.length = htonl(ns_size);
698 pseudo.next = htonl(IPPROTO_ICMPV6);
700 /* Generate checksum */
702 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
703 checksum = inet_checksum(&ns, ns_size, checksum);
705 checksum = inet_checksum(&opt, opt_size, checksum);
706 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
709 ns.nd_ns_hdr.icmp6_cksum = checksum;
711 /* Copy structs on stack back to packet */
713 memcpy(packet->data + ether_size, &ip6, ip6_size);
714 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
716 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
718 send_packet(source, packet);
721 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
722 if(!checklength(source, packet, ether_size + ip6_size))
725 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
726 route_neighborsol(source, packet);
730 if(broadcast && packet->data[38] == 255)
731 broadcast_packet(source, packet);
733 route_ipv6_unicast(source, packet);
738 static void route_arp(node_t *source, vpn_packet_t *packet) {
739 struct ether_arp arp;
743 if(!checklength(source, packet, ether_size + arp_size))
746 if(source != myself) {
747 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
751 /* First, snatch the source address from the ARP packet */
754 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
756 /* Copy headers from packet to structs on the stack */
758 memcpy(&arp, packet->data + ether_size, arp_size);
760 /* Check if this is a valid ARP request */
762 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
763 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
764 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
768 /* Check if the IPv4 address exists on the VPN */
770 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
773 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
774 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
779 /* Check if it is for our own subnet */
781 if(subnet->owner == myself)
782 return; /* silently ignore */
784 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
785 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
787 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
788 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
789 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
791 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
792 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
793 arp.arp_op = htons(ARPOP_REPLY);
795 /* Copy structs on stack back to packet */
797 memcpy(packet->data + ether_size, &arp, arp_size);
799 send_packet(source, packet);
802 static void route_mac(node_t *source, vpn_packet_t *packet) {
806 /* Learn source address */
808 if(source == myself) {
810 memcpy(&src, &packet->data[6], sizeof src);
814 /* Lookup destination address */
816 memcpy(&dest, &packet->data[0], sizeof dest);
817 subnet = lookup_subnet_mac(NULL, &dest);
821 broadcast_packet(source, packet);
825 if(subnet->owner == source) {
826 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
830 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
833 // Handle packets larger than PMTU
835 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
837 if(directonly && subnet->owner != via)
840 if(via && packet->len > via->mtu && via != myself) {
841 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);
842 uint16_t type = packet->data[12] << 8 | packet->data[13];
843 if(type == ETH_P_IP && packet->len > 590) {
844 if(packet->data[20] & 0x40) {
845 packet->len = via->mtu;
846 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
848 fragment_ipv4_packet(via, packet);
851 } else if(type == ETH_P_IPV6 && packet->len > 1294) {
852 packet->len = via->mtu;
853 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
858 clamp_mss(source, via, packet);
860 send_packet(subnet->owner, packet);
863 static bool do_decrement_ttl(node_t *source, vpn_packet_t *packet) {
864 uint16_t type = packet->data[12] << 8 | packet->data[13];
868 if(!checklength(source, packet, 14 + 32))
871 if(packet->data[22] < 1) {
872 if(packet->data[25] != IPPROTO_ICMP || packet->data[46] != ICMP_TIME_EXCEEDED)
873 route_ipv4_unreachable(source, packet, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL);
877 uint16_t old = packet->data[22] << 8 | packet->data[23];
879 uint16_t new = packet->data[22] << 8 | packet->data[23];
881 uint32_t checksum = packet->data[24] << 8 | packet->data[25];
882 checksum += old + (~new & 0xFFFF);
883 while(checksum >> 16)
884 checksum = (checksum & 0xFFFF) + (checksum >> 16);
885 packet->data[24] = checksum >> 8;
886 packet->data[25] = checksum & 0xff;
891 if(!checklength(source, packet, 14 + 40))
894 if(packet->data[21] < 1) {
895 if(packet->data[20] != IPPROTO_ICMPV6 || packet->data[54] != ICMP6_TIME_EXCEEDED)
896 route_ipv6_unreachable(source, packet, ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT);
909 void route(node_t *source, vpn_packet_t *packet) {
910 if(forwarding_mode == FMODE_KERNEL && source != myself) {
911 send_packet(myself, packet);
915 if(!checklength(source, packet, ether_size))
918 if(decrement_ttl && source != myself)
919 if(!do_decrement_ttl(source, packet))
922 switch (routing_mode) {
925 uint16_t type = packet->data[12] << 8 | packet->data[13];
929 route_arp(source, packet);
933 route_ipv4(source, packet);
937 route_ipv6(source, packet);
941 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
948 route_mac(source, packet);
952 broadcast_packet(source, packet);