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 = {};
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 = {};
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 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
638 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
639 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
642 if(decrement_ttl && source != myself && subnet->owner != myself)
643 if(!do_decrement_ttl(source, packet)) {
647 if(priorityinheritance) {
648 packet->priority = packet->data[15];
651 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
654 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
658 if(directonly && subnet->owner != via) {
659 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
662 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
663 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);
665 if(packet->data[20] & 0x40) {
666 packet->len = MAX(via->mtu, 590);
667 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
669 fragment_ipv4_packet(via, packet, ether_size);
675 clamp_mss(source, via, packet);
677 send_packet(subnet->owner, packet);
680 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
681 if(!checklength(source, packet, ether_size + ip_size)) {
685 if(broadcast_mode && (((packet->data[30] & 0xf0) == 0xe0) || (
686 packet->data[30] == 255 &&
687 packet->data[31] == 255 &&
688 packet->data[32] == 255 &&
689 packet->data[33] == 255))) {
690 route_broadcast(source, packet);
692 route_ipv4_unicast(source, packet);
696 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
701 memcpy(&dest, &packet->data[38], sizeof(dest));
702 subnet = lookup_subnet_ipv6(&dest);
705 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
706 source->name, source->hostname,
716 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
720 if(subnet->owner == source) {
721 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
725 if(!subnet->owner->status.reachable) {
726 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
729 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
730 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
733 if(decrement_ttl && source != myself && subnet->owner != myself)
734 if(!do_decrement_ttl(source, packet)) {
738 if(priorityinheritance) {
739 packet->priority = ((packet->data[14] & 0x0f) << 4) | (packet->data[15] >> 4);
742 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
745 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
749 if(directonly && subnet->owner != via) {
750 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
753 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
754 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);
755 packet->len = MAX(via->mtu, 1294);
756 route_ipv6_unreachable(source, packet, ether_size, ICMP6_PACKET_TOO_BIG, 0);
760 clamp_mss(source, via, packet);
762 send_packet(subnet->owner, packet);
767 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
769 struct nd_neighbor_solicit ns;
770 struct nd_opt_hdr opt;
776 struct in6_addr ip6_src; /* source address */
777 struct in6_addr ip6_dst; /* destination address */
782 if(!checklength(source, packet, ether_size + ip6_size + ns_size)) {
786 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
788 if(source != myself) {
789 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
793 /* Copy headers from packet to structs on the stack */
795 memcpy(&ip6, packet->data + ether_size, ip6_size);
796 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
799 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
802 /* First, snatch the source address from the neighbor solicitation packet */
805 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
808 /* Check if this is a valid neighbor solicitation request */
810 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
811 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
812 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
816 /* Create pseudo header */
818 pseudo.ip6_src = ip6.ip6_src;
819 pseudo.ip6_dst = ip6.ip6_dst;
822 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
824 pseudo.length = htonl(ns_size);
827 pseudo.next = htonl(IPPROTO_ICMPV6);
829 /* Generate checksum */
831 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
832 checksum = inet_checksum(&ns, ns_size, checksum);
835 checksum = inet_checksum(&opt, opt_size, checksum);
836 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
840 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
844 /* Check if the IPv6 address exists on the VPN */
846 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
849 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
850 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
851 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
852 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
853 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
854 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
855 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
856 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
857 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
862 /* Check if it is for our own subnet */
864 if(subnet->owner == myself) {
865 return; /* silently ignore */
869 if(!do_decrement_ttl(source, packet)) {
873 /* Create neighbor advertation reply */
875 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
876 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
878 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
879 ip6.ip6_src = ns.nd_ns_target;
882 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
886 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
887 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
888 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
890 /* Create pseudo header */
892 pseudo.ip6_src = ip6.ip6_src;
893 pseudo.ip6_dst = ip6.ip6_dst;
896 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
898 pseudo.length = htonl(ns_size);
901 pseudo.next = htonl(IPPROTO_ICMPV6);
903 /* Generate checksum */
905 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
906 checksum = inet_checksum(&ns, ns_size, checksum);
909 checksum = inet_checksum(&opt, opt_size, checksum);
910 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
913 ns.nd_ns_hdr.icmp6_cksum = checksum;
915 /* Copy structs on stack back to packet */
917 memcpy(packet->data + ether_size, &ip6, ip6_size);
918 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
921 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
924 send_packet(source, packet);
927 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
928 if(!checklength(source, packet, ether_size + ip6_size)) {
932 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
933 route_neighborsol(source, packet);
937 if(broadcast_mode && packet->data[38] == 255) {
938 route_broadcast(source, packet);
940 route_ipv6_unicast(source, packet);
946 static void route_arp(node_t *source, vpn_packet_t *packet) {
947 struct ether_arp arp;
951 if(!checklength(source, packet, ether_size + arp_size)) {
955 if(source != myself) {
956 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
960 /* First, snatch the source address from the ARP packet */
963 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
966 /* Copy headers from packet to structs on the stack */
968 memcpy(&arp, packet->data + ether_size, arp_size);
970 /* Check if this is a valid ARP request */
972 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
973 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
974 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
978 /* Check if the IPv4 address exists on the VPN */
980 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
983 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
984 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
989 /* Check if it is for our own subnet */
991 if(subnet->owner == myself) {
992 return; /* silently ignore */
996 if(!do_decrement_ttl(source, packet)) {
1000 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
1001 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
1003 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
1004 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
1005 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
1007 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
1008 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
1009 arp.arp_op = htons(ARPOP_REPLY);
1011 /* Copy structs on stack back to packet */
1013 memcpy(packet->data + ether_size, &arp, arp_size);
1015 send_packet(source, packet);
1018 static void route_mac(node_t *source, vpn_packet_t *packet) {
1022 /* Learn source address */
1024 if(source == myself) {
1026 memcpy(&src, &packet->data[6], sizeof(src));
1030 /* Lookup destination address */
1032 memcpy(&dest, &packet->data[0], sizeof(dest));
1033 subnet = lookup_subnet_mac(NULL, &dest);
1036 route_broadcast(source, packet);
1040 if(subnet->owner == source) {
1041 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
1045 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
1049 if(decrement_ttl && source != myself && subnet->owner != myself)
1050 if(!do_decrement_ttl(source, packet)) {
1054 uint16_t type = packet->data[12] << 8 | packet->data[13];
1056 if(priorityinheritance) {
1057 if(type == ETH_P_IP && packet->len >= ether_size + ip_size) {
1058 packet->priority = packet->data[15];
1059 } else if(type == ETH_P_IPV6 && packet->len >= ether_size + ip6_size) {
1060 packet->priority = ((packet->data[14] & 0x0f) << 4) | (packet->data[15] >> 4);
1064 // Handle packets larger than PMTU
1066 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
1068 if(directonly && subnet->owner != via) {
1072 if(via && packet->len > via->mtu && via != myself) {
1073 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);
1074 length_t ethlen = 14;
1076 if(type == ETH_P_8021Q) {
1077 type = packet->data[16] << 8 | packet->data[17];
1081 if(type == ETH_P_IP && packet->len > 576 + ethlen) {
1082 if(packet->data[6 + ethlen] & 0x40) {
1083 packet->len = via->mtu;
1084 route_ipv4_unreachable(source, packet, ethlen, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
1086 fragment_ipv4_packet(via, packet, ethlen);
1090 } else if(type == ETH_P_IPV6 && packet->len > 1280 + ethlen) {
1091 packet->len = via->mtu;
1092 route_ipv6_unreachable(source, packet, ethlen, ICMP6_PACKET_TOO_BIG, 0);
1097 clamp_mss(source, via, packet);
1099 send_packet(subnet->owner, packet);
1102 void route(node_t *source, vpn_packet_t *packet) {
1103 if(forwarding_mode == FMODE_KERNEL && source != myself) {
1104 send_packet(myself, packet);
1108 if(!checklength(source, packet, ether_size)) {
1112 switch(routing_mode) {
1113 case RMODE_ROUTER: {
1114 uint16_t type = packet->data[12] << 8 | packet->data[13];
1118 route_arp(source, packet);
1122 route_ipv4(source, packet);
1126 route_ipv6(source, packet);
1130 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
1137 route_mac(source, packet);
1141 route_broadcast(source, packet);