3 Copyright (C) 2000-2005 Ivo Timmermans,
4 2000-2018 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.
23 #include "connection.h"
24 #include "control_common.h"
37 rmode_t routing_mode = RMODE_ROUTER;
38 fmode_t forwarding_mode = FMODE_INTERNAL;
39 bmode_t broadcast_mode = BMODE_MST;
40 bool decrement_ttl = false;
41 bool directonly = false;
42 bool priorityinheritance = false;
44 bool overwrite_mac = false;
45 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
48 /* Sizes of various headers */
50 static const size_t ether_size = sizeof(struct ether_header);
51 static const size_t arp_size = sizeof(struct ether_arp);
52 static const size_t ip_size = sizeof(struct ip);
53 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
54 static const size_t ip6_size = sizeof(struct ip6_hdr);
55 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
56 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
57 static const size_t opt_size = sizeof(struct nd_opt_hdr);
59 static timeout_t age_subnets_timeout;
63 static uint16_t inet_checksum(void *vdata, size_t len, uint16_t prevsum) {
64 uint8_t *data = vdata;
66 uint32_t checksum = prevsum ^ 0xFFFF;
69 memcpy(&word, data, sizeof(word));
79 while(checksum >> 16) {
80 checksum = (checksum & 0xFFFF) + (checksum >> 16);
83 return (uint16_t) ~checksum;
86 static bool ratelimit(int frequency) {
87 static time_t lasttime = 0;
90 if(lasttime == now.tv_sec) {
91 if(count >= frequency) {
95 lasttime = now.tv_sec;
103 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
104 if(packet->len < length) {
105 logger(DEBUG_TRAFFIC, LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
112 static void swap_mac_addresses(vpn_packet_t *packet) {
114 memcpy(&tmp, &DATA(packet)[0], sizeof(tmp));
115 memcpy(&DATA(packet)[0], &DATA(packet)[6], sizeof(tmp));
116 memcpy(&DATA(packet)[6], &tmp, sizeof(tmp));
121 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
123 struct icmp icmp = {0};
125 struct in_addr ip_src;
126 struct in_addr ip_dst;
133 /* Swap Ethernet source and destination addresses */
135 swap_mac_addresses(packet);
137 /* Copy headers from packet into properly aligned structs on the stack */
139 memcpy(&ip, DATA(packet) + ether_size, ip_size);
141 /* Remember original source and destination */
146 /* Try to reply with an IP address assigned to the local machine */
148 if(type == ICMP_TIME_EXCEEDED && code == ICMP_EXC_TTL) {
149 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
152 struct sockaddr_in addr;
153 memset(&addr, 0, sizeof(addr));
154 addr.sin_family = AF_INET;
155 addr.sin_addr = ip.ip_src;
157 if(!connect(sockfd, (const struct sockaddr *) &addr, sizeof(addr))) {
158 memset(&addr, 0, sizeof(addr));
159 addr.sin_family = AF_INET;
160 socklen_t addrlen = sizeof(addr);
162 if(!getsockname(sockfd, (struct sockaddr *) &addr, &addrlen) && (size_t)addrlen <= sizeof(addr)) {
163 ip_dst = addr.sin_addr;
171 oldlen = packet->len - ether_size;
173 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
174 icmp.icmp_nextmtu = htons(packet->len - ether_size);
177 if(oldlen >= IP_MSS - ip_size - icmp_size) {
178 oldlen = IP_MSS - ip_size - icmp_size;
181 /* Copy first part of original contents to ICMP message */
183 memmove(DATA(packet) + ether_size + ip_size + icmp_size, DATA(packet) + ether_size, oldlen);
185 /* Fill in IPv4 header */
188 ip.ip_hl = ip_size / 4;
190 ip.ip_len = htons(ip_size + icmp_size + oldlen);
194 ip.ip_p = IPPROTO_ICMP;
199 ip.ip_sum = inet_checksum(&ip, ip_size, 0xFFFF);
201 /* Fill in ICMP header */
203 icmp.icmp_type = type;
204 icmp.icmp_code = code;
207 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, 0xFFFF);
208 icmp.icmp_cksum = inet_checksum(DATA(packet) + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
210 /* Copy structs on stack back to packet */
212 memcpy(DATA(packet) + ether_size, &ip, ip_size);
213 memcpy(DATA(packet) + ether_size + ip_size, &icmp, icmp_size);
215 packet->len = ether_size + ip_size + icmp_size + oldlen;
217 send_packet(source, packet);
222 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
224 struct icmp6_hdr icmp6 = {0};
228 struct in6_addr ip6_src; /* source address */
229 struct in6_addr ip6_dst; /* destination address */
238 /* Swap Ethernet source and destination addresses */
240 swap_mac_addresses(packet);
242 /* Copy headers from packet to structs on the stack */
244 memcpy(&ip6, DATA(packet) + ether_size, ip6_size);
246 /* Remember original source and destination */
248 pseudo.ip6_src = ip6.ip6_dst;
249 pseudo.ip6_dst = ip6.ip6_src;
251 /* Try to reply with an IP address assigned to the local machine */
253 if(type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT) {
254 int sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
257 struct sockaddr_in6 addr;
258 memset(&addr, 0, sizeof(addr));
259 addr.sin6_family = AF_INET6;
260 addr.sin6_addr = ip6.ip6_src;
262 if(!connect(sockfd, (const struct sockaddr *) &addr, sizeof(addr))) {
263 memset(&addr, 0, sizeof(addr));
264 addr.sin6_family = AF_INET6;
265 socklen_t addrlen = sizeof(addr);
267 if(!getsockname(sockfd, (struct sockaddr *) &addr, &addrlen) && (size_t)addrlen <= sizeof(addr)) {
268 pseudo.ip6_src = addr.sin6_addr;
276 pseudo.length = packet->len - ether_size;
278 if(type == ICMP6_PACKET_TOO_BIG) {
279 icmp6.icmp6_mtu = htonl(pseudo.length);
282 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size) {
283 pseudo.length = IP_MSS - ip6_size - icmp6_size;
286 /* Copy first part of original contents to ICMP message */
288 memmove(DATA(packet) + ether_size + ip6_size + icmp6_size, DATA(packet) + ether_size, pseudo.length);
290 /* Fill in IPv6 header */
292 ip6.ip6_flow = htonl(0x60000000UL);
293 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
294 ip6.ip6_nxt = IPPROTO_ICMPV6;
296 ip6.ip6_src = pseudo.ip6_src;
297 ip6.ip6_dst = pseudo.ip6_dst;
299 /* Fill in ICMP header */
301 icmp6.icmp6_type = type;
302 icmp6.icmp6_code = code;
303 icmp6.icmp6_cksum = 0;
305 /* Create pseudo header */
307 pseudo.length = htonl(icmp6_size + pseudo.length);
308 pseudo.next = htonl(IPPROTO_ICMPV6);
310 /* Generate checksum */
312 checksum = inet_checksum(&pseudo, sizeof(pseudo), 0xFFFF);
313 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
314 checksum = inet_checksum(DATA(packet) + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
316 icmp6.icmp6_cksum = checksum;
318 /* Copy structs on stack back to packet */
320 memcpy(DATA(packet) + ether_size, &ip6, ip6_size);
321 memcpy(DATA(packet) + ether_size + ip6_size, &icmp6, icmp6_size);
323 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
325 send_packet(source, packet);
328 static bool do_decrement_ttl(node_t *source, vpn_packet_t *packet) {
329 uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
330 length_t ethlen = ether_size;
332 if(type == ETH_P_8021Q) {
333 type = DATA(packet)[16] << 8 | DATA(packet)[17];
339 if(!checklength(source, packet, ethlen + ip_size)) {
343 if(DATA(packet)[ethlen + 8] <= 1) {
344 if(DATA(packet)[ethlen + 11] != IPPROTO_ICMP || DATA(packet)[ethlen + 32] != ICMP_TIME_EXCEEDED) {
345 route_ipv4_unreachable(source, packet, ethlen, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL);
351 uint16_t old = DATA(packet)[ethlen + 8] << 8 | DATA(packet)[ethlen + 9];
352 DATA(packet)[ethlen + 8]--;
353 uint16_t new = DATA(packet)[ethlen + 8] << 8 | DATA(packet)[ethlen + 9];
355 uint32_t checksum = DATA(packet)[ethlen + 10] << 8 | DATA(packet)[ethlen + 11];
356 checksum += old + (~new & 0xFFFF);
358 while(checksum >> 16) {
359 checksum = (checksum & 0xFFFF) + (checksum >> 16);
362 DATA(packet)[ethlen + 10] = checksum >> 8;
363 DATA(packet)[ethlen + 11] = checksum & 0xff;
368 if(!checklength(source, packet, ethlen + ip6_size)) {
372 if(DATA(packet)[ethlen + 7] <= 1) {
373 if(DATA(packet)[ethlen + 6] != IPPROTO_ICMPV6 || DATA(packet)[ethlen + 40] != ICMP6_TIME_EXCEEDED) {
374 route_ipv6_unreachable(source, packet, ethlen, ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT);
380 DATA(packet)[ethlen + 7]--;
389 static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
390 if(!source || !via || !(via->options & OPTION_CLAMP_MSS)) {
394 uint16_t mtu = source->mtu;
396 if(via != myself && via->mtu < mtu) {
400 /* Find TCP header */
401 size_t start = ether_size;
402 uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
404 if(type == ETH_P_8021Q) {
406 type = DATA(packet)[16] << 8 | DATA(packet)[17];
409 /* IP in IP (RFC 2003) packet */
410 if(type == ETH_P_IP && DATA(packet)[start + 9] == 4) {
414 if(packet->len <= start + 20) {
418 if(type == ETH_P_IP && DATA(packet)[start + 9] == 6) {
419 start += (DATA(packet)[start] & 0xf) * 4;
420 } else if(type == ETH_P_IPV6 && DATA(packet)[start + 6] == 6) {
426 if(packet->len <= start + 20) {
430 /* Use data offset field to calculate length of options field */
431 int len = ((DATA(packet)[start + 12] >> 4) - 5) * 4;
433 if(packet->len < start + 20 + len) {
437 /* Search for MSS option header */
438 for(int i = 0; i < len;) {
439 if(DATA(packet)[start + 20 + i] == 0) {
443 if(DATA(packet)[start + 20 + i] == 1) {
448 if(i > len - 2 || i > len - DATA(packet)[start + 21 + i]) {
452 if(DATA(packet)[start + 20 + i] != 2) {
453 if(DATA(packet)[start + 21 + i] < 2) {
457 i += DATA(packet)[start + 21 + i];
461 if(DATA(packet)[start + 21] != 4) {
466 uint16_t oldmss = DATA(packet)[start + 22 + i] << 8 | DATA(packet)[start + 23 + i];
467 uint16_t newmss = mtu - start - 20;
468 uint32_t csum = DATA(packet)[start + 16] << 8 | DATA(packet)[start + 17];
470 if(oldmss <= newmss) {
474 logger(DEBUG_TRAFFIC, LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
476 /* Update the MSS value and the checksum */
477 DATA(packet)[start + 22 + i] = newmss >> 8;
478 DATA(packet)[start + 23 + i] = newmss & 0xff;
480 csum += oldmss ^ 0xffff;
482 csum = (csum & 0xffff) + (csum >> 16);
485 DATA(packet)[start + 16] = csum >> 8;
486 DATA(packet)[start + 17] = csum & 0xff;
491 static void age_subnets(void *data) {
495 for splay_each(subnet_t, s, &myself->subnet_tree) {
496 if(s->expires && s->expires < now.tv_sec) {
497 if(debug_level >= DEBUG_TRAFFIC) {
498 char netstr[MAXNETSTR];
500 if(net2str(netstr, sizeof(netstr), s)) {
501 logger(DEBUG_TRAFFIC, LOG_INFO, "Subnet %s expired", netstr);
505 for list_each(connection_t, c, &connection_list)
507 send_del_subnet(c, s);
510 subnet_del(myself, s);
519 timeout_set(&age_subnets_timeout, &(struct timeval) {
524 static void learn_mac(mac_t *address) {
525 subnet_t *subnet = lookup_subnet_mac(myself, address);
527 /* If we don't know this MAC address yet, store it */
530 logger(DEBUG_TRAFFIC, LOG_INFO, "Learned new MAC address %x:%x:%x:%x:%x:%x",
531 address->x[0], address->x[1], address->x[2], address->x[3],
532 address->x[4], address->x[5]);
534 subnet = new_subnet();
535 subnet->type = SUBNET_MAC;
536 subnet->expires = now.tv_sec + macexpire;
537 subnet->net.mac.address = *address;
539 subnet_add(myself, subnet);
540 subnet_update(myself, subnet, true);
542 /* And tell all other tinc daemons it's our MAC */
544 for list_each(connection_t, c, &connection_list)
546 send_add_subnet(c, subnet);
549 timeout_add(&age_subnets_timeout, age_subnets, NULL, &(struct timeval) {
553 if(subnet->expires) {
554 subnet->expires = now.tv_sec + macexpire;
559 static void route_broadcast(node_t *source, vpn_packet_t *packet) {
560 if(decrement_ttl && source != myself)
561 if(!do_decrement_ttl(source, packet)) {
565 broadcast_packet(source, packet);
570 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t ether_size) {
572 vpn_packet_t fragment;
575 uint16_t ip_off, origf;
577 memcpy(&ip, DATA(packet) + ether_size, ip_size);
578 fragment.priority = packet->priority;
579 fragment.offset = DEFAULT_PACKET_OFFSET;
581 if(ip.ip_hl != ip_size / 4) {
585 todo = ntohs(ip.ip_len) - ip_size;
587 if(ether_size + ip_size + todo != packet->len) {
588 logger(DEBUG_TRAFFIC, LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%lu)", packet->len, (unsigned long)(ether_size + ip_size + todo));
592 logger(DEBUG_TRAFFIC, LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
594 offset = DATA(packet) + ether_size + ip_size;
595 maxlen = (MAX(dest->mtu, 590) - ether_size - ip_size) & ~0x7;
596 ip_off = ntohs(ip.ip_off);
597 origf = ip_off & ~IP_OFFMASK;
598 ip_off &= IP_OFFMASK;
601 size_t len = todo > maxlen ? maxlen : todo;
602 memcpy(DATA(&fragment) + ether_size + ip_size, offset, len);
606 ip.ip_len = htons(ip_size + len);
607 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
609 ip.ip_sum = inet_checksum(&ip, ip_size, 0xFFFF);
610 memcpy(DATA(&fragment), DATA(packet), ether_size);
611 memcpy(DATA(&fragment) + ether_size, &ip, ip_size);
612 fragment.len = ether_size + ip_size + len;
614 send_packet(dest, &fragment);
620 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
621 if(!checklength(source, packet, ether_size + ip_size)) {
629 memcpy(&dest, &DATA(packet)[30], sizeof(dest));
630 subnet = lookup_subnet_ipv4(&dest);
633 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
634 source->name, source->hostname,
640 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
645 route_broadcast(source, packet);
649 if(subnet->owner == source) {
650 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
654 if(!subnet->owner->status.reachable) {
655 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
659 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
660 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
664 if(decrement_ttl && source != myself && subnet->owner != myself)
665 if(!do_decrement_ttl(source, packet)) {
669 if(priorityinheritance) {
670 packet->priority = DATA(packet)[15];
673 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
676 logger(DEBUG_TRAFFIC, LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
680 if(directonly && subnet->owner != via) {
681 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
685 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
686 logger(DEBUG_TRAFFIC, LOG_INFO, "Packet for %s (%s) length %d larger than MTU %d", subnet->owner->name, subnet->owner->hostname, packet->len, via->mtu);
688 if(DATA(packet)[20] & 0x40) {
689 packet->len = MAX(via->mtu, 590);
690 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
692 fragment_ipv4_packet(via, packet, ether_size);
698 clamp_mss(source, via, packet);
700 send_packet(subnet->owner, packet);
703 static void route_neighborsol(node_t *source, vpn_packet_t *packet);
705 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
706 if(!checklength(source, packet, ether_size + ip6_size)) {
710 if(DATA(packet)[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && DATA(packet)[54] == ND_NEIGHBOR_SOLICIT) {
711 route_neighborsol(source, packet);
719 memcpy(&dest, &DATA(packet)[38], sizeof(dest));
720 subnet = lookup_subnet_ipv6(&dest);
723 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
724 source->name, source->hostname,
734 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
739 route_broadcast(source, packet);
743 if(subnet->owner == source) {
744 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
748 if(!subnet->owner->status.reachable) {
749 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
753 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
754 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
758 if(decrement_ttl && source != myself && subnet->owner != myself)
759 if(!do_decrement_ttl(source, packet)) {
763 if(priorityinheritance) {
764 packet->priority = ((DATA(packet)[14] & 0x0f) << 4) | (DATA(packet)[15] >> 4);
767 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
770 logger(DEBUG_TRAFFIC, LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
774 if(directonly && subnet->owner != via) {
775 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
779 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
780 logger(DEBUG_TRAFFIC, LOG_INFO, "Packet for %s (%s) length %d larger than MTU %d", subnet->owner->name, subnet->owner->hostname, packet->len, via->mtu);
781 packet->len = MAX(via->mtu, 1294);
782 route_ipv6_unreachable(source, packet, ether_size, ICMP6_PACKET_TOO_BIG, 0);
786 clamp_mss(source, via, packet);
788 send_packet(subnet->owner, packet);
793 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
795 struct nd_neighbor_solicit ns;
796 struct nd_opt_hdr opt;
802 struct in6_addr ip6_src;
803 struct in6_addr ip6_dst;
808 if(!checklength(source, packet, ether_size + ip6_size + ns_size)) {
812 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
814 if(source != myself) {
815 logger(DEBUG_TRAFFIC, LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
819 /* Copy headers from packet to structs on the stack */
821 memcpy(&ip6, DATA(packet) + ether_size, ip6_size);
822 memcpy(&ns, DATA(packet) + ether_size + ip6_size, ns_size);
825 memcpy(&opt, DATA(packet) + ether_size + ip6_size + ns_size, opt_size);
828 /* First, snatch the source address from the neighbor solicitation packet */
831 memcpy(mymac.x, DATA(packet) + ETH_ALEN, ETH_ALEN);
834 /* Check if this is a valid neighbor solicitation request */
836 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
837 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
838 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
842 /* Create pseudo header */
844 pseudo.ip6_src = ip6.ip6_src;
845 pseudo.ip6_dst = ip6.ip6_dst;
848 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
850 pseudo.length = htonl(ns_size);
853 pseudo.next = htonl(IPPROTO_ICMPV6);
855 /* Generate checksum */
857 checksum = inet_checksum(&pseudo, sizeof(pseudo), 0xFFFF);
858 checksum = inet_checksum(&ns, ns_size, checksum);
861 checksum = inet_checksum(&opt, opt_size, checksum);
862 checksum = inet_checksum(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
866 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
870 /* Check if the IPv6 address exists on the VPN */
872 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
875 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
876 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
877 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
878 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
879 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
880 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
881 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
882 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
883 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
888 /* Check if it is for our own subnet */
890 if(subnet->owner == myself) {
891 return; /* silently ignore */
895 if(!do_decrement_ttl(source, packet)) {
899 /* Create neighbor advertation reply */
901 memcpy(DATA(packet), DATA(packet) + ETH_ALEN, ETH_ALEN); /* copy destination address */
902 DATA(packet)[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
904 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocol address */
905 ip6.ip6_src = ns.nd_ns_target;
908 memcpy(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, DATA(packet) + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
912 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
913 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
914 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
916 /* Create pseudo header */
918 pseudo.ip6_src = ip6.ip6_src;
919 pseudo.ip6_dst = ip6.ip6_dst;
922 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
924 pseudo.length = htonl(ns_size);
927 pseudo.next = htonl(IPPROTO_ICMPV6);
929 /* Generate checksum */
931 checksum = inet_checksum(&pseudo, sizeof(pseudo), 0xFFFF);
932 checksum = inet_checksum(&ns, ns_size, checksum);
935 checksum = inet_checksum(&opt, opt_size, checksum);
936 checksum = inet_checksum(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
939 ns.nd_ns_hdr.icmp6_cksum = checksum;
941 /* Copy structs on stack back to packet */
943 memcpy(DATA(packet) + ether_size, &ip6, ip6_size);
944 memcpy(DATA(packet) + ether_size + ip6_size, &ns, ns_size);
947 memcpy(DATA(packet) + ether_size + ip6_size + ns_size, &opt, opt_size);
950 send_packet(source, packet);
955 static void route_arp(node_t *source, vpn_packet_t *packet) {
956 struct ether_arp arp;
960 if(!checklength(source, packet, ether_size + arp_size)) {
964 if(source != myself) {
965 logger(DEBUG_TRAFFIC, LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
969 /* First, snatch the source address from the ARP packet */
972 memcpy(mymac.x, DATA(packet) + ETH_ALEN, ETH_ALEN);
975 /* Copy headers from packet to structs on the stack */
977 memcpy(&arp, DATA(packet) + ether_size, arp_size);
979 /* Check if this is a valid ARP request */
981 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
982 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
983 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: received unknown type ARP request");
987 /* Check if the IPv4 address exists on the VPN */
989 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
992 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
993 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
998 /* Check if it is for our own subnet */
1000 if(subnet->owner == myself) {
1001 return; /* silently ignore */
1005 if(!do_decrement_ttl(source, packet)) {
1009 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
1010 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
1011 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
1013 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
1014 memcpy(arp.arp_sha, DATA(packet) + ETH_ALEN, ETH_ALEN); /* set source hard/proto addr */
1015 arp.arp_sha[ETH_ALEN - 1] ^= 0xFF; /* for consistency with route_packet() */
1016 arp.arp_op = htons(ARPOP_REPLY);
1018 /* Copy structs on stack back to packet */
1020 memcpy(DATA(packet) + 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, &DATA(packet)[6], sizeof(src));
1037 /* Lookup destination address */
1039 memcpy(&dest, &DATA(packet)[0], sizeof(dest));
1040 subnet = lookup_subnet_mac(NULL, &dest);
1042 if(!subnet || !subnet->owner) {
1043 route_broadcast(source, packet);
1047 if(subnet->owner == source) {
1048 logger(DEBUG_TRAFFIC, 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 = DATA(packet)[12] << 8 | DATA(packet)[13];
1063 if(priorityinheritance) {
1064 if(type == ETH_P_IP && packet->len >= ether_size + ip_size) {
1065 packet->priority = DATA(packet)[15];
1066 } else if(type == ETH_P_IPV6 && packet->len >= ether_size + ip6_size) {
1067 packet->priority = ((DATA(packet)[14] & 0x0f) << 4) | (DATA(packet)[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 logger(DEBUG_TRAFFIC, 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 = DATA(packet)[16] << 8 | DATA(packet)[17];
1088 if(type == ETH_P_IP && packet->len > 576 + ethlen) {
1089 if(DATA(packet)[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 static void send_pcap(vpn_packet_t *packet) {
1112 for list_each(connection_t, c, &connection_list) {
1113 if(!c->status.pcap) {
1118 int len = packet->len;
1120 if(c->outmaclength && c->outmaclength < len) {
1121 len = c->outmaclength;
1124 if(send_request(c, "%d %d %d", CONTROL, REQ_PCAP, len)) {
1125 send_meta(c, DATA(packet), len);
1130 void route(node_t *source, vpn_packet_t *packet) {
1135 if(forwarding_mode == FMODE_KERNEL && source != myself) {
1136 send_packet(myself, packet);
1140 if(!checklength(source, packet, ether_size)) {
1144 uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
1146 switch(routing_mode) {
1150 route_arp(source, packet);
1154 route_ipv4(source, packet);
1158 route_ipv6(source, packet);
1162 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
1169 route_mac(source, packet);
1173 route_broadcast(source, packet);