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"
35 rmode_t routing_mode = RMODE_ROUTER;
36 fmode_t forwarding_mode = FMODE_INTERNAL;
37 bool directonly = false;
38 bool priorityinheritance = false;
40 bool overwrite_mac = false;
41 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
43 /* Sizes of various headers */
45 static const size_t ether_size = sizeof(struct ether_header);
46 static const size_t arp_size = sizeof(struct ether_arp);
47 static const size_t ip_size = sizeof(struct ip);
48 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
49 static const size_t ip6_size = sizeof(struct ip6_hdr);
50 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
51 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
52 static const size_t opt_size = sizeof(struct nd_opt_hdr);
55 #define MAX(a, b) ((a) > (b) ? (a) : (b))
58 static struct event age_subnets_event;
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;
83 time_t now = time(NULL);
86 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 clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
105 if(!source || !via || !(via->options & OPTION_CLAMP_MSS))
108 uint16_t mtu = source->mtu;
109 if(via != myself && via->mtu < mtu)
112 /* Find TCP header */
114 uint16_t type = packet->data[12] << 8 | packet->data[13];
116 if(type == ETH_P_IP && packet->data[23] == 6)
117 start = 14 + (packet->data[14] & 0xf) * 4;
118 else if(type == ETH_P_IPV6 && packet->data[20] == 6)
121 if(!start || packet->len <= start + 20)
124 /* Use data offset field to calculate length of options field */
125 int len = ((packet->data[start + 12] >> 4) - 5) * 4;
127 if(packet->len < start + 20 + len)
130 /* Search for MSS option header */
131 for(int i = 0; i < len;) {
132 if(packet->data[start + 20 + i] == 0)
135 if(packet->data[start + 20 + i] == 1) {
140 if(i > len - 2 || i > len - packet->data[start + 21 + i])
143 if(packet->data[start + 20 + i] != 2) {
144 if(packet->data[start + 21 + i] < 2)
146 i += packet->data[start + 21 + i];
150 if(packet->data[start + 21] != 4)
154 uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
155 uint16_t newmss = mtu - start - 20;
156 uint16_t csum = packet->data[start + 16] << 8 | packet->data[start + 17];
161 ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
163 /* Update the MSS value and the checksum */
164 packet->data[start + 22 + i] = newmss >> 8;
165 packet->data[start + 23 + i] = newmss & 0xff;
170 packet->data[start + 16] = csum >> 8;
171 packet->data[start + 17] = csum & 0xff;
176 static void swap_mac_addresses(vpn_packet_t *packet) {
178 memcpy(&tmp, &packet->data[0], sizeof tmp);
179 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
180 memcpy(&packet->data[6], &tmp, sizeof tmp);
183 static void age_subnets(int fd, short events, void *data) {
186 splay_node_t *node, *next, *node2;
188 time_t now = time(NULL);
190 for(node = myself->subnet_tree->head; node; node = next) {
193 if(s->expires && s->expires < now) {
195 char netstr[MAXNETSTR];
196 if(net2str(netstr, sizeof netstr, s))
197 logger(LOG_INFO, "Subnet %s expired", netstr);
200 for(node2 = connection_tree->head; node2; node2 = node2->next) {
203 send_del_subnet(c, s);
206 subnet_del(myself, s);
214 event_add(&age_subnets_event, &(struct timeval){10, 0});
217 static void learn_mac(mac_t *address) {
222 subnet = lookup_subnet_mac(myself, address);
224 /* If we don't know this MAC address yet, store it */
227 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
228 address->x[0], address->x[1], address->x[2], address->x[3],
229 address->x[4], address->x[5]);
231 subnet = new_subnet();
232 subnet->type = SUBNET_MAC;
233 subnet->expires = time(NULL) + macexpire;
234 subnet->net.mac.address = *address;
236 subnet_add(myself, subnet);
237 subnet_update(myself, subnet, true);
239 /* And tell all other tinc daemons it's our MAC */
241 for(node = connection_tree->head; node; node = node->next) {
244 send_add_subnet(c, subnet);
247 if(!timeout_initialized(&age_subnets_event))
248 timeout_set(&age_subnets_event, age_subnets, NULL);
249 event_add(&age_subnets_event, &(struct timeval){10, 0});
252 subnet->expires = time(NULL) + macexpire;
258 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
260 struct icmp icmp = {0};
262 struct in_addr ip_src;
263 struct in_addr ip_dst;
269 /* Swap Ethernet source and destination addresses */
271 swap_mac_addresses(packet);
273 /* Copy headers from packet into properly aligned structs on the stack */
275 memcpy(&ip, packet->data + ether_size, ip_size);
277 /* Remember original source and destination */
282 oldlen = packet->len - ether_size;
284 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
285 icmp.icmp_nextmtu = htons(packet->len - ether_size);
287 if(oldlen >= IP_MSS - ip_size - icmp_size)
288 oldlen = IP_MSS - ip_size - icmp_size;
290 /* Copy first part of original contents to ICMP message */
292 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
294 /* Fill in IPv4 header */
297 ip.ip_hl = ip_size / 4;
299 ip.ip_len = htons(ip_size + icmp_size + oldlen);
303 ip.ip_p = IPPROTO_ICMP;
308 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
310 /* Fill in ICMP header */
312 icmp.icmp_type = type;
313 icmp.icmp_code = code;
316 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
317 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
319 /* Copy structs on stack back to packet */
321 memcpy(packet->data + ether_size, &ip, ip_size);
322 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
324 packet->len = ether_size + ip_size + icmp_size + oldlen;
326 send_packet(source, packet);
331 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
333 vpn_packet_t fragment;
334 int len, maxlen, todo;
336 uint16_t ip_off, origf;
338 memcpy(&ip, packet->data + ether_size, ip_size);
339 fragment.priority = packet->priority;
341 if(ip.ip_hl != ip_size / 4)
344 todo = ntohs(ip.ip_len) - ip_size;
346 if(ether_size + ip_size + todo != packet->len) {
347 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%zd)", packet->len, ether_size + ip_size + todo);
351 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
353 offset = packet->data + ether_size + ip_size;
354 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
355 ip_off = ntohs(ip.ip_off);
356 origf = ip_off & ~IP_OFFMASK;
357 ip_off &= IP_OFFMASK;
360 len = todo > maxlen ? maxlen : todo;
361 memcpy(fragment.data + ether_size + ip_size, offset, len);
365 ip.ip_len = htons(ip_size + len);
366 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
368 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
369 memcpy(fragment.data, packet->data, ether_size);
370 memcpy(fragment.data + ether_size, &ip, ip_size);
371 fragment.len = ether_size + ip_size + len;
373 send_packet(dest, &fragment);
379 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
384 memcpy(&dest, &packet->data[30], sizeof dest);
385 subnet = lookup_subnet_ipv4(&dest);
388 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
389 source->name, source->hostname,
395 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
399 if(subnet->owner == source) {
400 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
404 if(!subnet->owner->status.reachable)
405 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
407 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
408 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
410 if(priorityinheritance)
411 packet->priority = packet->data[15];
413 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
415 if(directonly && subnet->owner != via)
416 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
418 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
419 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);
420 if(packet->data[20] & 0x40) {
421 packet->len = MAX(via->mtu, 590);
422 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
424 fragment_ipv4_packet(via, packet);
430 clamp_mss(source, via, packet);
432 send_packet(subnet->owner, packet);
435 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
436 if(!checklength(source, packet, ether_size + ip_size))
439 if(((packet->data[30] & 0xf0) == 0xe0) || (
440 packet->data[30] == 255 &&
441 packet->data[31] == 255 &&
442 packet->data[32] == 255 &&
443 packet->data[33] == 255))
444 broadcast_packet(source, packet);
446 route_ipv4_unicast(source, packet);
451 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
453 struct icmp6_hdr icmp6 = {0};
457 struct in6_addr ip6_src; /* source address */
458 struct in6_addr ip6_dst; /* destination address */
466 /* Swap Ethernet source and destination addresses */
468 swap_mac_addresses(packet);
470 /* Copy headers from packet to structs on the stack */
472 memcpy(&ip6, packet->data + ether_size, ip6_size);
474 /* Remember original source and destination */
476 pseudo.ip6_src = ip6.ip6_dst;
477 pseudo.ip6_dst = ip6.ip6_src;
479 pseudo.length = packet->len - ether_size;
481 if(type == ICMP6_PACKET_TOO_BIG)
482 icmp6.icmp6_mtu = htonl(pseudo.length);
484 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
485 pseudo.length = IP_MSS - ip6_size - icmp6_size;
487 /* Copy first part of original contents to ICMP message */
489 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
491 /* Fill in IPv6 header */
493 ip6.ip6_flow = htonl(0x60000000UL);
494 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
495 ip6.ip6_nxt = IPPROTO_ICMPV6;
497 ip6.ip6_src = pseudo.ip6_src;
498 ip6.ip6_dst = pseudo.ip6_dst;
500 /* Fill in ICMP header */
502 icmp6.icmp6_type = type;
503 icmp6.icmp6_code = code;
504 icmp6.icmp6_cksum = 0;
506 /* Create pseudo header */
508 pseudo.length = htonl(icmp6_size + pseudo.length);
509 pseudo.next = htonl(IPPROTO_ICMPV6);
511 /* Generate checksum */
513 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
514 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
515 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
517 icmp6.icmp6_cksum = checksum;
519 /* Copy structs on stack back to packet */
521 memcpy(packet->data + ether_size, &ip6, ip6_size);
522 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
524 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
526 send_packet(source, packet);
529 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
534 memcpy(&dest, &packet->data[38], sizeof dest);
535 subnet = lookup_subnet_ipv6(&dest);
538 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
539 source->name, source->hostname,
549 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
553 if(subnet->owner == source) {
554 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
558 if(!subnet->owner->status.reachable)
559 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
561 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
562 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
564 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
566 if(directonly && subnet->owner != via)
567 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
569 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
570 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);
571 packet->len = MAX(via->mtu, 1294);
572 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
576 clamp_mss(source, via, packet);
578 send_packet(subnet->owner, packet);
583 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
585 struct nd_neighbor_solicit ns;
586 struct nd_opt_hdr opt;
592 struct in6_addr ip6_src; /* source address */
593 struct in6_addr ip6_dst; /* destination address */
598 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
601 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
603 if(source != myself) {
604 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
608 /* Copy headers from packet to structs on the stack */
610 memcpy(&ip6, packet->data + ether_size, ip6_size);
611 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
613 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
615 /* First, snatch the source address from the neighbor solicitation packet */
618 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
620 /* Check if this is a valid neighbor solicitation request */
622 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
623 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
624 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
628 /* Create pseudo header */
630 pseudo.ip6_src = ip6.ip6_src;
631 pseudo.ip6_dst = ip6.ip6_dst;
633 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
635 pseudo.length = htonl(ns_size);
636 pseudo.next = htonl(IPPROTO_ICMPV6);
638 /* Generate checksum */
640 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
641 checksum = inet_checksum(&ns, ns_size, checksum);
643 checksum = inet_checksum(&opt, opt_size, checksum);
644 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
648 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
652 /* Check if the IPv6 address exists on the VPN */
654 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
657 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
658 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
659 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
660 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
661 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
662 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
663 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
664 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
665 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
670 /* Check if it is for our own subnet */
672 if(subnet->owner == myself)
673 return; /* silently ignore */
675 /* Create neighbor advertation reply */
677 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
678 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
680 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
681 ip6.ip6_src = ns.nd_ns_target;
684 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
687 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
688 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
689 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
691 /* Create pseudo header */
693 pseudo.ip6_src = ip6.ip6_src;
694 pseudo.ip6_dst = ip6.ip6_dst;
696 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
698 pseudo.length = htonl(ns_size);
699 pseudo.next = htonl(IPPROTO_ICMPV6);
701 /* Generate checksum */
703 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
704 checksum = inet_checksum(&ns, ns_size, checksum);
706 checksum = inet_checksum(&opt, opt_size, checksum);
707 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
710 ns.nd_ns_hdr.icmp6_cksum = checksum;
712 /* Copy structs on stack back to packet */
714 memcpy(packet->data + ether_size, &ip6, ip6_size);
715 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
717 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
719 send_packet(source, packet);
722 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
723 if(!checklength(source, packet, ether_size + ip6_size))
726 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
727 route_neighborsol(source, packet);
731 if(packet->data[38] == 255)
732 broadcast_packet(source, packet);
734 route_ipv6_unicast(source, packet);
739 static void route_arp(node_t *source, vpn_packet_t *packet) {
740 struct ether_arp arp;
744 if(!checklength(source, packet, ether_size + arp_size))
747 if(source != myself) {
748 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
752 /* First, snatch the source address from the ARP packet */
755 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
757 /* Copy headers from packet to structs on the stack */
759 memcpy(&arp, packet->data + ether_size, arp_size);
761 /* Check if this is a valid ARP request */
763 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
764 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof addr || ntohs(arp.arp_op) != ARPOP_REQUEST) {
765 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
769 /* Check if the IPv4 address exists on the VPN */
771 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
774 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
775 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
780 /* Check if it is for our own subnet */
782 if(subnet->owner == myself)
783 return; /* silently ignore */
785 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
786 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
788 memcpy(&addr, arp.arp_tpa, sizeof addr); /* save protocol addr */
789 memcpy(arp.arp_tpa, arp.arp_spa, sizeof addr); /* swap destination and source protocol address */
790 memcpy(arp.arp_spa, &addr, sizeof addr); /* ... */
792 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
793 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
794 arp.arp_op = htons(ARPOP_REPLY);
796 /* Copy structs on stack back to packet */
798 memcpy(packet->data + ether_size, &arp, arp_size);
800 send_packet(source, packet);
803 static void route_mac(node_t *source, vpn_packet_t *packet) {
807 /* Learn source address */
809 if(source == myself) {
811 memcpy(&src, &packet->data[6], sizeof src);
815 /* Lookup destination address */
817 memcpy(&dest, &packet->data[0], sizeof dest);
818 subnet = lookup_subnet_mac(NULL, &dest);
821 broadcast_packet(source, packet);
825 if(subnet->owner == source) {
826 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
830 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
833 // Handle packets larger than PMTU
835 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
837 if(directonly && subnet->owner != via)
840 if(via && packet->len > via->mtu && via != myself) {
841 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);
842 uint16_t type = packet->data[12] << 8 | packet->data[13];
843 if(type == ETH_P_IP && packet->len > 590) {
844 if(packet->data[20] & 0x40) {
845 packet->len = via->mtu;
846 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
848 fragment_ipv4_packet(via, packet);
851 } else if(type == ETH_P_IPV6 && packet->len > 1294) {
852 packet->len = via->mtu;
853 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
858 clamp_mss(source, via, packet);
860 send_packet(subnet->owner, packet);
863 void route(node_t *source, vpn_packet_t *packet) {
864 if(forwarding_mode == FMODE_KERNEL && source != myself) {
865 send_packet(myself, packet);
869 if(!checklength(source, packet, ether_size))
872 switch (routing_mode) {
875 uint16_t type = packet->data[12] << 8 | packet->data[13];
879 route_arp(source, packet);
883 route_ipv4(source, packet);
887 route_ipv6(source, packet);
891 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
898 route_mac(source, packet);
902 broadcast_packet(source, packet);