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)
95 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
96 if(packet->len < length) {
97 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
103 static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
104 if(!source || !via || !(via->options & OPTION_CLAMP_MSS))
107 uint16_t mtu = source->mtu;
108 if(via != myself && via->mtu < mtu)
111 /* Find TCP header */
113 uint16_t type = packet->data[12] << 8 | packet->data[13];
115 if(type == ETH_P_IP && packet->data[23] == 6)
116 start = 14 + (packet->data[14] & 0xf) * 4;
117 else if(type == ETH_P_IPV6 && packet->data[20] == 6)
120 if(!start || packet->len <= start + 20)
123 /* Use data offset field to calculate length of options field */
124 int len = ((packet->data[start + 12] >> 4) - 5) * 4;
126 if(packet->len < start + 20 + len)
129 /* Search for MSS option header */
130 for(int i = 0; i < len;) {
131 if(packet->data[start + 20 + i] == 0)
134 if(packet->data[start + 20 + i] == 1) {
139 if(i > len - 2 || i > len - packet->data[start + 21 + i])
142 if(packet->data[start + 20 + i] != 2) {
143 if(packet->data[start + 21 + i] < 2)
145 i += packet->data[start + 21 + i];
149 if(packet->data[start + 21] != 4)
153 uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
154 uint16_t newmss = mtu - start - 20;
155 uint16_t csum = packet->data[start + 16] << 8 | packet->data[start + 17];
160 ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
162 /* Update the MSS value and the checksum */
163 packet->data[start + 22 + i] = newmss >> 8;
164 packet->data[start + 23 + i] = newmss & 0xff;
169 packet->data[start + 16] = csum >> 8;
170 packet->data[start + 17] = csum & 0xff;
175 static void swap_mac_addresses(vpn_packet_t *packet) {
177 memcpy(&tmp, &packet->data[0], sizeof tmp);
178 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
179 memcpy(&packet->data[6], &tmp, sizeof tmp);
182 static void learn_mac(mac_t *address) {
187 subnet = lookup_subnet_mac(myself, address);
189 /* If we don't know this MAC address yet, store it */
192 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
193 address->x[0], address->x[1], address->x[2], address->x[3],
194 address->x[4], address->x[5]);
196 subnet = new_subnet();
197 subnet->type = SUBNET_MAC;
198 subnet->expires = now + macexpire;
199 subnet->net.mac.address = *address;
201 subnet_add(myself, subnet);
202 subnet_update(myself, subnet, true);
204 /* And tell all other tinc daemons it's our MAC */
206 for(node = connection_tree->head; node; node = node->next) {
209 send_add_subnet(c, subnet);
214 subnet->expires = now + macexpire;
217 void age_subnets(void) {
220 avl_node_t *node, *next, *node2;
222 for(node = myself->subnet_tree->head; node; node = next) {
225 if(s->expires && s->expires <= now) {
227 char netstr[MAXNETSTR];
228 if(net2str(netstr, sizeof netstr, s))
229 logger(LOG_INFO, "Subnet %s expired", netstr);
232 for(node2 = connection_tree->head; node2; node2 = node2->next) {
235 send_del_subnet(c, s);
238 subnet_update(myself, s, false);
239 subnet_del(myself, s);
246 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
248 struct icmp icmp = {0};
250 struct in_addr ip_src;
251 struct in_addr ip_dst;
257 /* Swap Ethernet source and destination addresses */
259 swap_mac_addresses(packet);
261 /* Copy headers from packet into properly aligned structs on the stack */
263 memcpy(&ip, packet->data + ether_size, ip_size);
265 /* Remember original source and destination */
270 oldlen = packet->len - ether_size;
272 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
273 icmp.icmp_nextmtu = htons(packet->len - ether_size);
275 if(oldlen >= IP_MSS - ip_size - icmp_size)
276 oldlen = IP_MSS - ip_size - icmp_size;
278 /* Copy first part of original contents to ICMP message */
280 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
282 /* Fill in IPv4 header */
285 ip.ip_hl = ip_size / 4;
287 ip.ip_len = htons(ip_size + icmp_size + oldlen);
291 ip.ip_p = IPPROTO_ICMP;
296 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
298 /* Fill in ICMP header */
300 icmp.icmp_type = type;
301 icmp.icmp_code = code;
304 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
305 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
307 /* Copy structs on stack back to packet */
309 memcpy(packet->data + ether_size, &ip, ip_size);
310 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
312 packet->len = ether_size + ip_size + icmp_size + oldlen;
314 send_packet(source, packet);
319 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
321 vpn_packet_t fragment;
322 int len, maxlen, todo;
324 uint16_t ip_off, origf;
326 memcpy(&ip, packet->data + ether_size, ip_size);
327 fragment.priority = packet->priority;
329 if(ip.ip_hl != ip_size / 4)
332 todo = ntohs(ip.ip_len) - ip_size;
334 if(ether_size + ip_size + todo != packet->len) {
335 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%zd)", packet->len, ether_size + ip_size + todo);
339 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
341 offset = packet->data + ether_size + ip_size;
342 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
343 ip_off = ntohs(ip.ip_off);
344 origf = ip_off & ~IP_OFFMASK;
345 ip_off &= IP_OFFMASK;
348 len = todo > maxlen ? maxlen : todo;
349 memcpy(fragment.data + ether_size + ip_size, offset, len);
353 ip.ip_len = htons(ip_size + len);
354 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
356 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
357 memcpy(fragment.data, packet->data, ether_size);
358 memcpy(fragment.data + ether_size, &ip, ip_size);
359 fragment.len = ether_size + ip_size + len;
361 send_packet(dest, &fragment);
367 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
372 memcpy(&dest, &packet->data[30], sizeof dest);
373 subnet = lookup_subnet_ipv4(&dest);
376 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
377 source->name, source->hostname,
383 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
387 if(subnet->owner == source) {
388 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
392 if(!subnet->owner->status.reachable)
393 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
395 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
396 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
398 if(priorityinheritance)
399 packet->priority = packet->data[15];
401 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
403 if(directonly && subnet->owner != via)
404 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
406 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
407 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);
408 if(packet->data[20] & 0x40) {
409 packet->len = MAX(via->mtu, 590);
410 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
412 fragment_ipv4_packet(via, packet);
418 clamp_mss(source, via, packet);
420 send_packet(subnet->owner, packet);
423 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
424 if(!checklength(source, packet, ether_size + ip_size))
427 if(broadcast && (((packet->data[30] & 0xf0) == 0xe0) || (
428 packet->data[30] == 255 &&
429 packet->data[31] == 255 &&
430 packet->data[32] == 255 &&
431 packet->data[33] == 255)))
432 broadcast_packet(source, packet);
434 route_ipv4_unicast(source, packet);
439 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
441 struct icmp6_hdr icmp6 = {0};
445 struct in6_addr ip6_src; /* source address */
446 struct in6_addr ip6_dst; /* destination address */
454 /* Swap Ethernet source and destination addresses */
456 swap_mac_addresses(packet);
458 /* Copy headers from packet to structs on the stack */
460 memcpy(&ip6, packet->data + ether_size, ip6_size);
462 /* Remember original source and destination */
464 pseudo.ip6_src = ip6.ip6_dst;
465 pseudo.ip6_dst = ip6.ip6_src;
467 pseudo.length = packet->len - ether_size;
469 if(type == ICMP6_PACKET_TOO_BIG)
470 icmp6.icmp6_mtu = htonl(pseudo.length);
472 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
473 pseudo.length = IP_MSS - ip6_size - icmp6_size;
475 /* Copy first part of original contents to ICMP message */
477 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
479 /* Fill in IPv6 header */
481 ip6.ip6_flow = htonl(0x60000000UL);
482 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
483 ip6.ip6_nxt = IPPROTO_ICMPV6;
485 ip6.ip6_src = pseudo.ip6_src;
486 ip6.ip6_dst = pseudo.ip6_dst;
488 /* Fill in ICMP header */
490 icmp6.icmp6_type = type;
491 icmp6.icmp6_code = code;
492 icmp6.icmp6_cksum = 0;
494 /* Create pseudo header */
496 pseudo.length = htonl(icmp6_size + pseudo.length);
497 pseudo.next = htonl(IPPROTO_ICMPV6);
499 /* Generate checksum */
501 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
502 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
503 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
505 icmp6.icmp6_cksum = checksum;
507 /* Copy structs on stack back to packet */
509 memcpy(packet->data + ether_size, &ip6, ip6_size);
510 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
512 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
514 send_packet(source, packet);
517 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
522 memcpy(&dest, &packet->data[38], sizeof dest);
523 subnet = lookup_subnet_ipv6(&dest);
526 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
527 source->name, source->hostname,
537 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
541 if(subnet->owner == source) {
542 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
546 if(!subnet->owner->status.reachable)
547 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
549 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
550 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
552 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
554 if(directonly && subnet->owner != via)
555 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
557 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
558 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);
559 packet->len = MAX(via->mtu, 1294);
560 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
564 clamp_mss(source, via, packet);
566 send_packet(subnet->owner, packet);
571 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
573 struct nd_neighbor_solicit ns;
574 struct nd_opt_hdr opt;
580 struct in6_addr ip6_src; /* source address */
581 struct in6_addr ip6_dst; /* destination address */
586 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
589 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
591 if(source != myself) {
592 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
596 /* Copy headers from packet to structs on the stack */
598 memcpy(&ip6, packet->data + ether_size, ip6_size);
599 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
601 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
603 /* First, snatch the source address from the neighbor solicitation packet */
606 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
608 /* Check if this is a valid neighbor solicitation request */
610 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
611 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
612 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
616 /* Create pseudo header */
618 pseudo.ip6_src = ip6.ip6_src;
619 pseudo.ip6_dst = ip6.ip6_dst;
621 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
623 pseudo.length = htonl(ns_size);
624 pseudo.next = htonl(IPPROTO_ICMPV6);
626 /* Generate checksum */
628 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
629 checksum = inet_checksum(&ns, ns_size, checksum);
631 checksum = inet_checksum(&opt, opt_size, checksum);
632 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
636 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
640 /* Check if the IPv6 address exists on the VPN */
642 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
645 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
646 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
647 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
648 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
649 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
650 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
651 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
652 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
653 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
658 /* Check if it is for our own subnet */
660 if(subnet->owner == myself)
661 return; /* silently ignore */
663 /* Create neighbor advertation reply */
665 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
666 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
668 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
669 ip6.ip6_src = ns.nd_ns_target;
672 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
675 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
676 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
677 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
679 /* Create pseudo header */
681 pseudo.ip6_src = ip6.ip6_src;
682 pseudo.ip6_dst = ip6.ip6_dst;
684 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
686 pseudo.length = htonl(ns_size);
687 pseudo.next = htonl(IPPROTO_ICMPV6);
689 /* Generate checksum */
691 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
692 checksum = inet_checksum(&ns, ns_size, checksum);
694 checksum = inet_checksum(&opt, opt_size, checksum);
695 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
698 ns.nd_ns_hdr.icmp6_cksum = checksum;
700 /* Copy structs on stack back to packet */
702 memcpy(packet->data + ether_size, &ip6, ip6_size);
703 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
705 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
707 send_packet(source, packet);
710 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
711 if(!checklength(source, packet, ether_size + ip6_size))
714 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
715 route_neighborsol(source, packet);
719 if(broadcast && packet->data[38] == 255)
720 broadcast_packet(source, packet);
722 route_ipv6_unicast(source, packet);
727 static void route_arp(node_t *source, vpn_packet_t *packet) {
728 struct ether_arp arp;
732 if(!checklength(source, packet, ether_size + arp_size))
735 if(source != myself) {
736 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
740 /* First, snatch the source address from the ARP packet */
743 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
745 /* Copy headers from packet to structs on the stack */
747 memcpy(&arp, packet->data + ether_size, arp_size);
749 /* Check if this is a valid ARP request */
751 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
752 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
753 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
757 /* Check if the IPv4 address exists on the VPN */
759 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
762 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
763 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
768 /* Check if it is for our own subnet */
770 if(subnet->owner == myself)
771 return; /* silently ignore */
773 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
774 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
776 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
777 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
778 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
780 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
781 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
782 arp.arp_op = htons(ARPOP_REPLY);
784 /* Copy structs on stack back to packet */
786 memcpy(packet->data + ether_size, &arp, arp_size);
788 send_packet(source, packet);
791 static void route_mac(node_t *source, vpn_packet_t *packet) {
795 /* Learn source address */
797 if(source == myself) {
799 memcpy(&src, &packet->data[6], sizeof src);
803 /* Lookup destination address */
805 memcpy(&dest, &packet->data[0], sizeof dest);
806 subnet = lookup_subnet_mac(NULL, &dest);
810 broadcast_packet(source, packet);
814 if(subnet->owner == source) {
815 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
819 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
822 // Handle packets larger than PMTU
824 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
826 if(directonly && subnet->owner != via)
829 if(via && packet->len > via->mtu && via != myself) {
830 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);
831 uint16_t type = packet->data[12] << 8 | packet->data[13];
832 if(type == ETH_P_IP && packet->len > 590) {
833 if(packet->data[20] & 0x40) {
834 packet->len = via->mtu;
835 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
837 fragment_ipv4_packet(via, packet);
840 } else if(type == ETH_P_IPV6 && packet->len > 1294) {
841 packet->len = via->mtu;
842 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
847 clamp_mss(source, via, packet);
849 send_packet(subnet->owner, packet);
852 static bool do_decrement_ttl(node_t *source, vpn_packet_t *packet) {
853 uint16_t type = packet->data[12] << 8 | packet->data[13];
857 if(!checklength(source, packet, 14 + 32))
860 if(packet->data[22] < 1) {
861 route_ipv4_unreachable(source, packet, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL);
865 uint16_t old = packet->data[22] << 8 | packet->data[23];
867 uint16_t new = packet->data[22] << 8 | packet->data[23];
869 uint32_t checksum = packet->data[24] << 8 | packet->data[25];
870 checksum += old + (~new & 0xFFFF);
871 while(checksum >> 16)
872 checksum = (checksum & 0xFFFF) + (checksum >> 16);
873 packet->data[24] = checksum >> 8;
874 packet->data[25] = checksum & 0xff;
879 if(!checklength(source, packet, 14 + 40))
882 if(packet->data[21] < 1) {
883 route_ipv6_unreachable(source, packet, ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT);
896 void route(node_t *source, vpn_packet_t *packet) {
897 if(forwarding_mode == FMODE_KERNEL && source != myself) {
898 send_packet(myself, packet);
902 if(!checklength(source, packet, ether_size))
905 if(decrement_ttl && source != myself)
906 if(!do_decrement_ttl(source, packet))
909 switch (routing_mode) {
912 uint16_t type = packet->data[12] << 8 | packet->data[13];
916 route_arp(source, packet);
920 route_ipv4(source, packet);
924 route_ipv6(source, packet);
928 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
935 route_mac(source, packet);
939 broadcast_packet(source, packet);