3 Copyright (C) 2000-2005 Ivo Timmermans,
4 2000-2014 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "connection.h"
35 rmode_t routing_mode = RMODE_ROUTER;
36 fmode_t forwarding_mode = FMODE_INTERNAL;
37 bmode_t broadcast_mode = BMODE_MST;
38 bool decrement_ttl = false;
39 bool directonly = false;
40 bool priorityinheritance = false;
42 bool overwrite_mac = false;
43 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
45 /* Sizes of various headers */
47 static const size_t ether_size = sizeof(struct ether_header);
48 static const size_t arp_size = sizeof(struct ether_arp);
49 static const size_t ip_size = sizeof(struct ip);
50 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
51 static const size_t ip6_size = sizeof(struct ip6_hdr);
52 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
53 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
54 static const size_t opt_size = sizeof(struct nd_opt_hdr);
57 #define MAX(a, b) ((a) > (b) ? (a) : (b))
62 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
64 uint32_t checksum = prevsum ^ 0xFFFF;
72 checksum += *(uint8_t *)p;
75 checksum = (checksum & 0xFFFF) + (checksum >> 16);
80 static bool ratelimit(int frequency) {
81 static time_t lasttime = 0;
85 if(count >= frequency)
96 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
97 if(packet->len < length) {
98 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
104 static void swap_mac_addresses(vpn_packet_t *packet) {
106 memcpy(&tmp, &packet->data[0], sizeof tmp);
107 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
108 memcpy(&packet->data[6], &tmp, sizeof tmp);
113 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
115 struct icmp icmp = {0};
117 struct in_addr ip_src;
118 struct in_addr ip_dst;
124 /* Swap Ethernet source and destination addresses */
126 swap_mac_addresses(packet);
128 /* Copy headers from packet into properly aligned structs on the stack */
130 memcpy(&ip, packet->data + ether_size, ip_size);
132 /* Remember original source and destination */
137 /* Try to reply with an IP address assigned to the local machine */
139 if (type == ICMP_TIME_EXCEEDED && code == ICMP_EXC_TTL) {
140 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
142 struct sockaddr_in addr;
143 memset(&addr, 0, sizeof(addr));
144 addr.sin_family = AF_INET;
145 addr.sin_addr = ip.ip_src;
146 if (!connect(sockfd, (const struct sockaddr*) &addr, sizeof(addr))) {
147 memset(&addr, 0, sizeof(addr));
148 addr.sin_family = AF_INET;
149 socklen_t addrlen = sizeof(addr);
150 if (!getsockname(sockfd, (struct sockaddr*) &addr, &addrlen) && addrlen <= sizeof(addr)) {
151 ip_dst = addr.sin_addr;
158 oldlen = packet->len - ether_size;
160 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
161 icmp.icmp_nextmtu = htons(packet->len - ether_size);
163 if(oldlen >= IP_MSS - ip_size - icmp_size)
164 oldlen = IP_MSS - ip_size - icmp_size;
166 /* Copy first part of original contents to ICMP message */
168 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
170 /* Fill in IPv4 header */
173 ip.ip_hl = ip_size / 4;
175 ip.ip_len = htons(ip_size + icmp_size + oldlen);
179 ip.ip_p = IPPROTO_ICMP;
184 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
186 /* Fill in ICMP header */
188 icmp.icmp_type = type;
189 icmp.icmp_code = code;
192 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
193 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
195 /* Copy structs on stack back to packet */
197 memcpy(packet->data + ether_size, &ip, ip_size);
198 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
200 packet->len = ether_size + ip_size + icmp_size + oldlen;
202 send_packet(source, packet);
207 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
209 struct icmp6_hdr icmp6 = {0};
213 struct in6_addr ip6_src; /* source address */
214 struct in6_addr ip6_dst; /* destination address */
222 /* Swap Ethernet source and destination addresses */
224 swap_mac_addresses(packet);
226 /* Copy headers from packet to structs on the stack */
228 memcpy(&ip6, packet->data + ether_size, ip6_size);
230 /* Remember original source and destination */
232 pseudo.ip6_src = ip6.ip6_dst;
233 pseudo.ip6_dst = ip6.ip6_src;
235 /* Try to reply with an IP address assigned to the local machine */
237 if (type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT) {
238 int sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
240 struct sockaddr_in6 addr;
241 memset(&addr, 0, sizeof(addr));
242 addr.sin6_family = AF_INET6;
243 addr.sin6_addr = ip6.ip6_src;
244 if (!connect(sockfd, (const struct sockaddr*) &addr, sizeof(addr))) {
245 memset(&addr, 0, sizeof(addr));
246 addr.sin6_family = AF_INET6;
247 socklen_t addrlen = sizeof(addr);
248 if (!getsockname(sockfd, (struct sockaddr*) &addr, &addrlen) && addrlen <= sizeof(addr)) {
249 pseudo.ip6_src = addr.sin6_addr;
256 pseudo.length = packet->len - ether_size;
258 if(type == ICMP6_PACKET_TOO_BIG)
259 icmp6.icmp6_mtu = htonl(pseudo.length);
261 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
262 pseudo.length = IP_MSS - ip6_size - icmp6_size;
264 /* Copy first part of original contents to ICMP message */
266 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
268 /* Fill in IPv6 header */
270 ip6.ip6_flow = htonl(0x60000000UL);
271 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
272 ip6.ip6_nxt = IPPROTO_ICMPV6;
274 ip6.ip6_src = pseudo.ip6_src;
275 ip6.ip6_dst = pseudo.ip6_dst;
277 /* Fill in ICMP header */
279 icmp6.icmp6_type = type;
280 icmp6.icmp6_code = code;
281 icmp6.icmp6_cksum = 0;
283 /* Create pseudo header */
285 pseudo.length = htonl(icmp6_size + pseudo.length);
286 pseudo.next = htonl(IPPROTO_ICMPV6);
288 /* Generate checksum */
290 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
291 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
292 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
294 icmp6.icmp6_cksum = checksum;
296 /* Copy structs on stack back to packet */
298 memcpy(packet->data + ether_size, &ip6, ip6_size);
299 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
301 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
303 send_packet(source, packet);
306 static bool do_decrement_ttl(node_t *source, vpn_packet_t *packet) {
307 uint16_t type = packet->data[12] << 8 | packet->data[13];
308 length_t ethlen = ether_size;
310 if(type == ETH_P_8021Q) {
311 type = packet->data[16] << 8 | packet->data[17];
317 if(!checklength(source, packet, ethlen + ip_size))
320 if(packet->data[ethlen + 8] <= 1) {
321 if(packet->data[ethlen + 11] != IPPROTO_ICMP || packet->data[ethlen + 32] != ICMP_TIME_EXCEEDED)
322 route_ipv4_unreachable(source, packet, ethlen, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL);
326 uint16_t old = packet->data[ethlen + 8] << 8 | packet->data[ethlen + 9];
327 packet->data[ethlen + 8]--;
328 uint16_t new = packet->data[ethlen + 8] << 8 | packet->data[ethlen + 9];
330 uint32_t checksum = packet->data[ethlen + 10] << 8 | packet->data[ethlen + 11];
331 checksum += old + (~new & 0xFFFF);
332 while(checksum >> 16)
333 checksum = (checksum & 0xFFFF) + (checksum >> 16);
334 packet->data[ethlen + 10] = checksum >> 8;
335 packet->data[ethlen + 11] = checksum & 0xff;
340 if(!checklength(source, packet, ethlen + ip6_size))
343 if(packet->data[ethlen + 7] <= 1) {
344 if(packet->data[ethlen + 6] != IPPROTO_ICMPV6 || packet->data[ethlen + 40] != ICMP6_TIME_EXCEEDED)
345 route_ipv6_unreachable(source, packet, ethlen, ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT);
349 packet->data[ethlen + 7]--;
358 static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
359 if(!source || !via || !(via->options & OPTION_CLAMP_MSS))
362 uint16_t mtu = source->mtu;
363 if(via != myself && via->mtu < mtu)
366 /* Find TCP header */
367 int start = ether_size;
368 uint16_t type = packet->data[12] << 8 | packet->data[13];
370 if(type == ETH_P_8021Q) {
372 type = packet->data[16] << 8 | packet->data[17];
375 if(type == ETH_P_IP && packet->data[start + 9] == 6)
376 start += (packet->data[start] & 0xf) * 4;
377 else if(type == ETH_P_IPV6 && packet->data[start + 6] == 6)
382 if(packet->len <= start + 20)
385 /* Use data offset field to calculate length of options field */
386 int len = ((packet->data[start + 12] >> 4) - 5) * 4;
388 if(packet->len < start + 20 + len)
391 /* Search for MSS option header */
392 for(int i = 0; i < len;) {
393 if(packet->data[start + 20 + i] == 0)
396 if(packet->data[start + 20 + i] == 1) {
401 if(i > len - 2 || i > len - packet->data[start + 21 + i])
404 if(packet->data[start + 20 + i] != 2) {
405 if(packet->data[start + 21 + i] < 2)
407 i += packet->data[start + 21 + i];
411 if(packet->data[start + 21] != 4)
415 uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
416 uint16_t newmss = mtu - start - 20;
417 uint32_t csum = packet->data[start + 16] << 8 | packet->data[start + 17];
422 ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
424 /* Update the MSS value and the checksum */
425 packet->data[start + 22 + i] = newmss >> 8;
426 packet->data[start + 23 + i] = newmss & 0xff;
428 csum += oldmss ^ 0xffff;
430 csum = (csum & 0xffff) + (csum >> 16);
433 packet->data[start + 16] = csum >> 8;
434 packet->data[start + 17] = csum;
439 static void learn_mac(mac_t *address) {
444 subnet = lookup_subnet_mac(myself, address);
446 /* If we don't know this MAC address yet, store it */
449 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %x:%x:%x:%x:%x:%x",
450 address->x[0], address->x[1], address->x[2], address->x[3],
451 address->x[4], address->x[5]);
453 subnet = new_subnet();
454 subnet->type = SUBNET_MAC;
455 subnet->expires = now + macexpire;
456 subnet->net.mac.address = *address;
458 subnet_add(myself, subnet);
459 subnet_update(myself, subnet, true);
461 /* And tell all other tinc daemons it's our MAC */
463 for(node = connection_tree->head; node; node = node->next) {
466 send_add_subnet(c, subnet);
471 subnet->expires = now + macexpire;
474 void age_subnets(void) {
477 avl_node_t *node, *next, *node2;
479 for(node = myself->subnet_tree->head; node; node = next) {
482 if(s->expires && s->expires <= now) {
484 char netstr[MAXNETSTR];
485 if(net2str(netstr, sizeof netstr, s))
486 logger(LOG_INFO, "Subnet %s expired", netstr);
489 for(node2 = connection_tree->head; node2; node2 = node2->next) {
492 send_del_subnet(c, s);
495 subnet_update(myself, s, false);
496 subnet_del(myself, s);
501 static void route_broadcast(node_t *source, vpn_packet_t *packet) {
502 if(decrement_ttl && source != myself)
503 if(!do_decrement_ttl(source, packet))
506 broadcast_packet(source, packet);
511 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t ether_size) {
513 vpn_packet_t fragment;
514 int len, maxlen, todo;
516 uint16_t ip_off, origf;
518 memcpy(&ip, packet->data + ether_size, ip_size);
519 fragment.priority = packet->priority;
521 if(ip.ip_hl != ip_size / 4)
524 todo = ntohs(ip.ip_len) - ip_size;
526 if(ether_size + ip_size + todo != packet->len) {
527 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));
531 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
533 offset = packet->data + ether_size + ip_size;
534 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
535 ip_off = ntohs(ip.ip_off);
536 origf = ip_off & ~IP_OFFMASK;
537 ip_off &= IP_OFFMASK;
540 len = todo > maxlen ? maxlen : todo;
541 memcpy(fragment.data + ether_size + ip_size, offset, len);
545 ip.ip_len = htons(ip_size + len);
546 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
548 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
549 memcpy(fragment.data, packet->data, ether_size);
550 memcpy(fragment.data + ether_size, &ip, ip_size);
551 fragment.len = ether_size + ip_size + len;
553 send_packet(dest, &fragment);
559 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
564 memcpy(&dest, &packet->data[30], sizeof dest);
565 subnet = lookup_subnet_ipv4(&dest);
568 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
569 source->name, source->hostname,
575 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
579 if(subnet->owner == source) {
580 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
584 if(!subnet->owner->status.reachable)
585 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
587 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
588 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
590 if(decrement_ttl && source != myself && subnet->owner != myself)
591 if(!do_decrement_ttl(source, packet))
594 if(priorityinheritance)
595 packet->priority = packet->data[15];
597 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
600 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
604 if(directonly && subnet->owner != via)
605 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
607 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
608 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);
609 if(packet->data[20] & 0x40) {
610 packet->len = MAX(via->mtu, 590);
611 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
613 fragment_ipv4_packet(via, packet, ether_size);
619 clamp_mss(source, via, packet);
621 send_packet(subnet->owner, packet);
624 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
625 if(!checklength(source, packet, ether_size + ip_size))
628 if(broadcast_mode && (((packet->data[30] & 0xf0) == 0xe0) || (
629 packet->data[30] == 255 &&
630 packet->data[31] == 255 &&
631 packet->data[32] == 255 &&
632 packet->data[33] == 255)))
633 route_broadcast(source, packet);
635 route_ipv4_unicast(source, packet);
638 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
643 memcpy(&dest, &packet->data[38], sizeof dest);
644 subnet = lookup_subnet_ipv6(&dest);
647 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
648 source->name, source->hostname,
658 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
662 if(subnet->owner == source) {
663 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
667 if(!subnet->owner->status.reachable)
668 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
670 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
671 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
673 if(decrement_ttl && source != myself && subnet->owner != myself)
674 if(!do_decrement_ttl(source, packet))
677 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
680 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
684 if(directonly && subnet->owner != via)
685 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
687 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
688 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);
689 packet->len = MAX(via->mtu, 1294);
690 route_ipv6_unreachable(source, packet, ether_size, ICMP6_PACKET_TOO_BIG, 0);
694 clamp_mss(source, via, packet);
696 send_packet(subnet->owner, packet);
701 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
703 struct nd_neighbor_solicit ns;
704 struct nd_opt_hdr opt;
710 struct in6_addr ip6_src; /* source address */
711 struct in6_addr ip6_dst; /* destination address */
716 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
719 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
721 if(source != myself) {
722 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
726 /* Copy headers from packet to structs on the stack */
728 memcpy(&ip6, packet->data + ether_size, ip6_size);
729 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
731 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
733 /* First, snatch the source address from the neighbor solicitation packet */
736 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
738 /* Check if this is a valid neighbor solicitation request */
740 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
741 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
742 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
746 /* Create pseudo header */
748 pseudo.ip6_src = ip6.ip6_src;
749 pseudo.ip6_dst = ip6.ip6_dst;
751 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
753 pseudo.length = htonl(ns_size);
754 pseudo.next = htonl(IPPROTO_ICMPV6);
756 /* Generate checksum */
758 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
759 checksum = inet_checksum(&ns, ns_size, checksum);
761 checksum = inet_checksum(&opt, opt_size, checksum);
762 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
766 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
770 /* Check if the IPv6 address exists on the VPN */
772 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
775 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
776 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
777 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
778 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
779 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
780 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
781 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
782 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
783 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
788 /* Check if it is for our own subnet */
790 if(subnet->owner == myself)
791 return; /* silently ignore */
794 if(!do_decrement_ttl(source, packet))
797 /* Create neighbor advertation reply */
799 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
800 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
802 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
803 ip6.ip6_src = ns.nd_ns_target;
806 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
809 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
810 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
811 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
813 /* Create pseudo header */
815 pseudo.ip6_src = ip6.ip6_src;
816 pseudo.ip6_dst = ip6.ip6_dst;
818 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
820 pseudo.length = htonl(ns_size);
821 pseudo.next = htonl(IPPROTO_ICMPV6);
823 /* Generate checksum */
825 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
826 checksum = inet_checksum(&ns, ns_size, checksum);
828 checksum = inet_checksum(&opt, opt_size, checksum);
829 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
832 ns.nd_ns_hdr.icmp6_cksum = checksum;
834 /* Copy structs on stack back to packet */
836 memcpy(packet->data + ether_size, &ip6, ip6_size);
837 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
839 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
841 send_packet(source, packet);
844 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
845 if(!checklength(source, packet, ether_size + ip6_size))
848 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
849 route_neighborsol(source, packet);
853 if(broadcast_mode && packet->data[38] == 255)
854 route_broadcast(source, packet);
856 route_ipv6_unicast(source, packet);
861 static void route_arp(node_t *source, vpn_packet_t *packet) {
862 struct ether_arp arp;
866 if(!checklength(source, packet, ether_size + arp_size))
869 if(source != myself) {
870 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
874 /* First, snatch the source address from the ARP packet */
877 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
879 /* Copy headers from packet to structs on the stack */
881 memcpy(&arp, packet->data + ether_size, arp_size);
883 /* Check if this is a valid ARP request */
885 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
886 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
887 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
891 /* Check if the IPv4 address exists on the VPN */
893 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
896 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
897 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
902 /* Check if it is for our own subnet */
904 if(subnet->owner == myself)
905 return; /* silently ignore */
908 if(!do_decrement_ttl(source, packet))
911 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
912 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
914 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
915 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
916 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
918 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
919 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
920 arp.arp_op = htons(ARPOP_REPLY);
922 /* Copy structs on stack back to packet */
924 memcpy(packet->data + ether_size, &arp, arp_size);
926 send_packet(source, packet);
929 static void route_mac(node_t *source, vpn_packet_t *packet) {
933 /* Learn source address */
935 if(source == myself) {
937 memcpy(&src, &packet->data[6], sizeof src);
941 /* Lookup destination address */
943 memcpy(&dest, &packet->data[0], sizeof dest);
944 subnet = lookup_subnet_mac(NULL, &dest);
947 route_broadcast(source, packet);
951 if(subnet->owner == source) {
952 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
956 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
959 if(decrement_ttl && source != myself && subnet->owner != myself)
960 if(!do_decrement_ttl(source, packet))
963 uint16_t type = packet->data[12] << 8 | packet->data[13];
965 if(priorityinheritance && type == ETH_P_IP && packet->len >= ether_size + ip_size)
966 packet->priority = packet->data[15];
968 // Handle packets larger than PMTU
970 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
972 if(directonly && subnet->owner != via)
975 if(via && packet->len > via->mtu && via != myself) {
976 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);
977 length_t ethlen = 14;
979 if(type == ETH_P_8021Q) {
980 type = packet->data[16] << 8 | packet->data[17];
984 if(type == ETH_P_IP && packet->len > 576 + ethlen) {
985 if(packet->data[6 + ethlen] & 0x40) {
986 packet->len = via->mtu;
987 route_ipv4_unreachable(source, packet, ethlen, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
989 fragment_ipv4_packet(via, packet, ethlen);
992 } else if(type == ETH_P_IPV6 && packet->len > 1280 + ethlen) {
993 packet->len = via->mtu;
994 route_ipv6_unreachable(source, packet, ethlen, ICMP6_PACKET_TOO_BIG, 0);
999 clamp_mss(source, via, packet);
1001 send_packet(subnet->owner, packet);
1004 void route(node_t *source, vpn_packet_t *packet) {
1005 if(forwarding_mode == FMODE_KERNEL && source != myself) {
1006 send_packet(myself, packet);
1010 if(!checklength(source, packet, ether_size))
1013 switch (routing_mode) {
1016 uint16_t type = packet->data[12] << 8 | packet->data[13];
1020 route_arp(source, packet);
1024 route_ipv4(source, packet);
1028 route_ipv6(source, packet);
1032 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
1039 route_mac(source, packet);
1043 route_broadcast(source, packet);