3 Copyright (C) 2000-2005 Ivo Timmermans,
4 2000-2017 Guus Sliepen <guus@tinc-vpn.org>
5 2015-2016 Vittorio Gambaletta
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "connection.h"
36 rmode_t routing_mode = RMODE_ROUTER;
37 fmode_t forwarding_mode = FMODE_INTERNAL;
38 bmode_t broadcast_mode = BMODE_MST;
39 bool decrement_ttl = false;
40 bool directonly = false;
41 bool priorityinheritance = false;
43 bool overwrite_mac = false;
44 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
46 /* Sizes of various headers */
48 static const size_t ether_size = sizeof(struct ether_header);
49 static const size_t arp_size = sizeof(struct ether_arp);
50 static const size_t ip_size = sizeof(struct ip);
51 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
52 static const size_t ip6_size = sizeof(struct ip6_hdr);
53 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
54 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
55 static const size_t opt_size = sizeof(struct nd_opt_hdr);
58 #define MAX(a, b) ((a) > (b) ? (a) : (b))
63 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
65 uint32_t checksum = prevsum ^ 0xFFFF;
73 checksum += *(uint8_t *)p;
76 while(checksum >> 16) {
77 checksum = (checksum & 0xFFFF) + (checksum >> 16);
83 static bool ratelimit(int frequency) {
84 static time_t lasttime = 0;
88 if(count >= frequency) {
100 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
101 if(packet->len < length) {
102 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
109 static void swap_mac_addresses(vpn_packet_t *packet) {
111 memcpy(&tmp, &packet->data[0], sizeof(tmp));
112 memcpy(&packet->data[0], &packet->data[6], sizeof(tmp));
113 memcpy(&packet->data[6], &tmp, sizeof(tmp));
118 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
120 struct icmp icmp = {0};
122 struct in_addr ip_src;
123 struct in_addr ip_dst;
130 /* Swap Ethernet source and destination addresses */
132 swap_mac_addresses(packet);
134 /* Copy headers from packet into properly aligned structs on the stack */
136 memcpy(&ip, packet->data + ether_size, ip_size);
138 /* Remember original source and destination */
143 /* Try to reply with an IP address assigned to the local machine */
145 if(type == ICMP_TIME_EXCEEDED && code == ICMP_EXC_TTL) {
146 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
149 struct sockaddr_in addr;
150 memset(&addr, 0, sizeof(addr));
151 addr.sin_family = AF_INET;
152 addr.sin_addr = ip.ip_src;
154 if(!connect(sockfd, (const struct sockaddr *) &addr, sizeof(addr))) {
155 memset(&addr, 0, sizeof(addr));
156 addr.sin_family = AF_INET;
157 socklen_t addrlen = sizeof(addr);
159 if(!getsockname(sockfd, (struct sockaddr *) &addr, &addrlen) && addrlen <= sizeof(addr)) {
160 ip_dst = addr.sin_addr;
168 oldlen = packet->len - ether_size;
170 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
171 icmp.icmp_nextmtu = htons(packet->len - ether_size);
174 if(oldlen >= IP_MSS - ip_size - icmp_size) {
175 oldlen = IP_MSS - ip_size - icmp_size;
178 /* Copy first part of original contents to ICMP message */
180 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
182 /* Fill in IPv4 header */
185 ip.ip_hl = ip_size / 4;
187 ip.ip_len = htons(ip_size + icmp_size + oldlen);
191 ip.ip_p = IPPROTO_ICMP;
196 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
198 /* Fill in ICMP header */
200 icmp.icmp_type = type;
201 icmp.icmp_code = code;
204 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
205 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
207 /* Copy structs on stack back to packet */
209 memcpy(packet->data + ether_size, &ip, ip_size);
210 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
212 packet->len = ether_size + ip_size + icmp_size + oldlen;
214 send_packet(source, packet);
219 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
221 struct icmp6_hdr icmp6 = {0};
225 struct in6_addr ip6_src; /* source address */
226 struct in6_addr ip6_dst; /* destination address */
235 /* Swap Ethernet source and destination addresses */
237 swap_mac_addresses(packet);
239 /* Copy headers from packet to structs on the stack */
241 memcpy(&ip6, packet->data + ether_size, ip6_size);
243 /* Remember original source and destination */
245 pseudo.ip6_src = ip6.ip6_dst;
246 pseudo.ip6_dst = ip6.ip6_src;
248 /* Try to reply with an IP address assigned to the local machine */
250 if(type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT) {
251 int sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
254 struct sockaddr_in6 addr;
255 memset(&addr, 0, sizeof(addr));
256 addr.sin6_family = AF_INET6;
257 addr.sin6_addr = ip6.ip6_src;
259 if(!connect(sockfd, (const struct sockaddr *) &addr, sizeof(addr))) {
260 memset(&addr, 0, sizeof(addr));
261 addr.sin6_family = AF_INET6;
262 socklen_t addrlen = sizeof(addr);
264 if(!getsockname(sockfd, (struct sockaddr *) &addr, &addrlen) && addrlen <= sizeof(addr)) {
265 pseudo.ip6_src = addr.sin6_addr;
273 pseudo.length = packet->len - ether_size;
275 if(type == ICMP6_PACKET_TOO_BIG) {
276 icmp6.icmp6_mtu = htonl(pseudo.length);
279 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size) {
280 pseudo.length = IP_MSS - ip6_size - icmp6_size;
283 /* Copy first part of original contents to ICMP message */
285 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
287 /* Fill in IPv6 header */
289 ip6.ip6_flow = htonl(0x60000000UL);
290 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
291 ip6.ip6_nxt = IPPROTO_ICMPV6;
293 ip6.ip6_src = pseudo.ip6_src;
294 ip6.ip6_dst = pseudo.ip6_dst;
296 /* Fill in ICMP header */
298 icmp6.icmp6_type = type;
299 icmp6.icmp6_code = code;
300 icmp6.icmp6_cksum = 0;
302 /* Create pseudo header */
304 pseudo.length = htonl(icmp6_size + pseudo.length);
305 pseudo.next = htonl(IPPROTO_ICMPV6);
307 /* Generate checksum */
309 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
310 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
311 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
313 icmp6.icmp6_cksum = checksum;
315 /* Copy structs on stack back to packet */
317 memcpy(packet->data + ether_size, &ip6, ip6_size);
318 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
320 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
322 send_packet(source, packet);
325 static bool do_decrement_ttl(node_t *source, vpn_packet_t *packet) {
326 uint16_t type = packet->data[12] << 8 | packet->data[13];
327 length_t ethlen = ether_size;
329 if(type == ETH_P_8021Q) {
330 type = packet->data[16] << 8 | packet->data[17];
336 if(!checklength(source, packet, ethlen + ip_size)) {
340 if(packet->data[ethlen + 8] <= 1) {
341 if(packet->data[ethlen + 11] != IPPROTO_ICMP || packet->data[ethlen + 32] != ICMP_TIME_EXCEEDED) {
342 route_ipv4_unreachable(source, packet, ethlen, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL);
348 uint16_t old = packet->data[ethlen + 8] << 8 | packet->data[ethlen + 9];
349 packet->data[ethlen + 8]--;
350 uint16_t new = packet->data[ethlen + 8] << 8 | packet->data[ethlen + 9];
352 uint32_t checksum = packet->data[ethlen + 10] << 8 | packet->data[ethlen + 11];
353 checksum += old + (~new & 0xFFFF);
355 while(checksum >> 16) {
356 checksum = (checksum & 0xFFFF) + (checksum >> 16);
359 packet->data[ethlen + 10] = checksum >> 8;
360 packet->data[ethlen + 11] = checksum & 0xff;
365 if(!checklength(source, packet, ethlen + ip6_size)) {
369 if(packet->data[ethlen + 7] <= 1) {
370 if(packet->data[ethlen + 6] != IPPROTO_ICMPV6 || packet->data[ethlen + 40] != ICMP6_TIME_EXCEEDED) {
371 route_ipv6_unreachable(source, packet, ethlen, ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT);
377 packet->data[ethlen + 7]--;
386 static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
387 if(!source || !via || !(via->options & OPTION_CLAMP_MSS)) {
391 uint16_t mtu = source->mtu;
393 if(via != myself && via->mtu < mtu) {
397 /* Find TCP header */
398 int start = ether_size;
399 uint16_t type = packet->data[12] << 8 | packet->data[13];
401 if(type == ETH_P_8021Q) {
403 type = packet->data[16] << 8 | packet->data[17];
406 if(type == ETH_P_IP && packet->data[start + 9] == 6) {
407 start += (packet->data[start] & 0xf) * 4;
408 } else if(type == ETH_P_IPV6 && packet->data[start + 6] == 6) {
414 if(packet->len <= start + 20) {
418 /* Use data offset field to calculate length of options field */
419 int len = ((packet->data[start + 12] >> 4) - 5) * 4;
421 if(packet->len < start + 20 + len) {
425 /* Search for MSS option header */
426 for(int i = 0; i < len;) {
427 if(packet->data[start + 20 + i] == 0) {
431 if(packet->data[start + 20 + i] == 1) {
436 if(i > len - 2 || i > len - packet->data[start + 21 + i]) {
440 if(packet->data[start + 20 + i] != 2) {
441 if(packet->data[start + 21 + i] < 2) {
445 i += packet->data[start + 21 + i];
449 if(packet->data[start + 21] != 4) {
454 uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
455 uint16_t newmss = mtu - start - 20;
456 uint32_t csum = packet->data[start + 16] << 8 | packet->data[start + 17];
458 if(oldmss <= newmss) {
462 ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
464 /* Update the MSS value and the checksum */
465 packet->data[start + 22 + i] = newmss >> 8;
466 packet->data[start + 23 + i] = newmss & 0xff;
468 csum += oldmss ^ 0xffff;
470 csum = (csum & 0xffff) + (csum >> 16);
473 packet->data[start + 16] = csum >> 8;
474 packet->data[start + 17] = csum;
479 static void learn_mac(mac_t *address) {
484 subnet = lookup_subnet_mac(myself, address);
486 /* If we don't know this MAC address yet, store it */
489 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %x:%x:%x:%x:%x:%x",
490 address->x[0], address->x[1], address->x[2], address->x[3],
491 address->x[4], address->x[5]);
493 subnet = new_subnet();
494 subnet->type = SUBNET_MAC;
495 subnet->expires = now + macexpire;
496 subnet->net.mac.address = *address;
498 subnet_add(myself, subnet);
499 subnet_update(myself, subnet, true);
501 /* And tell all other tinc daemons it's our MAC */
503 for(node = connection_tree->head; node; node = node->next) {
506 if(c->status.active) {
507 send_add_subnet(c, subnet);
512 if(subnet->expires) {
513 subnet->expires = now + macexpire;
517 void age_subnets(void) {
520 avl_node_t *node, *next, *node2;
522 for(node = myself->subnet_tree->head; node; node = next) {
526 if(s->expires && s->expires <= now) {
528 char netstr[MAXNETSTR];
530 if(net2str(netstr, sizeof(netstr), s)) {
531 logger(LOG_INFO, "Subnet %s expired", netstr);
535 for(node2 = connection_tree->head; node2; node2 = node2->next) {
538 if(c->status.active) {
539 send_del_subnet(c, s);
543 subnet_update(myself, s, false);
544 subnet_del(myself, s);
549 static void route_broadcast(node_t *source, vpn_packet_t *packet) {
550 if(decrement_ttl && source != myself)
551 if(!do_decrement_ttl(source, packet)) {
555 broadcast_packet(source, packet);
560 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t ether_size) {
562 vpn_packet_t fragment;
563 int len, maxlen, todo;
565 uint16_t ip_off, origf;
567 memcpy(&ip, packet->data + ether_size, ip_size);
568 fragment.priority = packet->priority;
570 if(ip.ip_hl != ip_size / 4) {
574 todo = ntohs(ip.ip_len) - ip_size;
576 if(ether_size + ip_size + todo != packet->len) {
577 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));
581 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
583 offset = packet->data + ether_size + ip_size;
584 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
585 ip_off = ntohs(ip.ip_off);
586 origf = ip_off & ~IP_OFFMASK;
587 ip_off &= IP_OFFMASK;
590 len = todo > maxlen ? maxlen : todo;
591 memcpy(fragment.data + ether_size + ip_size, offset, len);
595 ip.ip_len = htons(ip_size + len);
596 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
598 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
599 memcpy(fragment.data, packet->data, ether_size);
600 memcpy(fragment.data + ether_size, &ip, ip_size);
601 fragment.len = ether_size + ip_size + len;
603 send_packet(dest, &fragment);
609 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
614 memcpy(&dest, &packet->data[30], sizeof(dest));
615 subnet = lookup_subnet_ipv4(&dest);
618 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
619 source->name, source->hostname,
625 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
629 if(subnet->owner == source) {
630 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
634 if(!subnet->owner->status.reachable) {
635 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
639 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
640 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
644 if(decrement_ttl && source != myself && subnet->owner != myself)
645 if(!do_decrement_ttl(source, packet)) {
649 if(priorityinheritance) {
650 packet->priority = packet->data[15];
653 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
656 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
660 if(directonly && subnet->owner != via) {
661 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
665 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
666 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);
668 if(packet->data[20] & 0x40) {
669 packet->len = MAX(via->mtu, 590);
670 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
672 fragment_ipv4_packet(via, packet, ether_size);
678 clamp_mss(source, via, packet);
680 send_packet(subnet->owner, packet);
683 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
684 if(!checklength(source, packet, ether_size + ip_size)) {
688 if(broadcast_mode && (((packet->data[30] & 0xf0) == 0xe0) || (
689 packet->data[30] == 255 &&
690 packet->data[31] == 255 &&
691 packet->data[32] == 255 &&
692 packet->data[33] == 255))) {
693 route_broadcast(source, packet);
695 route_ipv4_unicast(source, packet);
699 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
704 memcpy(&dest, &packet->data[38], sizeof(dest));
705 subnet = lookup_subnet_ipv6(&dest);
708 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
709 source->name, source->hostname,
719 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
723 if(subnet->owner == source) {
724 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
728 if(!subnet->owner->status.reachable) {
729 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
733 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
734 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
738 if(decrement_ttl && source != myself && subnet->owner != myself) {
739 if(!do_decrement_ttl(source, packet)) {
744 if(priorityinheritance) {
745 packet->priority = ((packet->data[14] & 0x0f) << 4) | (packet->data[15] >> 4);
748 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
751 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
755 if(directonly && subnet->owner != via) {
756 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
760 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
761 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);
762 packet->len = MAX(via->mtu, 1294);
763 route_ipv6_unreachable(source, packet, ether_size, ICMP6_PACKET_TOO_BIG, 0);
767 clamp_mss(source, via, packet);
769 send_packet(subnet->owner, packet);
774 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
776 struct nd_neighbor_solicit ns;
777 struct nd_opt_hdr opt;
783 struct in6_addr ip6_src; /* source address */
784 struct in6_addr ip6_dst; /* destination address */
789 if(!checklength(source, packet, ether_size + ip6_size + ns_size)) {
793 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
795 if(source != myself) {
796 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
800 /* Copy headers from packet to structs on the stack */
802 memcpy(&ip6, packet->data + ether_size, ip6_size);
803 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
806 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
809 /* First, snatch the source address from the neighbor solicitation packet */
812 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
815 /* Check if this is a valid neighbor solicitation request */
817 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
818 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
819 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
823 /* Create pseudo header */
825 pseudo.ip6_src = ip6.ip6_src;
826 pseudo.ip6_dst = ip6.ip6_dst;
829 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
831 pseudo.length = htonl(ns_size);
834 pseudo.next = htonl(IPPROTO_ICMPV6);
836 /* Generate checksum */
838 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
839 checksum = inet_checksum(&ns, ns_size, checksum);
842 checksum = inet_checksum(&opt, opt_size, checksum);
843 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
847 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
851 /* Check if the IPv6 address exists on the VPN */
853 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
856 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
857 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
858 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
859 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
860 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
861 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
862 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
863 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
864 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
869 /* Check if it is for our own subnet */
871 if(subnet->owner == myself) {
872 return; /* silently ignore */
876 if(!do_decrement_ttl(source, packet)) {
880 /* Create neighbor advertation reply */
882 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
883 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
885 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
886 ip6.ip6_src = ns.nd_ns_target;
889 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
893 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
894 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
895 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
897 /* Create pseudo header */
899 pseudo.ip6_src = ip6.ip6_src;
900 pseudo.ip6_dst = ip6.ip6_dst;
903 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
905 pseudo.length = htonl(ns_size);
908 pseudo.next = htonl(IPPROTO_ICMPV6);
910 /* Generate checksum */
912 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
913 checksum = inet_checksum(&ns, ns_size, checksum);
916 checksum = inet_checksum(&opt, opt_size, checksum);
917 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
920 ns.nd_ns_hdr.icmp6_cksum = checksum;
922 /* Copy structs on stack back to packet */
924 memcpy(packet->data + ether_size, &ip6, ip6_size);
925 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
928 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
931 send_packet(source, packet);
934 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
935 if(!checklength(source, packet, ether_size + ip6_size)) {
939 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
940 route_neighborsol(source, packet);
944 if(broadcast_mode && packet->data[38] == 255) {
945 route_broadcast(source, packet);
947 route_ipv6_unicast(source, packet);
953 static void route_arp(node_t *source, vpn_packet_t *packet) {
954 struct ether_arp arp;
958 if(!checklength(source, packet, ether_size + arp_size)) {
962 if(source != myself) {
963 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
967 /* First, snatch the source address from the ARP packet */
970 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
973 /* Copy headers from packet to structs on the stack */
975 memcpy(&arp, packet->data + ether_size, arp_size);
977 /* Check if this is a valid ARP request */
979 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
980 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
981 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
985 /* Check if the IPv4 address exists on the VPN */
987 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
990 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
991 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
996 /* Check if it is for our own subnet */
998 if(subnet->owner == myself) {
999 return; /* silently ignore */
1003 if(!do_decrement_ttl(source, packet)) {
1007 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
1008 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
1010 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
1011 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
1012 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
1014 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
1015 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
1016 arp.arp_op = htons(ARPOP_REPLY);
1018 /* Copy structs on stack back to packet */
1020 memcpy(packet->data + ether_size, &arp, arp_size);
1022 send_packet(source, packet);
1025 static void route_mac(node_t *source, vpn_packet_t *packet) {
1029 /* Learn source address */
1031 if(source == myself) {
1033 memcpy(&src, &packet->data[6], sizeof(src));
1037 /* Lookup destination address */
1039 memcpy(&dest, &packet->data[0], sizeof(dest));
1040 subnet = lookup_subnet_mac(NULL, &dest);
1043 route_broadcast(source, packet);
1047 if(subnet->owner == source) {
1048 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
1052 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
1056 if(decrement_ttl && source != myself && subnet->owner != myself)
1057 if(!do_decrement_ttl(source, packet)) {
1061 uint16_t type = packet->data[12] << 8 | packet->data[13];
1063 if(priorityinheritance) {
1064 if(type == ETH_P_IP && packet->len >= ether_size + ip_size) {
1065 packet->priority = packet->data[15];
1066 } else if(type == ETH_P_IPV6 && packet->len >= ether_size + ip6_size) {
1067 packet->priority = ((packet->data[14] & 0x0f) << 4) | (packet->data[15] >> 4);
1071 // Handle packets larger than PMTU
1073 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
1075 if(directonly && subnet->owner != via) {
1079 if(via && packet->len > via->mtu && via != myself) {
1080 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);
1081 length_t ethlen = 14;
1083 if(type == ETH_P_8021Q) {
1084 type = packet->data[16] << 8 | packet->data[17];
1088 if(type == ETH_P_IP && packet->len > 576 + ethlen) {
1089 if(packet->data[6 + ethlen] & 0x40) {
1090 packet->len = via->mtu;
1091 route_ipv4_unreachable(source, packet, ethlen, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
1093 fragment_ipv4_packet(via, packet, ethlen);
1097 } else if(type == ETH_P_IPV6 && packet->len > 1280 + ethlen) {
1098 packet->len = via->mtu;
1099 route_ipv6_unreachable(source, packet, ethlen, ICMP6_PACKET_TOO_BIG, 0);
1104 clamp_mss(source, via, packet);
1106 send_packet(subnet->owner, packet);
1109 void route(node_t *source, vpn_packet_t *packet) {
1110 if(forwarding_mode == FMODE_KERNEL && source != myself) {
1111 send_packet(myself, packet);
1115 if(!checklength(source, packet, ether_size)) {
1119 switch(routing_mode) {
1120 case RMODE_ROUTER: {
1121 uint16_t type = packet->data[12] << 8 | packet->data[13];
1125 route_arp(source, packet);
1129 route_ipv4(source, packet);
1133 route_ipv6(source, packet);
1137 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
1144 route_mac(source, packet);
1148 route_broadcast(source, packet);