3 Copyright (C) 2000-2005 Ivo Timmermans,
4 2000-2010 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 "splay_tree.h"
24 #include "connection.h"
25 #include "control_common.h"
37 rmode_t routing_mode = RMODE_ROUTER;
38 fmode_t forwarding_mode = FMODE_INTERNAL;
39 bool directonly = false;
40 bool priorityinheritance = false;
42 bool overwrite_mac = false;
43 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))
61 static struct event age_subnets_event;
65 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
67 uint32_t checksum = prevsum ^ 0xFFFF;
75 checksum += *(uint8_t *)p;
78 checksum = (checksum & 0xFFFF) + (checksum >> 16);
83 static bool ratelimit(int frequency) {
84 static time_t lasttime = 0;
86 time_t now = time(NULL);
89 if(++count > frequency)
99 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
100 if(packet->len < length) {
101 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
107 static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
108 if(!source || !via || !(via->options & OPTION_CLAMP_MSS))
111 uint16_t mtu = source->mtu;
112 if(via != myself && via->mtu < mtu)
115 /* Find TCP header */
117 uint16_t type = packet->data[12] << 8 | packet->data[13];
119 if(type == ETH_P_IP && packet->data[23] == 6)
120 start = 14 + (packet->data[14] & 0xf) * 4;
121 else if(type == ETH_P_IPV6 && packet->data[20] == 6)
124 if(!start || packet->len <= start + 20)
127 /* Use data offset field to calculate length of options field */
128 int len = ((packet->data[start + 12] >> 4) - 5) * 4;
130 if(packet->len < start + 20 + len)
133 /* Search for MSS option header */
134 for(int i = 0; i < len;) {
135 if(packet->data[start + 20 + i] == 0)
138 if(packet->data[start + 20 + i] == 1) {
143 if(i > len - 2 || i > len - packet->data[start + 21 + i])
146 if(packet->data[start + 20 + i] != 2) {
147 if(packet->data[start + 21 + i] < 2)
149 i += packet->data[start + 21 + i];
153 if(packet->data[start + 21] != 4)
157 uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
158 uint16_t newmss = mtu - start - 20;
159 uint16_t csum = packet->data[start + 16] << 8 | packet->data[start + 17];
164 ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
166 /* Update the MSS value and the checksum */
167 packet->data[start + 22 + i] = newmss >> 8;
168 packet->data[start + 23 + i] = newmss & 0xff;
173 packet->data[start + 16] = csum >> 8;
174 packet->data[start + 17] = csum & 0xff;
179 static void swap_mac_addresses(vpn_packet_t *packet) {
181 memcpy(&tmp, &packet->data[0], sizeof tmp);
182 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
183 memcpy(&packet->data[6], &tmp, sizeof tmp);
186 static void age_subnets(int fd, short events, void *data) {
189 splay_node_t *node, *next, *node2;
191 time_t now = time(NULL);
193 for(node = myself->subnet_tree->head; node; node = next) {
196 if(s->expires && s->expires < now) {
198 char netstr[MAXNETSTR];
199 if(net2str(netstr, sizeof netstr, s))
200 logger(LOG_INFO, "Subnet %s expired", netstr);
203 for(node2 = connection_tree->head; node2; node2 = node2->next) {
206 send_del_subnet(c, s);
209 subnet_del(myself, s);
217 event_add(&age_subnets_event, &(struct timeval){10, 0});
220 static void learn_mac(mac_t *address) {
225 subnet = lookup_subnet_mac(myself, address);
227 /* If we don't know this MAC address yet, store it */
230 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
231 address->x[0], address->x[1], address->x[2], address->x[3],
232 address->x[4], address->x[5]);
234 subnet = new_subnet();
235 subnet->type = SUBNET_MAC;
236 subnet->expires = time(NULL) + macexpire;
237 subnet->net.mac.address = *address;
239 subnet_add(myself, subnet);
240 subnet_update(myself, subnet, true);
242 /* And tell all other tinc daemons it's our MAC */
244 for(node = connection_tree->head; node; node = node->next) {
247 send_add_subnet(c, subnet);
250 if(!timeout_initialized(&age_subnets_event))
251 timeout_set(&age_subnets_event, age_subnets, NULL);
252 event_add(&age_subnets_event, &(struct timeval){10, 0});
255 subnet->expires = time(NULL) + macexpire;
261 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
263 struct icmp icmp = {0};
265 struct in_addr ip_src;
266 struct in_addr ip_dst;
272 /* Swap Ethernet source and destination addresses */
274 swap_mac_addresses(packet);
276 /* Copy headers from packet into properly aligned structs on the stack */
278 memcpy(&ip, packet->data + ether_size, ip_size);
280 /* Remember original source and destination */
285 oldlen = packet->len - ether_size;
287 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
288 icmp.icmp_nextmtu = htons(packet->len - ether_size);
290 if(oldlen >= IP_MSS - ip_size - icmp_size)
291 oldlen = IP_MSS - ip_size - icmp_size;
293 /* Copy first part of original contents to ICMP message */
295 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
297 /* Fill in IPv4 header */
300 ip.ip_hl = ip_size / 4;
302 ip.ip_len = htons(ip_size + icmp_size + oldlen);
306 ip.ip_p = IPPROTO_ICMP;
311 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
313 /* Fill in ICMP header */
315 icmp.icmp_type = type;
316 icmp.icmp_code = code;
319 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
320 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
322 /* Copy structs on stack back to packet */
324 memcpy(packet->data + ether_size, &ip, ip_size);
325 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
327 packet->len = ether_size + ip_size + icmp_size + oldlen;
329 send_packet(source, packet);
334 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
336 vpn_packet_t fragment;
337 int len, maxlen, todo;
339 uint16_t ip_off, origf;
341 memcpy(&ip, packet->data + ether_size, ip_size);
342 fragment.priority = packet->priority;
344 if(ip.ip_hl != ip_size / 4)
347 todo = ntohs(ip.ip_len) - ip_size;
349 if(ether_size + ip_size + todo != packet->len) {
350 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));
354 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
356 offset = packet->data + ether_size + ip_size;
357 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
358 ip_off = ntohs(ip.ip_off);
359 origf = ip_off & ~IP_OFFMASK;
360 ip_off &= IP_OFFMASK;
363 len = todo > maxlen ? maxlen : todo;
364 memcpy(fragment.data + ether_size + ip_size, offset, len);
368 ip.ip_len = htons(ip_size + len);
369 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
371 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
372 memcpy(fragment.data, packet->data, ether_size);
373 memcpy(fragment.data + ether_size, &ip, ip_size);
374 fragment.len = ether_size + ip_size + len;
376 send_packet(dest, &fragment);
382 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
387 memcpy(&dest, &packet->data[30], sizeof dest);
388 subnet = lookup_subnet_ipv4(&dest);
391 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
392 source->name, source->hostname,
398 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
402 if(subnet->owner == source) {
403 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
407 if(!subnet->owner->status.reachable)
408 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
410 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
411 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
413 if(priorityinheritance)
414 packet->priority = packet->data[15];
416 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
418 if(directonly && subnet->owner != via)
419 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
421 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
422 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);
423 if(packet->data[20] & 0x40) {
424 packet->len = MAX(via->mtu, 590);
425 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
427 fragment_ipv4_packet(via, packet);
433 clamp_mss(source, via, packet);
435 send_packet(subnet->owner, packet);
438 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
439 if(!checklength(source, packet, ether_size + ip_size))
442 if(((packet->data[30] & 0xf0) == 0xe0) || (
443 packet->data[30] == 255 &&
444 packet->data[31] == 255 &&
445 packet->data[32] == 255 &&
446 packet->data[33] == 255))
447 broadcast_packet(source, packet);
449 route_ipv4_unicast(source, packet);
454 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
456 struct icmp6_hdr icmp6 = {0};
460 struct in6_addr ip6_src; /* source address */
461 struct in6_addr ip6_dst; /* destination address */
469 /* Swap Ethernet source and destination addresses */
471 swap_mac_addresses(packet);
473 /* Copy headers from packet to structs on the stack */
475 memcpy(&ip6, packet->data + ether_size, ip6_size);
477 /* Remember original source and destination */
479 pseudo.ip6_src = ip6.ip6_dst;
480 pseudo.ip6_dst = ip6.ip6_src;
482 pseudo.length = packet->len - ether_size;
484 if(type == ICMP6_PACKET_TOO_BIG)
485 icmp6.icmp6_mtu = htonl(pseudo.length);
487 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
488 pseudo.length = IP_MSS - ip6_size - icmp6_size;
490 /* Copy first part of original contents to ICMP message */
492 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
494 /* Fill in IPv6 header */
496 ip6.ip6_flow = htonl(0x60000000UL);
497 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
498 ip6.ip6_nxt = IPPROTO_ICMPV6;
500 ip6.ip6_src = pseudo.ip6_src;
501 ip6.ip6_dst = pseudo.ip6_dst;
503 /* Fill in ICMP header */
505 icmp6.icmp6_type = type;
506 icmp6.icmp6_code = code;
507 icmp6.icmp6_cksum = 0;
509 /* Create pseudo header */
511 pseudo.length = htonl(icmp6_size + pseudo.length);
512 pseudo.next = htonl(IPPROTO_ICMPV6);
514 /* Generate checksum */
516 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
517 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
518 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
520 icmp6.icmp6_cksum = checksum;
522 /* Copy structs on stack back to packet */
524 memcpy(packet->data + ether_size, &ip6, ip6_size);
525 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
527 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
529 send_packet(source, packet);
532 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
537 memcpy(&dest, &packet->data[38], sizeof dest);
538 subnet = lookup_subnet_ipv6(&dest);
541 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
542 source->name, source->hostname,
552 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
556 if(subnet->owner == source) {
557 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
561 if(!subnet->owner->status.reachable)
562 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
564 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
565 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
567 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
569 if(directonly && subnet->owner != via)
570 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
572 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
573 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);
574 packet->len = MAX(via->mtu, 1294);
575 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
579 clamp_mss(source, via, packet);
581 send_packet(subnet->owner, packet);
586 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
588 struct nd_neighbor_solicit ns;
589 struct nd_opt_hdr opt;
595 struct in6_addr ip6_src; /* source address */
596 struct in6_addr ip6_dst; /* destination address */
601 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
604 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
606 if(source != myself) {
607 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
611 /* Copy headers from packet to structs on the stack */
613 memcpy(&ip6, packet->data + ether_size, ip6_size);
614 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
616 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
618 /* First, snatch the source address from the neighbor solicitation packet */
621 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
623 /* Check if this is a valid neighbor solicitation request */
625 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
626 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
627 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
631 /* Create pseudo header */
633 pseudo.ip6_src = ip6.ip6_src;
634 pseudo.ip6_dst = ip6.ip6_dst;
636 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
638 pseudo.length = htonl(ns_size);
639 pseudo.next = htonl(IPPROTO_ICMPV6);
641 /* Generate checksum */
643 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
644 checksum = inet_checksum(&ns, ns_size, checksum);
646 checksum = inet_checksum(&opt, opt_size, checksum);
647 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
651 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
655 /* Check if the IPv6 address exists on the VPN */
657 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
660 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
661 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
662 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
663 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
664 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
665 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
666 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
667 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
668 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
673 /* Check if it is for our own subnet */
675 if(subnet->owner == myself)
676 return; /* silently ignore */
678 /* Create neighbor advertation reply */
680 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
681 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
683 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
684 ip6.ip6_src = ns.nd_ns_target;
687 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
690 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
691 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
692 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
694 /* Create pseudo header */
696 pseudo.ip6_src = ip6.ip6_src;
697 pseudo.ip6_dst = ip6.ip6_dst;
699 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
701 pseudo.length = htonl(ns_size);
702 pseudo.next = htonl(IPPROTO_ICMPV6);
704 /* Generate checksum */
706 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
707 checksum = inet_checksum(&ns, ns_size, checksum);
709 checksum = inet_checksum(&opt, opt_size, checksum);
710 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
713 ns.nd_ns_hdr.icmp6_cksum = checksum;
715 /* Copy structs on stack back to packet */
717 memcpy(packet->data + ether_size, &ip6, ip6_size);
718 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
720 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
722 send_packet(source, packet);
725 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
726 if(!checklength(source, packet, ether_size + ip6_size))
729 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
730 route_neighborsol(source, packet);
734 if(packet->data[38] == 255)
735 broadcast_packet(source, packet);
737 route_ipv6_unicast(source, packet);
742 static void route_arp(node_t *source, vpn_packet_t *packet) {
743 struct ether_arp arp;
747 if(!checklength(source, packet, ether_size + arp_size))
750 if(source != myself) {
751 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
755 /* First, snatch the source address from the ARP packet */
758 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
760 /* Copy headers from packet to structs on the stack */
762 memcpy(&arp, packet->data + ether_size, arp_size);
764 /* Check if this is a valid ARP request */
766 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
767 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof addr || ntohs(arp.arp_op) != ARPOP_REQUEST) {
768 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
772 /* Check if the IPv4 address exists on the VPN */
774 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
777 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
778 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
783 /* Check if it is for our own subnet */
785 if(subnet->owner == myself)
786 return; /* silently ignore */
788 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
789 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
791 memcpy(&addr, arp.arp_tpa, sizeof addr); /* save protocol addr */
792 memcpy(arp.arp_tpa, arp.arp_spa, sizeof addr); /* swap destination and source protocol address */
793 memcpy(arp.arp_spa, &addr, sizeof addr); /* ... */
795 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
796 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
797 arp.arp_op = htons(ARPOP_REPLY);
799 /* Copy structs on stack back to packet */
801 memcpy(packet->data + ether_size, &arp, arp_size);
803 send_packet(source, packet);
806 static void route_mac(node_t *source, vpn_packet_t *packet) {
810 /* Learn source address */
812 if(source == myself) {
814 memcpy(&src, &packet->data[6], sizeof src);
818 /* Lookup destination address */
820 memcpy(&dest, &packet->data[0], sizeof dest);
821 subnet = lookup_subnet_mac(NULL, &dest);
824 broadcast_packet(source, packet);
828 if(subnet->owner == source) {
829 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
833 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
836 // Handle packets larger than PMTU
838 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
840 if(directonly && subnet->owner != via)
843 if(via && packet->len > via->mtu && via != myself) {
844 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);
845 uint16_t type = packet->data[12] << 8 | packet->data[13];
846 if(type == ETH_P_IP && packet->len > 590) {
847 if(packet->data[20] & 0x40) {
848 packet->len = via->mtu;
849 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
851 fragment_ipv4_packet(via, packet);
854 } else if(type == ETH_P_IPV6 && packet->len > 1294) {
855 packet->len = via->mtu;
856 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
861 clamp_mss(source, via, packet);
863 send_packet(subnet->owner, packet);
866 static void send_pcap(vpn_packet_t *packet) {
868 for(splay_node_t *node = connection_tree->head; node; node = node->next) {
869 connection_t *c = node->data;
874 if(send_request(c, "%d %d %d", CONTROL, REQ_PCAP, packet->len))
875 send_meta(c, (char *)packet->data, packet->len);
879 void route(node_t *source, vpn_packet_t *packet) {
883 if(forwarding_mode == FMODE_KERNEL && source != myself) {
884 send_packet(myself, packet);
888 if(!checklength(source, packet, ether_size))
891 switch (routing_mode) {
894 uint16_t type = packet->data[12] << 8 | packet->data[13];
898 route_arp(source, packet);
902 route_ipv4(source, packet);
906 route_ipv6(source, packet);
910 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
917 route_mac(source, packet);
921 broadcast_packet(source, packet);