3 Copyright (C) 2000-2005 Ivo Timmermans,
4 2000-2014 Guus Sliepen <guus@tinc-vpn.org>
5 2015 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 checksum = (checksum & 0xFFFF) + (checksum >> 16);
81 static bool ratelimit(int frequency) {
82 static time_t lasttime = 0;
86 if(count >= frequency)
97 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
98 if(packet->len < length) {
99 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
105 static void swap_mac_addresses(vpn_packet_t *packet) {
107 memcpy(&tmp, &packet->data[0], sizeof tmp);
108 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
109 memcpy(&packet->data[6], &tmp, sizeof tmp);
114 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
116 struct icmp icmp = {0};
118 struct in_addr ip_src;
119 struct in_addr ip_dst;
125 /* Swap Ethernet source and destination addresses */
127 swap_mac_addresses(packet);
129 /* Copy headers from packet into properly aligned structs on the stack */
131 memcpy(&ip, packet->data + ether_size, ip_size);
133 /* Remember original source and destination */
138 /* Try to reply with an IP address assigned to the local machine */
140 if (type == ICMP_TIME_EXCEEDED && code == ICMP_EXC_TTL) {
141 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
143 struct sockaddr_in addr;
144 memset(&addr, 0, sizeof(addr));
145 addr.sin_family = AF_INET;
146 addr.sin_addr = ip.ip_src;
147 if (!connect(sockfd, (const struct sockaddr*) &addr, sizeof(addr))) {
148 memset(&addr, 0, sizeof(addr));
149 addr.sin_family = AF_INET;
150 socklen_t addrlen = sizeof(addr);
151 if (!getsockname(sockfd, (struct sockaddr*) &addr, &addrlen) && addrlen <= sizeof(addr)) {
152 ip_dst = addr.sin_addr;
159 oldlen = packet->len - ether_size;
161 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
162 icmp.icmp_nextmtu = htons(packet->len - ether_size);
164 if(oldlen >= IP_MSS - ip_size - icmp_size)
165 oldlen = IP_MSS - ip_size - icmp_size;
167 /* Copy first part of original contents to ICMP message */
169 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
171 /* Fill in IPv4 header */
174 ip.ip_hl = ip_size / 4;
176 ip.ip_len = htons(ip_size + icmp_size + oldlen);
180 ip.ip_p = IPPROTO_ICMP;
185 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
187 /* Fill in ICMP header */
189 icmp.icmp_type = type;
190 icmp.icmp_code = code;
193 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
194 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
196 /* Copy structs on stack back to packet */
198 memcpy(packet->data + ether_size, &ip, ip_size);
199 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
201 packet->len = ether_size + ip_size + icmp_size + oldlen;
203 send_packet(source, packet);
208 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
210 struct icmp6_hdr icmp6 = {0};
214 struct in6_addr ip6_src; /* source address */
215 struct in6_addr ip6_dst; /* destination address */
223 /* Swap Ethernet source and destination addresses */
225 swap_mac_addresses(packet);
227 /* Copy headers from packet to structs on the stack */
229 memcpy(&ip6, packet->data + ether_size, ip6_size);
231 /* Remember original source and destination */
233 pseudo.ip6_src = ip6.ip6_dst;
234 pseudo.ip6_dst = ip6.ip6_src;
236 /* Try to reply with an IP address assigned to the local machine */
238 if (type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT) {
239 int sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
241 struct sockaddr_in6 addr;
242 memset(&addr, 0, sizeof(addr));
243 addr.sin6_family = AF_INET6;
244 addr.sin6_addr = ip6.ip6_src;
245 if (!connect(sockfd, (const struct sockaddr*) &addr, sizeof(addr))) {
246 memset(&addr, 0, sizeof(addr));
247 addr.sin6_family = AF_INET6;
248 socklen_t addrlen = sizeof(addr);
249 if (!getsockname(sockfd, (struct sockaddr*) &addr, &addrlen) && addrlen <= sizeof(addr)) {
250 pseudo.ip6_src = addr.sin6_addr;
257 pseudo.length = packet->len - ether_size;
259 if(type == ICMP6_PACKET_TOO_BIG)
260 icmp6.icmp6_mtu = htonl(pseudo.length);
262 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
263 pseudo.length = IP_MSS - ip6_size - icmp6_size;
265 /* Copy first part of original contents to ICMP message */
267 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
269 /* Fill in IPv6 header */
271 ip6.ip6_flow = htonl(0x60000000UL);
272 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
273 ip6.ip6_nxt = IPPROTO_ICMPV6;
275 ip6.ip6_src = pseudo.ip6_src;
276 ip6.ip6_dst = pseudo.ip6_dst;
278 /* Fill in ICMP header */
280 icmp6.icmp6_type = type;
281 icmp6.icmp6_code = code;
282 icmp6.icmp6_cksum = 0;
284 /* Create pseudo header */
286 pseudo.length = htonl(icmp6_size + pseudo.length);
287 pseudo.next = htonl(IPPROTO_ICMPV6);
289 /* Generate checksum */
291 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
292 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
293 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
295 icmp6.icmp6_cksum = checksum;
297 /* Copy structs on stack back to packet */
299 memcpy(packet->data + ether_size, &ip6, ip6_size);
300 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
302 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
304 send_packet(source, packet);
307 static bool do_decrement_ttl(node_t *source, vpn_packet_t *packet) {
308 uint16_t type = packet->data[12] << 8 | packet->data[13];
309 length_t ethlen = ether_size;
311 if(type == ETH_P_8021Q) {
312 type = packet->data[16] << 8 | packet->data[17];
318 if(!checklength(source, packet, ethlen + ip_size))
321 if(packet->data[ethlen + 8] <= 1) {
322 if(packet->data[ethlen + 11] != IPPROTO_ICMP || packet->data[ethlen + 32] != ICMP_TIME_EXCEEDED)
323 route_ipv4_unreachable(source, packet, ethlen, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL);
327 uint16_t old = packet->data[ethlen + 8] << 8 | packet->data[ethlen + 9];
328 packet->data[ethlen + 8]--;
329 uint16_t new = packet->data[ethlen + 8] << 8 | packet->data[ethlen + 9];
331 uint32_t checksum = packet->data[ethlen + 10] << 8 | packet->data[ethlen + 11];
332 checksum += old + (~new & 0xFFFF);
333 while(checksum >> 16)
334 checksum = (checksum & 0xFFFF) + (checksum >> 16);
335 packet->data[ethlen + 10] = checksum >> 8;
336 packet->data[ethlen + 11] = checksum & 0xff;
341 if(!checklength(source, packet, ethlen + ip6_size))
344 if(packet->data[ethlen + 7] <= 1) {
345 if(packet->data[ethlen + 6] != IPPROTO_ICMPV6 || packet->data[ethlen + 40] != ICMP6_TIME_EXCEEDED)
346 route_ipv6_unreachable(source, packet, ethlen, ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT);
350 packet->data[ethlen + 7]--;
359 static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
360 if(!source || !via || !(via->options & OPTION_CLAMP_MSS))
363 uint16_t mtu = source->mtu;
364 if(via != myself && via->mtu < mtu)
367 /* Find TCP header */
368 int start = ether_size;
369 uint16_t type = packet->data[12] << 8 | packet->data[13];
371 if(type == ETH_P_8021Q) {
373 type = packet->data[16] << 8 | packet->data[17];
376 if(type == ETH_P_IP && packet->data[start + 9] == 6)
377 start += (packet->data[start] & 0xf) * 4;
378 else if(type == ETH_P_IPV6 && packet->data[start + 6] == 6)
383 if(packet->len <= start + 20)
386 /* Use data offset field to calculate length of options field */
387 int len = ((packet->data[start + 12] >> 4) - 5) * 4;
389 if(packet->len < start + 20 + len)
392 /* Search for MSS option header */
393 for(int i = 0; i < len;) {
394 if(packet->data[start + 20 + i] == 0)
397 if(packet->data[start + 20 + i] == 1) {
402 if(i > len - 2 || i > len - packet->data[start + 21 + i])
405 if(packet->data[start + 20 + i] != 2) {
406 if(packet->data[start + 21 + i] < 2)
408 i += packet->data[start + 21 + i];
412 if(packet->data[start + 21] != 4)
416 uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
417 uint16_t newmss = mtu - start - 20;
418 uint32_t csum = packet->data[start + 16] << 8 | packet->data[start + 17];
423 ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
425 /* Update the MSS value and the checksum */
426 packet->data[start + 22 + i] = newmss >> 8;
427 packet->data[start + 23 + i] = newmss & 0xff;
429 csum += oldmss ^ 0xffff;
431 csum = (csum & 0xffff) + (csum >> 16);
434 packet->data[start + 16] = csum >> 8;
435 packet->data[start + 17] = csum;
440 static void learn_mac(mac_t *address) {
445 subnet = lookup_subnet_mac(myself, address);
447 /* If we don't know this MAC address yet, store it */
450 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %x:%x:%x:%x:%x:%x",
451 address->x[0], address->x[1], address->x[2], address->x[3],
452 address->x[4], address->x[5]);
454 subnet = new_subnet();
455 subnet->type = SUBNET_MAC;
456 subnet->expires = now + macexpire;
457 subnet->net.mac.address = *address;
459 subnet_add(myself, subnet);
460 subnet_update(myself, subnet, true);
462 /* And tell all other tinc daemons it's our MAC */
464 for(node = connection_tree->head; node; node = node->next) {
467 send_add_subnet(c, subnet);
472 subnet->expires = now + macexpire;
475 void age_subnets(void) {
478 avl_node_t *node, *next, *node2;
480 for(node = myself->subnet_tree->head; node; node = next) {
483 if(s->expires && s->expires <= now) {
485 char netstr[MAXNETSTR];
486 if(net2str(netstr, sizeof netstr, s))
487 logger(LOG_INFO, "Subnet %s expired", netstr);
490 for(node2 = connection_tree->head; node2; node2 = node2->next) {
493 send_del_subnet(c, s);
496 subnet_update(myself, s, false);
497 subnet_del(myself, s);
502 static void route_broadcast(node_t *source, vpn_packet_t *packet) {
503 if(decrement_ttl && source != myself)
504 if(!do_decrement_ttl(source, packet))
507 broadcast_packet(source, packet);
512 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t ether_size) {
514 vpn_packet_t fragment;
515 int len, maxlen, todo;
517 uint16_t ip_off, origf;
519 memcpy(&ip, packet->data + ether_size, ip_size);
520 fragment.priority = packet->priority;
522 if(ip.ip_hl != ip_size / 4)
525 todo = ntohs(ip.ip_len) - ip_size;
527 if(ether_size + ip_size + todo != packet->len) {
528 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));
532 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
534 offset = packet->data + ether_size + ip_size;
535 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
536 ip_off = ntohs(ip.ip_off);
537 origf = ip_off & ~IP_OFFMASK;
538 ip_off &= IP_OFFMASK;
541 len = todo > maxlen ? maxlen : todo;
542 memcpy(fragment.data + ether_size + ip_size, offset, len);
546 ip.ip_len = htons(ip_size + len);
547 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
549 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
550 memcpy(fragment.data, packet->data, ether_size);
551 memcpy(fragment.data + ether_size, &ip, ip_size);
552 fragment.len = ether_size + ip_size + len;
554 send_packet(dest, &fragment);
560 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
565 memcpy(&dest, &packet->data[30], sizeof dest);
566 subnet = lookup_subnet_ipv4(&dest);
569 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
570 source->name, source->hostname,
576 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
580 if(subnet->owner == source) {
581 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
585 if(!subnet->owner->status.reachable)
586 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
588 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
589 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
591 if(decrement_ttl && source != myself && subnet->owner != myself)
592 if(!do_decrement_ttl(source, packet))
595 if(priorityinheritance)
596 packet->priority = packet->data[15];
598 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
601 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
605 if(directonly && subnet->owner != via)
606 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
608 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
609 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);
610 if(packet->data[20] & 0x40) {
611 packet->len = MAX(via->mtu, 590);
612 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
614 fragment_ipv4_packet(via, packet, ether_size);
620 clamp_mss(source, via, packet);
622 send_packet(subnet->owner, packet);
625 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
626 if(!checklength(source, packet, ether_size + ip_size))
629 if(broadcast_mode && (((packet->data[30] & 0xf0) == 0xe0) || (
630 packet->data[30] == 255 &&
631 packet->data[31] == 255 &&
632 packet->data[32] == 255 &&
633 packet->data[33] == 255)))
634 route_broadcast(source, packet);
636 route_ipv4_unicast(source, packet);
639 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
644 memcpy(&dest, &packet->data[38], sizeof dest);
645 subnet = lookup_subnet_ipv6(&dest);
648 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
649 source->name, source->hostname,
659 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
663 if(subnet->owner == source) {
664 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
668 if(!subnet->owner->status.reachable)
669 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
671 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
672 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
674 if(decrement_ttl && source != myself && subnet->owner != myself)
675 if(!do_decrement_ttl(source, packet))
678 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
681 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
685 if(directonly && subnet->owner != via)
686 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
688 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
689 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);
690 packet->len = MAX(via->mtu, 1294);
691 route_ipv6_unreachable(source, packet, ether_size, ICMP6_PACKET_TOO_BIG, 0);
695 clamp_mss(source, via, packet);
697 send_packet(subnet->owner, packet);
702 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
704 struct nd_neighbor_solicit ns;
705 struct nd_opt_hdr opt;
711 struct in6_addr ip6_src; /* source address */
712 struct in6_addr ip6_dst; /* destination address */
717 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
720 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
722 if(source != myself) {
723 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
727 /* Copy headers from packet to structs on the stack */
729 memcpy(&ip6, packet->data + ether_size, ip6_size);
730 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
732 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
734 /* First, snatch the source address from the neighbor solicitation packet */
737 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
739 /* Check if this is a valid neighbor solicitation request */
741 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
742 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
743 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
747 /* Create pseudo header */
749 pseudo.ip6_src = ip6.ip6_src;
750 pseudo.ip6_dst = ip6.ip6_dst;
752 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
754 pseudo.length = htonl(ns_size);
755 pseudo.next = htonl(IPPROTO_ICMPV6);
757 /* Generate checksum */
759 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
760 checksum = inet_checksum(&ns, ns_size, checksum);
762 checksum = inet_checksum(&opt, opt_size, checksum);
763 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
767 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
771 /* Check if the IPv6 address exists on the VPN */
773 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
776 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
777 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
778 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
779 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
780 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
781 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
782 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
783 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
784 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
789 /* Check if it is for our own subnet */
791 if(subnet->owner == myself)
792 return; /* silently ignore */
795 if(!do_decrement_ttl(source, packet))
798 /* Create neighbor advertation reply */
800 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
801 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
803 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
804 ip6.ip6_src = ns.nd_ns_target;
807 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
810 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
811 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
812 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
814 /* Create pseudo header */
816 pseudo.ip6_src = ip6.ip6_src;
817 pseudo.ip6_dst = ip6.ip6_dst;
819 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
821 pseudo.length = htonl(ns_size);
822 pseudo.next = htonl(IPPROTO_ICMPV6);
824 /* Generate checksum */
826 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
827 checksum = inet_checksum(&ns, ns_size, checksum);
829 checksum = inet_checksum(&opt, opt_size, checksum);
830 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
833 ns.nd_ns_hdr.icmp6_cksum = checksum;
835 /* Copy structs on stack back to packet */
837 memcpy(packet->data + ether_size, &ip6, ip6_size);
838 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
840 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
842 send_packet(source, packet);
845 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
846 if(!checklength(source, packet, ether_size + ip6_size))
849 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
850 route_neighborsol(source, packet);
854 if(broadcast_mode && packet->data[38] == 255)
855 route_broadcast(source, packet);
857 route_ipv6_unicast(source, packet);
862 static void route_arp(node_t *source, vpn_packet_t *packet) {
863 struct ether_arp arp;
867 if(!checklength(source, packet, ether_size + arp_size))
870 if(source != myself) {
871 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
875 /* First, snatch the source address from the ARP packet */
878 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
880 /* Copy headers from packet to structs on the stack */
882 memcpy(&arp, packet->data + ether_size, arp_size);
884 /* Check if this is a valid ARP request */
886 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
887 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
888 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
892 /* Check if the IPv4 address exists on the VPN */
894 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
897 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
898 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
903 /* Check if it is for our own subnet */
905 if(subnet->owner == myself)
906 return; /* silently ignore */
909 if(!do_decrement_ttl(source, packet))
912 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
913 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
915 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
916 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
917 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
919 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
920 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
921 arp.arp_op = htons(ARPOP_REPLY);
923 /* Copy structs on stack back to packet */
925 memcpy(packet->data + ether_size, &arp, arp_size);
927 send_packet(source, packet);
930 static void route_mac(node_t *source, vpn_packet_t *packet) {
934 /* Learn source address */
936 if(source == myself) {
938 memcpy(&src, &packet->data[6], sizeof src);
942 /* Lookup destination address */
944 memcpy(&dest, &packet->data[0], sizeof dest);
945 subnet = lookup_subnet_mac(NULL, &dest);
948 route_broadcast(source, packet);
952 if(subnet->owner == source) {
953 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
957 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
960 if(decrement_ttl && source != myself && subnet->owner != myself)
961 if(!do_decrement_ttl(source, packet))
964 uint16_t type = packet->data[12] << 8 | packet->data[13];
966 if(priorityinheritance && type == ETH_P_IP && packet->len >= ether_size + ip_size)
967 packet->priority = packet->data[15];
969 // Handle packets larger than PMTU
971 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
973 if(directonly && subnet->owner != via)
976 if(via && packet->len > via->mtu && via != myself) {
977 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);
978 length_t ethlen = 14;
980 if(type == ETH_P_8021Q) {
981 type = packet->data[16] << 8 | packet->data[17];
985 if(type == ETH_P_IP && packet->len > 576 + ethlen) {
986 if(packet->data[6 + ethlen] & 0x40) {
987 packet->len = via->mtu;
988 route_ipv4_unreachable(source, packet, ethlen, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
990 fragment_ipv4_packet(via, packet, ethlen);
993 } else if(type == ETH_P_IPV6 && packet->len > 1280 + ethlen) {
994 packet->len = via->mtu;
995 route_ipv6_unreachable(source, packet, ethlen, ICMP6_PACKET_TOO_BIG, 0);
1000 clamp_mss(source, via, packet);
1002 send_packet(subnet->owner, packet);
1005 void route(node_t *source, vpn_packet_t *packet) {
1006 if(forwarding_mode == FMODE_KERNEL && source != myself) {
1007 send_packet(myself, packet);
1011 if(!checklength(source, packet, ether_size))
1014 switch (routing_mode) {
1017 uint16_t type = packet->data[12] << 8 | packet->data[13];
1021 route_arp(source, packet);
1025 route_ipv4(source, packet);
1029 route_ipv6(source, packet);
1033 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
1040 route_mac(source, packet);
1044 route_broadcast(source, packet);