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);
53 #define max(a, b) ((a) > (b) ? (a) : (b))
55 static struct event age_subnets_event;
59 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
61 uint32_t checksum = prevsum ^ 0xFFFF;
69 checksum += *(uint8_t *)p;
72 checksum = (checksum & 0xFFFF) + (checksum >> 16);
77 static bool ratelimit(int frequency) {
78 static time_t lasttime = 0;
80 time_t now = time(NULL);
83 if(++count > frequency)
93 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
94 if(packet->len < length) {
95 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
101 static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
102 if(!source || !via || !(via->options & OPTION_CLAMP_MSS))
105 uint16_t mtu = source->mtu;
106 if(via != myself && via->mtu < mtu)
109 /* Find TCP header */
111 uint16_t type = packet->data[12] << 8 | packet->data[13];
113 if(type == ETH_P_IP && packet->data[23] == 6)
114 start = 14 + (packet->data[14] & 0xf) * 4;
115 else if(type == ETH_P_IPV6 && packet->data[20] == 6)
118 if(!start || packet->len <= start + 20)
121 /* Use data offset field to calculate length of options field */
122 int len = ((packet->data[start + 12] >> 4) - 5) * 4;
124 if(packet->len < start + 20 + len)
127 /* Search for MSS option header */
128 for(int i = 0; i < len;) {
129 if(packet->data[start + 20 + i] == 0)
132 if(packet->data[start + 20 + i] == 1) {
137 if(i > len - 2 || i > len - packet->data[start + 21 + i])
140 if(packet->data[start + 20 + i] != 2) {
141 if(packet->data[start + 21 + i] < 2)
143 i += packet->data[start + 21 + i];
147 if(packet->data[start + 21] != 4)
151 uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
152 uint16_t newmss = mtu - start - 20;
153 uint16_t csum = packet->data[start + 16] << 8 | packet->data[start + 17];
158 ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
160 /* Update the MSS value and the checksum */
161 packet->data[start + 22 + i] = newmss >> 8;
162 packet->data[start + 23 + i] = newmss & 0xff;
167 packet->data[start + 16] = csum >> 8;
168 packet->data[start + 17] = csum & 0xff;
173 static void swap_mac_addresses(vpn_packet_t *packet) {
175 memcpy(&tmp, &packet->data[0], sizeof tmp);
176 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
177 memcpy(&packet->data[6], &tmp, sizeof tmp);
180 static void age_subnets(int fd, short events, void *data) {
183 splay_node_t *node, *next, *node2;
185 time_t now = time(NULL);
187 for(node = myself->subnet_tree->head; node; node = next) {
190 if(s->expires && s->expires < now) {
192 char netstr[MAXNETSTR];
193 if(net2str(netstr, sizeof netstr, s))
194 logger(LOG_INFO, "Subnet %s expired", netstr);
197 for(node2 = connection_tree->head; node2; node2 = node2->next) {
200 send_del_subnet(c, s);
203 subnet_del(myself, s);
211 event_add(&age_subnets_event, &(struct timeval){10, 0});
214 static void learn_mac(mac_t *address) {
219 subnet = lookup_subnet_mac(myself, address);
221 /* If we don't know this MAC address yet, store it */
224 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
225 address->x[0], address->x[1], address->x[2], address->x[3],
226 address->x[4], address->x[5]);
228 subnet = new_subnet();
229 subnet->type = SUBNET_MAC;
230 subnet->expires = time(NULL) + macexpire;
231 subnet->net.mac.address = *address;
233 subnet_add(myself, subnet);
234 subnet_update(myself, subnet, true);
236 /* And tell all other tinc daemons it's our MAC */
238 for(node = connection_tree->head; node; node = node->next) {
241 send_add_subnet(c, subnet);
244 if(!timeout_initialized(&age_subnets_event))
245 timeout_set(&age_subnets_event, age_subnets, NULL);
246 event_add(&age_subnets_event, &(struct timeval){10, 0});
249 subnet->expires = time(NULL) + macexpire;
255 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
257 struct icmp icmp = {0};
259 struct in_addr ip_src;
260 struct in_addr ip_dst;
266 /* Swap Ethernet source and destination addresses */
268 swap_mac_addresses(packet);
270 /* Copy headers from packet into properly aligned structs on the stack */
272 memcpy(&ip, packet->data + ether_size, ip_size);
274 /* Remember original source and destination */
279 oldlen = packet->len - ether_size;
281 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
282 icmp.icmp_nextmtu = htons(packet->len - ether_size);
284 if(oldlen >= IP_MSS - ip_size - icmp_size)
285 oldlen = IP_MSS - ip_size - icmp_size;
287 /* Copy first part of original contents to ICMP message */
289 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
291 /* Fill in IPv4 header */
294 ip.ip_hl = ip_size / 4;
296 ip.ip_len = htons(ip_size + icmp_size + oldlen);
300 ip.ip_p = IPPROTO_ICMP;
305 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
307 /* Fill in ICMP header */
309 icmp.icmp_type = type;
310 icmp.icmp_code = code;
313 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
314 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
316 /* Copy structs on stack back to packet */
318 memcpy(packet->data + ether_size, &ip, ip_size);
319 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
321 packet->len = ether_size + ip_size + icmp_size + oldlen;
323 send_packet(source, packet);
328 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
330 vpn_packet_t fragment;
331 int len, maxlen, todo;
333 uint16_t ip_off, origf;
335 memcpy(&ip, packet->data + ether_size, ip_size);
336 fragment.priority = packet->priority;
338 if(ip.ip_hl != ip_size / 4)
341 todo = ntohs(ip.ip_len) - ip_size;
343 if(ether_size + ip_size + todo != packet->len) {
344 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%zd)", packet->len, ether_size + ip_size + todo);
348 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
350 offset = packet->data + ether_size + ip_size;
351 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
352 ip_off = ntohs(ip.ip_off);
353 origf = ip_off & ~IP_OFFMASK;
354 ip_off &= IP_OFFMASK;
357 len = todo > maxlen ? maxlen : todo;
358 memcpy(fragment.data + ether_size + ip_size, offset, len);
362 ip.ip_len = htons(ip_size + len);
363 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
365 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
366 memcpy(fragment.data, packet->data, ether_size);
367 memcpy(fragment.data + ether_size, &ip, ip_size);
368 fragment.len = ether_size + ip_size + len;
370 send_packet(dest, &fragment);
376 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
381 memcpy(&dest, &packet->data[30], sizeof dest);
382 subnet = lookup_subnet_ipv4(&dest);
385 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
386 source->name, source->hostname,
392 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
396 if(subnet->owner == source) {
397 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
401 if(!subnet->owner->status.reachable)
402 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
404 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
405 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
407 if(priorityinheritance)
408 packet->priority = packet->data[15];
410 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
412 if(directonly && subnet->owner != via)
413 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
415 if(via && packet->len > max(via->mtu, 590) && via != myself) {
416 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);
417 if(packet->data[20] & 0x40) {
418 packet->len = max(via->mtu, 590);
419 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
421 fragment_ipv4_packet(via, packet);
427 clamp_mss(source, via, packet);
429 send_packet(subnet->owner, packet);
432 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
433 if(!checklength(source, packet, ether_size + ip_size))
436 if(((packet->data[30] & 0xf0) == 0xe0) || (
437 packet->data[30] == 255 &&
438 packet->data[31] == 255 &&
439 packet->data[32] == 255 &&
440 packet->data[33] == 255))
441 broadcast_packet(source, packet);
443 route_ipv4_unicast(source, packet);
448 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
450 struct icmp6_hdr icmp6 = {0};
454 struct in6_addr ip6_src; /* source address */
455 struct in6_addr ip6_dst; /* destination address */
463 /* Swap Ethernet source and destination addresses */
465 swap_mac_addresses(packet);
467 /* Copy headers from packet to structs on the stack */
469 memcpy(&ip6, packet->data + ether_size, ip6_size);
471 /* Remember original source and destination */
473 pseudo.ip6_src = ip6.ip6_dst;
474 pseudo.ip6_dst = ip6.ip6_src;
476 pseudo.length = packet->len - ether_size;
478 if(type == ICMP6_PACKET_TOO_BIG)
479 icmp6.icmp6_mtu = htonl(pseudo.length);
481 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
482 pseudo.length = IP_MSS - ip6_size - icmp6_size;
484 /* Copy first part of original contents to ICMP message */
486 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
488 /* Fill in IPv6 header */
490 ip6.ip6_flow = htonl(0x60000000UL);
491 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
492 ip6.ip6_nxt = IPPROTO_ICMPV6;
494 ip6.ip6_src = pseudo.ip6_src;
495 ip6.ip6_dst = pseudo.ip6_dst;
497 /* Fill in ICMP header */
499 icmp6.icmp6_type = type;
500 icmp6.icmp6_code = code;
501 icmp6.icmp6_cksum = 0;
503 /* Create pseudo header */
505 pseudo.length = htonl(icmp6_size + pseudo.length);
506 pseudo.next = htonl(IPPROTO_ICMPV6);
508 /* Generate checksum */
510 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
511 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
512 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
514 icmp6.icmp6_cksum = checksum;
516 /* Copy structs on stack back to packet */
518 memcpy(packet->data + ether_size, &ip6, ip6_size);
519 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
521 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
523 send_packet(source, packet);
526 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
531 memcpy(&dest, &packet->data[38], sizeof dest);
532 subnet = lookup_subnet_ipv6(&dest);
535 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
536 source->name, source->hostname,
546 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
550 if(subnet->owner == source) {
551 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
555 if(!subnet->owner->status.reachable)
556 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
558 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
559 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
561 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
563 if(directonly && subnet->owner != via)
564 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
566 if(via && packet->len > max(via->mtu, 1294) && via != myself) {
567 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);
568 packet->len = max(via->mtu, 1294);
569 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
573 clamp_mss(source, via, packet);
575 send_packet(subnet->owner, packet);
580 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
582 struct nd_neighbor_solicit ns;
583 struct nd_opt_hdr opt;
589 struct in6_addr ip6_src; /* source address */
590 struct in6_addr ip6_dst; /* destination address */
595 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
598 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
600 if(source != myself) {
601 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
605 /* Copy headers from packet to structs on the stack */
607 memcpy(&ip6, packet->data + ether_size, ip6_size);
608 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
610 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
612 /* First, snatch the source address from the neighbor solicitation packet */
615 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
617 /* Check if this is a valid neighbor solicitation request */
619 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
620 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
621 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
625 /* Create pseudo header */
627 pseudo.ip6_src = ip6.ip6_src;
628 pseudo.ip6_dst = ip6.ip6_dst;
630 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
632 pseudo.length = htonl(ns_size);
633 pseudo.next = htonl(IPPROTO_ICMPV6);
635 /* Generate checksum */
637 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
638 checksum = inet_checksum(&ns, ns_size, checksum);
640 checksum = inet_checksum(&opt, opt_size, checksum);
641 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
645 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
649 /* Check if the IPv6 address exists on the VPN */
651 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
654 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
655 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
656 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
657 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
658 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
659 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
660 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
661 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
662 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
667 /* Check if it is for our own subnet */
669 if(subnet->owner == myself)
670 return; /* silently ignore */
672 /* Create neighbor advertation reply */
674 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
675 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
677 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
678 ip6.ip6_src = ns.nd_ns_target;
681 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
684 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
685 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
686 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
688 /* Create pseudo header */
690 pseudo.ip6_src = ip6.ip6_src;
691 pseudo.ip6_dst = ip6.ip6_dst;
693 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
695 pseudo.length = htonl(ns_size);
696 pseudo.next = htonl(IPPROTO_ICMPV6);
698 /* Generate checksum */
700 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
701 checksum = inet_checksum(&ns, ns_size, checksum);
703 checksum = inet_checksum(&opt, opt_size, checksum);
704 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
707 ns.nd_ns_hdr.icmp6_cksum = checksum;
709 /* Copy structs on stack back to packet */
711 memcpy(packet->data + ether_size, &ip6, ip6_size);
712 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
714 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
716 send_packet(source, packet);
719 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
720 if(!checklength(source, packet, ether_size + ip6_size))
723 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
724 route_neighborsol(source, packet);
728 if(packet->data[38] == 255)
729 broadcast_packet(source, packet);
731 route_ipv6_unicast(source, packet);
736 static void route_arp(node_t *source, vpn_packet_t *packet) {
737 struct ether_arp arp;
741 if(!checklength(source, packet, ether_size + arp_size))
744 if(source != myself) {
745 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
749 /* First, snatch the source address from the ARP packet */
752 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
754 /* Copy headers from packet to structs on the stack */
756 memcpy(&arp, packet->data + ether_size, arp_size);
758 /* Check if this is a valid ARP request */
760 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
761 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof addr || ntohs(arp.arp_op) != ARPOP_REQUEST) {
762 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
766 /* Check if the IPv4 address exists on the VPN */
768 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
771 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
772 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
777 /* Check if it is for our own subnet */
779 if(subnet->owner == myself)
780 return; /* silently ignore */
782 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
783 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
785 memcpy(&addr, arp.arp_tpa, sizeof addr); /* save protocol addr */
786 memcpy(arp.arp_tpa, arp.arp_spa, sizeof addr); /* swap destination and source protocol address */
787 memcpy(arp.arp_spa, &addr, sizeof addr); /* ... */
789 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
790 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
791 arp.arp_op = htons(ARPOP_REPLY);
793 /* Copy structs on stack back to packet */
795 memcpy(packet->data + ether_size, &arp, arp_size);
797 send_packet(source, packet);
800 static void route_mac(node_t *source, vpn_packet_t *packet) {
804 /* Learn source address */
806 if(source == myself) {
808 memcpy(&src, &packet->data[6], sizeof src);
812 /* Lookup destination address */
814 memcpy(&dest, &packet->data[0], sizeof dest);
815 subnet = lookup_subnet_mac(NULL, &dest);
818 broadcast_packet(source, packet);
822 if(subnet->owner == source) {
823 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
827 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
830 // Handle packets larger than PMTU
832 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
834 if(directonly && subnet->owner != via)
837 if(via && packet->len > via->mtu && via != myself) {
838 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);
839 uint16_t type = packet->data[12] << 8 | packet->data[13];
840 if(type == ETH_P_IP && packet->len > 590) {
841 if(packet->data[20] & 0x40) {
842 packet->len = via->mtu;
843 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
845 fragment_ipv4_packet(via, packet);
848 } else if(type == ETH_P_IPV6 && packet->len > 1294) {
849 packet->len = via->mtu;
850 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
855 clamp_mss(source, via, packet);
857 send_packet(subnet->owner, packet);
860 void route(node_t *source, vpn_packet_t *packet) {
861 if(forwarding_mode == FMODE_KERNEL && source != myself) {
862 send_packet(myself, packet);
866 if(!checklength(source, packet, ether_size))
869 switch (routing_mode) {
872 uint16_t type = packet->data[12] << 8 | packet->data[13];
876 route_arp(source, packet);
880 route_ipv4(source, packet);
884 route_ipv6(source, packet);
888 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
895 route_mac(source, packet);
899 broadcast_packet(source, packet);