3 Copyright (C) 2000-2005 Ivo Timmermans,
4 2000-2014 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 bmode_t broadcast_mode = BMODE_MST;
38 bool decrement_ttl = false;
39 bool directonly = false;
40 bool priorityinheritance = false;
42 bool overwrite_mac = false;
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);
56 static bool do_decrement_ttl(node_t *source, vpn_packet_t *packet);
59 #define MAX(a, b) ((a) > (b) ? (a) : (b))
64 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
66 uint32_t checksum = prevsum ^ 0xFFFF;
74 checksum += *(uint8_t *)p;
77 checksum = (checksum & 0xFFFF) + (checksum >> 16);
82 static bool ratelimit(int frequency) {
83 static time_t lasttime = 0;
87 if(count >= frequency)
98 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
99 if(packet->len < length) {
100 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
106 static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
107 if(!source || !via || !(via->options & OPTION_CLAMP_MSS))
110 uint16_t mtu = source->mtu;
111 if(via != myself && via->mtu < mtu)
114 /* Find TCP header */
115 int start = ether_size;
116 uint16_t type = packet->data[12] << 8 | packet->data[13];
118 if(type == ETH_P_8021Q) {
120 type = packet->data[16] << 8 | packet->data[17];
123 if(type == ETH_P_IP && packet->data[start + 9] == 6)
124 start += (packet->data[start] & 0xf) * 4;
125 else if(type == ETH_P_IPV6 && packet->data[start + 6] == 6)
130 if(packet->len <= start + 20)
133 /* Use data offset field to calculate length of options field */
134 int len = ((packet->data[start + 12] >> 4) - 5) * 4;
136 if(packet->len < start + 20 + len)
139 /* Search for MSS option header */
140 for(int i = 0; i < len;) {
141 if(packet->data[start + 20 + i] == 0)
144 if(packet->data[start + 20 + i] == 1) {
149 if(i > len - 2 || i > len - packet->data[start + 21 + i])
152 if(packet->data[start + 20 + i] != 2) {
153 if(packet->data[start + 21 + i] < 2)
155 i += packet->data[start + 21 + i];
159 if(packet->data[start + 21] != 4)
163 uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
164 uint16_t newmss = mtu - start - 20;
165 uint32_t csum = packet->data[start + 16] << 8 | packet->data[start + 17];
170 ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
172 /* Update the MSS value and the checksum */
173 packet->data[start + 22 + i] = newmss >> 8;
174 packet->data[start + 23 + i] = newmss & 0xff;
176 csum += oldmss ^ 0xffff;
178 csum = (csum & 0xffff) + (csum >> 16);
181 packet->data[start + 16] = csum >> 8;
182 packet->data[start + 17] = csum;
187 static void swap_mac_addresses(vpn_packet_t *packet) {
189 memcpy(&tmp, &packet->data[0], sizeof tmp);
190 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
191 memcpy(&packet->data[6], &tmp, sizeof tmp);
194 static void learn_mac(mac_t *address) {
199 subnet = lookup_subnet_mac(myself, address);
201 /* If we don't know this MAC address yet, store it */
204 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %x:%x:%x:%x:%x:%x",
205 address->x[0], address->x[1], address->x[2], address->x[3],
206 address->x[4], address->x[5]);
208 subnet = new_subnet();
209 subnet->type = SUBNET_MAC;
210 subnet->expires = now + macexpire;
211 subnet->net.mac.address = *address;
213 subnet_add(myself, subnet);
214 subnet_update(myself, subnet, true);
216 /* And tell all other tinc daemons it's our MAC */
218 for(node = connection_tree->head; node; node = node->next) {
221 send_add_subnet(c, subnet);
226 subnet->expires = now + macexpire;
229 void age_subnets(void) {
232 avl_node_t *node, *next, *node2;
234 for(node = myself->subnet_tree->head; node; node = next) {
237 if(s->expires && s->expires <= now) {
239 char netstr[MAXNETSTR];
240 if(net2str(netstr, sizeof netstr, s))
241 logger(LOG_INFO, "Subnet %s expired", netstr);
244 for(node2 = connection_tree->head; node2; node2 = node2->next) {
247 send_del_subnet(c, s);
250 subnet_update(myself, s, false);
251 subnet_del(myself, s);
256 static void route_broadcast(node_t *source, vpn_packet_t *packet) {
257 if(decrement_ttl && source != myself)
258 if(!do_decrement_ttl(source, packet))
261 broadcast_packet(source, packet);
266 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
268 struct icmp icmp = {0};
270 struct in_addr ip_src;
271 struct in_addr ip_dst;
277 /* Swap Ethernet source and destination addresses */
279 swap_mac_addresses(packet);
281 /* Copy headers from packet into properly aligned structs on the stack */
283 memcpy(&ip, packet->data + ether_size, ip_size);
285 /* Remember original source and destination */
290 /* Try to reply with an IP address assigned to the local machine */
292 if (type == ICMP_TIME_EXCEEDED && code == ICMP_EXC_TTL) {
293 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
295 struct sockaddr_in addr;
296 memset(&addr, 0, sizeof(addr));
297 addr.sin_family = AF_INET;
298 addr.sin_addr = ip.ip_src;
299 if (!connect(sockfd, (const struct sockaddr*) &addr, sizeof(addr))) {
300 memset(&addr, 0, sizeof(addr));
301 addr.sin_family = AF_INET;
302 socklen_t addrlen = sizeof(addr);
303 if (!getsockname(sockfd, (struct sockaddr*) &addr, &addrlen) && addrlen <= sizeof(addr)) {
304 ip_dst = addr.sin_addr;
311 oldlen = packet->len - ether_size;
313 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
314 icmp.icmp_nextmtu = htons(packet->len - ether_size);
316 if(oldlen >= IP_MSS - ip_size - icmp_size)
317 oldlen = IP_MSS - ip_size - icmp_size;
319 /* Copy first part of original contents to ICMP message */
321 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
323 /* Fill in IPv4 header */
326 ip.ip_hl = ip_size / 4;
328 ip.ip_len = htons(ip_size + icmp_size + oldlen);
332 ip.ip_p = IPPROTO_ICMP;
337 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
339 /* Fill in ICMP header */
341 icmp.icmp_type = type;
342 icmp.icmp_code = code;
345 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
346 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
348 /* Copy structs on stack back to packet */
350 memcpy(packet->data + ether_size, &ip, ip_size);
351 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
353 packet->len = ether_size + ip_size + icmp_size + oldlen;
355 send_packet(source, packet);
360 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t ether_size) {
362 vpn_packet_t fragment;
363 int len, maxlen, todo;
365 uint16_t ip_off, origf;
367 memcpy(&ip, packet->data + ether_size, ip_size);
368 fragment.priority = packet->priority;
370 if(ip.ip_hl != ip_size / 4)
373 todo = ntohs(ip.ip_len) - ip_size;
375 if(ether_size + ip_size + todo != packet->len) {
376 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%d)", packet->len, (int)(ether_size + ip_size + todo));
380 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
382 offset = packet->data + ether_size + ip_size;
383 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
384 ip_off = ntohs(ip.ip_off);
385 origf = ip_off & ~IP_OFFMASK;
386 ip_off &= IP_OFFMASK;
389 len = todo > maxlen ? maxlen : todo;
390 memcpy(fragment.data + ether_size + ip_size, offset, len);
394 ip.ip_len = htons(ip_size + len);
395 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
397 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
398 memcpy(fragment.data, packet->data, ether_size);
399 memcpy(fragment.data + ether_size, &ip, ip_size);
400 fragment.len = ether_size + ip_size + len;
402 send_packet(dest, &fragment);
408 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
413 memcpy(&dest, &packet->data[30], sizeof dest);
414 subnet = lookup_subnet_ipv4(&dest);
417 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
418 source->name, source->hostname,
424 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
428 if(subnet->owner == source) {
429 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
433 if(!subnet->owner->status.reachable)
434 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
436 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
437 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
439 if(decrement_ttl && source != myself && subnet->owner != myself)
440 if(!do_decrement_ttl(source, packet))
443 if(priorityinheritance)
444 packet->priority = packet->data[15];
446 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
449 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
453 if(directonly && subnet->owner != via)
454 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
456 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
457 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);
458 if(packet->data[20] & 0x40) {
459 packet->len = MAX(via->mtu, 590);
460 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
462 fragment_ipv4_packet(via, packet, ether_size);
468 clamp_mss(source, via, packet);
470 send_packet(subnet->owner, packet);
473 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
474 if(!checklength(source, packet, ether_size + ip_size))
477 if(broadcast_mode && (((packet->data[30] & 0xf0) == 0xe0) || (
478 packet->data[30] == 255 &&
479 packet->data[31] == 255 &&
480 packet->data[32] == 255 &&
481 packet->data[33] == 255)))
482 route_broadcast(source, packet);
484 route_ipv4_unicast(source, packet);
489 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
491 struct icmp6_hdr icmp6 = {0};
495 struct in6_addr ip6_src; /* source address */
496 struct in6_addr ip6_dst; /* destination address */
504 /* Swap Ethernet source and destination addresses */
506 swap_mac_addresses(packet);
508 /* Copy headers from packet to structs on the stack */
510 memcpy(&ip6, packet->data + ether_size, ip6_size);
512 /* Remember original source and destination */
514 pseudo.ip6_src = ip6.ip6_dst;
515 pseudo.ip6_dst = ip6.ip6_src;
517 /* Try to reply with an IP address assigned to the local machine */
519 if (type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT) {
520 int sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
522 struct sockaddr_in6 addr;
523 memset(&addr, 0, sizeof(addr));
524 addr.sin6_family = AF_INET6;
525 addr.sin6_addr = ip6.ip6_src;
526 if (!connect(sockfd, (const struct sockaddr*) &addr, sizeof(addr))) {
527 memset(&addr, 0, sizeof(addr));
528 addr.sin6_family = AF_INET6;
529 socklen_t addrlen = sizeof(addr);
530 if (!getsockname(sockfd, (struct sockaddr*) &addr, &addrlen) && addrlen <= sizeof(addr)) {
531 pseudo.ip6_src = addr.sin6_addr;
538 pseudo.length = packet->len - ether_size;
540 if(type == ICMP6_PACKET_TOO_BIG)
541 icmp6.icmp6_mtu = htonl(pseudo.length);
543 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
544 pseudo.length = IP_MSS - ip6_size - icmp6_size;
546 /* Copy first part of original contents to ICMP message */
548 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
550 /* Fill in IPv6 header */
552 ip6.ip6_flow = htonl(0x60000000UL);
553 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
554 ip6.ip6_nxt = IPPROTO_ICMPV6;
556 ip6.ip6_src = pseudo.ip6_src;
557 ip6.ip6_dst = pseudo.ip6_dst;
559 /* Fill in ICMP header */
561 icmp6.icmp6_type = type;
562 icmp6.icmp6_code = code;
563 icmp6.icmp6_cksum = 0;
565 /* Create pseudo header */
567 pseudo.length = htonl(icmp6_size + pseudo.length);
568 pseudo.next = htonl(IPPROTO_ICMPV6);
570 /* Generate checksum */
572 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
573 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
574 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
576 icmp6.icmp6_cksum = checksum;
578 /* Copy structs on stack back to packet */
580 memcpy(packet->data + ether_size, &ip6, ip6_size);
581 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
583 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
585 send_packet(source, packet);
588 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
593 memcpy(&dest, &packet->data[38], sizeof dest);
594 subnet = lookup_subnet_ipv6(&dest);
597 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
598 source->name, source->hostname,
608 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
612 if(subnet->owner == source) {
613 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
617 if(!subnet->owner->status.reachable)
618 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
620 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
621 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
623 if(decrement_ttl && source != myself && subnet->owner != myself)
624 if(!do_decrement_ttl(source, packet))
627 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
630 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
634 if(directonly && subnet->owner != via)
635 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
637 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
638 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);
639 packet->len = MAX(via->mtu, 1294);
640 route_ipv6_unreachable(source, packet, ether_size, ICMP6_PACKET_TOO_BIG, 0);
644 clamp_mss(source, via, packet);
646 send_packet(subnet->owner, packet);
651 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
653 struct nd_neighbor_solicit ns;
654 struct nd_opt_hdr opt;
660 struct in6_addr ip6_src; /* source address */
661 struct in6_addr ip6_dst; /* destination address */
666 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
669 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
671 if(source != myself) {
672 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
676 /* Copy headers from packet to structs on the stack */
678 memcpy(&ip6, packet->data + ether_size, ip6_size);
679 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
681 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
683 /* First, snatch the source address from the neighbor solicitation packet */
686 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
688 /* Check if this is a valid neighbor solicitation request */
690 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
691 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
692 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
696 /* Create pseudo header */
698 pseudo.ip6_src = ip6.ip6_src;
699 pseudo.ip6_dst = ip6.ip6_dst;
701 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
703 pseudo.length = htonl(ns_size);
704 pseudo.next = htonl(IPPROTO_ICMPV6);
706 /* Generate checksum */
708 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
709 checksum = inet_checksum(&ns, ns_size, checksum);
711 checksum = inet_checksum(&opt, opt_size, checksum);
712 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
716 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
720 /* Check if the IPv6 address exists on the VPN */
722 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
725 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
726 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
727 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
728 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
729 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
730 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
731 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
732 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
733 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
738 /* Check if it is for our own subnet */
740 if(subnet->owner == myself)
741 return; /* silently ignore */
744 if(!do_decrement_ttl(source, packet))
747 /* Create neighbor advertation reply */
749 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
750 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
752 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
753 ip6.ip6_src = ns.nd_ns_target;
756 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
759 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
760 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
761 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
763 /* Create pseudo header */
765 pseudo.ip6_src = ip6.ip6_src;
766 pseudo.ip6_dst = ip6.ip6_dst;
768 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
770 pseudo.length = htonl(ns_size);
771 pseudo.next = htonl(IPPROTO_ICMPV6);
773 /* Generate checksum */
775 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
776 checksum = inet_checksum(&ns, ns_size, checksum);
778 checksum = inet_checksum(&opt, opt_size, checksum);
779 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
782 ns.nd_ns_hdr.icmp6_cksum = checksum;
784 /* Copy structs on stack back to packet */
786 memcpy(packet->data + ether_size, &ip6, ip6_size);
787 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
789 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
791 send_packet(source, packet);
794 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
795 if(!checklength(source, packet, ether_size + ip6_size))
798 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
799 route_neighborsol(source, packet);
803 if(broadcast_mode && packet->data[38] == 255)
804 route_broadcast(source, packet);
806 route_ipv6_unicast(source, packet);
811 static void route_arp(node_t *source, vpn_packet_t *packet) {
812 struct ether_arp arp;
816 if(!checklength(source, packet, ether_size + arp_size))
819 if(source != myself) {
820 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
824 /* First, snatch the source address from the ARP packet */
827 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
829 /* Copy headers from packet to structs on the stack */
831 memcpy(&arp, packet->data + ether_size, arp_size);
833 /* Check if this is a valid ARP request */
835 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
836 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
837 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
841 /* Check if the IPv4 address exists on the VPN */
843 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
846 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
847 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
852 /* Check if it is for our own subnet */
854 if(subnet->owner == myself)
855 return; /* silently ignore */
858 if(!do_decrement_ttl(source, packet))
861 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
862 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
864 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
865 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
866 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
868 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
869 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
870 arp.arp_op = htons(ARPOP_REPLY);
872 /* Copy structs on stack back to packet */
874 memcpy(packet->data + ether_size, &arp, arp_size);
876 send_packet(source, packet);
879 static void route_mac(node_t *source, vpn_packet_t *packet) {
883 /* Learn source address */
885 if(source == myself) {
887 memcpy(&src, &packet->data[6], sizeof src);
891 /* Lookup destination address */
893 memcpy(&dest, &packet->data[0], sizeof dest);
894 subnet = lookup_subnet_mac(NULL, &dest);
897 route_broadcast(source, packet);
901 if(subnet->owner == source) {
902 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
906 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
909 if(decrement_ttl && source != myself && subnet->owner != myself)
910 if(!do_decrement_ttl(source, packet))
913 uint16_t type = packet->data[12] << 8 | packet->data[13];
915 if(priorityinheritance && type == ETH_P_IP && packet->len >= ether_size + ip_size)
916 packet->priority = packet->data[15];
918 // Handle packets larger than PMTU
920 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
922 if(directonly && subnet->owner != via)
925 if(via && packet->len > via->mtu && via != myself) {
926 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);
927 length_t ethlen = 14;
929 if(type == ETH_P_8021Q) {
930 type = packet->data[16] << 8 | packet->data[17];
934 if(type == ETH_P_IP && packet->len > 576 + ethlen) {
935 if(packet->data[6 + ethlen] & 0x40) {
936 packet->len = via->mtu;
937 route_ipv4_unreachable(source, packet, ethlen, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
939 fragment_ipv4_packet(via, packet, ethlen);
942 } else if(type == ETH_P_IPV6 && packet->len > 1280 + ethlen) {
943 packet->len = via->mtu;
944 route_ipv6_unreachable(source, packet, ethlen, ICMP6_PACKET_TOO_BIG, 0);
949 clamp_mss(source, via, packet);
951 send_packet(subnet->owner, packet);
954 static bool do_decrement_ttl(node_t *source, vpn_packet_t *packet) {
955 uint16_t type = packet->data[12] << 8 | packet->data[13];
956 length_t ethlen = ether_size;
958 if(type == ETH_P_8021Q) {
959 type = packet->data[16] << 8 | packet->data[17];
965 if(!checklength(source, packet, ethlen + ip_size))
968 if(packet->data[ethlen + 8] <= 1) {
969 if(packet->data[ethlen + 11] != IPPROTO_ICMP || packet->data[ethlen + 32] != ICMP_TIME_EXCEEDED)
970 route_ipv4_unreachable(source, packet, ethlen, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL);
974 uint16_t old = packet->data[ethlen + 8] << 8 | packet->data[ethlen + 9];
975 packet->data[ethlen + 8]--;
976 uint16_t new = packet->data[ethlen + 8] << 8 | packet->data[ethlen + 9];
978 uint32_t checksum = packet->data[ethlen + 10] << 8 | packet->data[ethlen + 11];
979 checksum += old + (~new & 0xFFFF);
980 while(checksum >> 16)
981 checksum = (checksum & 0xFFFF) + (checksum >> 16);
982 packet->data[ethlen + 10] = checksum >> 8;
983 packet->data[ethlen + 11] = checksum & 0xff;
988 if(!checklength(source, packet, ethlen + ip6_size))
991 if(packet->data[ethlen + 7] <= 1) {
992 if(packet->data[ethlen + 6] != IPPROTO_ICMPV6 || packet->data[ethlen + 40] != ICMP6_TIME_EXCEEDED)
993 route_ipv6_unreachable(source, packet, ethlen, ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT);
997 packet->data[ethlen + 7]--;
1006 void route(node_t *source, vpn_packet_t *packet) {
1007 if(forwarding_mode == FMODE_KERNEL && source != myself) {
1008 send_packet(myself, packet);
1012 if(!checklength(source, packet, ether_size))
1015 switch (routing_mode) {
1018 uint16_t type = packet->data[12] << 8 | packet->data[13];
1022 route_arp(source, packet);
1026 route_ipv4(source, packet);
1030 route_ipv6(source, packet);
1034 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
1041 route_mac(source, packet);
1045 route_broadcast(source, packet);