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(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 age_subnets_event.time = time(NULL) + 10;
215 event_add(&age_subnets_event);
219 static void learn_mac(mac_t *address) {
224 subnet = lookup_subnet_mac(myself, address);
226 /* If we don't know this MAC address yet, store it */
229 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
230 address->x[0], address->x[1], address->x[2], address->x[3],
231 address->x[4], address->x[5]);
233 subnet = new_subnet();
234 subnet->type = SUBNET_MAC;
235 subnet->expires = time(NULL) + macexpire;
236 subnet->net.mac.address = *address;
238 subnet_add(myself, subnet);
239 subnet_update(myself, subnet, true);
241 /* And tell all other tinc daemons it's our MAC */
243 for(node = connection_tree->head; node; node = node->next) {
246 send_add_subnet(c, subnet);
249 age_subnets_event.handler = age_subnets;
250 age_subnets_event.time = time(NULL) + 10;
251 event_add(&age_subnets_event);
254 subnet->expires = time(NULL) + macexpire;
260 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
262 struct icmp icmp = {0};
264 struct in_addr ip_src;
265 struct in_addr ip_dst;
271 /* Swap Ethernet source and destination addresses */
273 swap_mac_addresses(packet);
275 /* Copy headers from packet into properly aligned structs on the stack */
277 memcpy(&ip, packet->data + ether_size, ip_size);
279 /* Remember original source and destination */
284 oldlen = packet->len - ether_size;
286 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
287 icmp.icmp_nextmtu = htons(packet->len - ether_size);
289 if(oldlen >= IP_MSS - ip_size - icmp_size)
290 oldlen = IP_MSS - ip_size - icmp_size;
292 /* Copy first part of original contents to ICMP message */
294 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
296 /* Fill in IPv4 header */
299 ip.ip_hl = ip_size / 4;
301 ip.ip_len = htons(ip_size + icmp_size + oldlen);
305 ip.ip_p = IPPROTO_ICMP;
310 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
312 /* Fill in ICMP header */
314 icmp.icmp_type = type;
315 icmp.icmp_code = code;
318 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
319 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
321 /* Copy structs on stack back to packet */
323 memcpy(packet->data + ether_size, &ip, ip_size);
324 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
326 packet->len = ether_size + ip_size + icmp_size + oldlen;
328 send_packet(source, packet);
333 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
335 vpn_packet_t fragment;
336 int len, maxlen, todo;
338 uint16_t ip_off, origf;
340 memcpy(&ip, packet->data + ether_size, ip_size);
341 fragment.priority = packet->priority;
343 if(ip.ip_hl != ip_size / 4)
346 todo = ntohs(ip.ip_len) - ip_size;
348 if(ether_size + ip_size + todo != packet->len) {
349 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%zd)", packet->len, ether_size + ip_size + todo);
353 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
355 offset = packet->data + ether_size + ip_size;
356 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
357 ip_off = ntohs(ip.ip_off);
358 origf = ip_off & ~IP_OFFMASK;
359 ip_off &= IP_OFFMASK;
362 len = todo > maxlen ? maxlen : todo;
363 memcpy(fragment.data + ether_size + ip_size, offset, len);
367 ip.ip_len = htons(ip_size + len);
368 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
370 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
371 memcpy(fragment.data, packet->data, ether_size);
372 memcpy(fragment.data + ether_size, &ip, ip_size);
373 fragment.len = ether_size + ip_size + len;
375 send_packet(dest, &fragment);
381 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
386 memcpy(&dest, &packet->data[30], sizeof dest);
387 subnet = lookup_subnet_ipv4(&dest);
390 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
391 source->name, source->hostname,
397 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
401 if(subnet->owner == source) {
402 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
406 if(!subnet->owner->status.reachable)
407 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
409 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
410 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
412 if(priorityinheritance)
413 packet->priority = packet->data[15];
415 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
417 if(directonly && subnet->owner != via)
418 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
420 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
421 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);
422 if(packet->data[20] & 0x40) {
423 packet->len = MAX(via->mtu, 590);
424 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
426 fragment_ipv4_packet(via, packet);
432 clamp_mss(source, via, packet);
434 send_packet(subnet->owner, packet);
437 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
438 if(!checklength(source, packet, ether_size + ip_size))
441 if(((packet->data[30] & 0xf0) == 0xe0) || (
442 packet->data[30] == 255 &&
443 packet->data[31] == 255 &&
444 packet->data[32] == 255 &&
445 packet->data[33] == 255))
446 broadcast_packet(source, packet);
448 route_ipv4_unicast(source, packet);
453 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
455 struct icmp6_hdr icmp6 = {0};
459 struct in6_addr ip6_src; /* source address */
460 struct in6_addr ip6_dst; /* destination address */
468 /* Swap Ethernet source and destination addresses */
470 swap_mac_addresses(packet);
472 /* Copy headers from packet to structs on the stack */
474 memcpy(&ip6, packet->data + ether_size, ip6_size);
476 /* Remember original source and destination */
478 pseudo.ip6_src = ip6.ip6_dst;
479 pseudo.ip6_dst = ip6.ip6_src;
481 pseudo.length = packet->len - ether_size;
483 if(type == ICMP6_PACKET_TOO_BIG)
484 icmp6.icmp6_mtu = htonl(pseudo.length);
486 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
487 pseudo.length = IP_MSS - ip6_size - icmp6_size;
489 /* Copy first part of original contents to ICMP message */
491 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
493 /* Fill in IPv6 header */
495 ip6.ip6_flow = htonl(0x60000000UL);
496 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
497 ip6.ip6_nxt = IPPROTO_ICMPV6;
499 ip6.ip6_src = pseudo.ip6_src;
500 ip6.ip6_dst = pseudo.ip6_dst;
502 /* Fill in ICMP header */
504 icmp6.icmp6_type = type;
505 icmp6.icmp6_code = code;
506 icmp6.icmp6_cksum = 0;
508 /* Create pseudo header */
510 pseudo.length = htonl(icmp6_size + pseudo.length);
511 pseudo.next = htonl(IPPROTO_ICMPV6);
513 /* Generate checksum */
515 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
516 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
517 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
519 icmp6.icmp6_cksum = checksum;
521 /* Copy structs on stack back to packet */
523 memcpy(packet->data + ether_size, &ip6, ip6_size);
524 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
526 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
528 send_packet(source, packet);
531 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
536 memcpy(&dest, &packet->data[38], sizeof dest);
537 subnet = lookup_subnet_ipv6(&dest);
540 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
541 source->name, source->hostname,
551 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
555 if(subnet->owner == source) {
556 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
560 if(!subnet->owner->status.reachable)
561 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
563 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
564 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
566 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
568 if(directonly && subnet->owner != via)
569 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
571 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
572 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);
573 packet->len = MAX(via->mtu, 1294);
574 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
578 clamp_mss(source, via, packet);
580 send_packet(subnet->owner, packet);
585 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
587 struct nd_neighbor_solicit ns;
588 struct nd_opt_hdr opt;
594 struct in6_addr ip6_src; /* source address */
595 struct in6_addr ip6_dst; /* destination address */
600 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
603 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
605 if(source != myself) {
606 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
610 /* Copy headers from packet to structs on the stack */
612 memcpy(&ip6, packet->data + ether_size, ip6_size);
613 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
615 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
617 /* First, snatch the source address from the neighbor solicitation packet */
620 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
622 /* Check if this is a valid neighbor solicitation request */
624 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
625 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
626 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
630 /* Create pseudo header */
632 pseudo.ip6_src = ip6.ip6_src;
633 pseudo.ip6_dst = ip6.ip6_dst;
635 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
637 pseudo.length = htonl(ns_size);
638 pseudo.next = htonl(IPPROTO_ICMPV6);
640 /* Generate checksum */
642 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
643 checksum = inet_checksum(&ns, ns_size, checksum);
645 checksum = inet_checksum(&opt, opt_size, checksum);
646 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
650 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
654 /* Check if the IPv6 address exists on the VPN */
656 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
659 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
660 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
661 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
662 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
663 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
664 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
665 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
666 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
667 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
672 /* Check if it is for our own subnet */
674 if(subnet->owner == myself)
675 return; /* silently ignore */
677 /* Create neighbor advertation reply */
679 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
680 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
682 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
683 ip6.ip6_src = ns.nd_ns_target;
686 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
689 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
690 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
691 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
693 /* Create pseudo header */
695 pseudo.ip6_src = ip6.ip6_src;
696 pseudo.ip6_dst = ip6.ip6_dst;
698 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
700 pseudo.length = htonl(ns_size);
701 pseudo.next = htonl(IPPROTO_ICMPV6);
703 /* Generate checksum */
705 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
706 checksum = inet_checksum(&ns, ns_size, checksum);
708 checksum = inet_checksum(&opt, opt_size, checksum);
709 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
712 ns.nd_ns_hdr.icmp6_cksum = checksum;
714 /* Copy structs on stack back to packet */
716 memcpy(packet->data + ether_size, &ip6, ip6_size);
717 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
719 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
721 send_packet(source, packet);
724 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
725 if(!checklength(source, packet, ether_size + ip6_size))
728 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
729 route_neighborsol(source, packet);
733 if(packet->data[38] == 255)
734 broadcast_packet(source, packet);
736 route_ipv6_unicast(source, packet);
741 static void route_arp(node_t *source, vpn_packet_t *packet) {
742 struct ether_arp arp;
746 if(!checklength(source, packet, ether_size + arp_size))
749 if(source != myself) {
750 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
754 /* First, snatch the source address from the ARP packet */
757 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
759 /* Copy headers from packet to structs on the stack */
761 memcpy(&arp, packet->data + ether_size, arp_size);
763 /* Check if this is a valid ARP request */
765 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
766 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof addr || ntohs(arp.arp_op) != ARPOP_REQUEST) {
767 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
771 /* Check if the IPv4 address exists on the VPN */
773 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
776 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
777 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
782 /* Check if it is for our own subnet */
784 if(subnet->owner == myself)
785 return; /* silently ignore */
787 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
788 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
790 memcpy(&addr, arp.arp_tpa, sizeof addr); /* save protocol addr */
791 memcpy(arp.arp_tpa, arp.arp_spa, sizeof addr); /* swap destination and source protocol address */
792 memcpy(arp.arp_spa, &addr, sizeof addr); /* ... */
794 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
795 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
796 arp.arp_op = htons(ARPOP_REPLY);
798 /* Copy structs on stack back to packet */
800 memcpy(packet->data + ether_size, &arp, arp_size);
802 send_packet(source, packet);
805 static void route_mac(node_t *source, vpn_packet_t *packet) {
809 /* Learn source address */
811 if(source == myself) {
813 memcpy(&src, &packet->data[6], sizeof src);
817 /* Lookup destination address */
819 memcpy(&dest, &packet->data[0], sizeof dest);
820 subnet = lookup_subnet_mac(NULL, &dest);
823 broadcast_packet(source, packet);
827 if(subnet->owner == source) {
828 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
832 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
835 // Handle packets larger than PMTU
837 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
839 if(directonly && subnet->owner != via)
842 if(via && packet->len > via->mtu && via != myself) {
843 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);
844 uint16_t type = packet->data[12] << 8 | packet->data[13];
845 if(type == ETH_P_IP && packet->len > 590) {
846 if(packet->data[20] & 0x40) {
847 packet->len = via->mtu;
848 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
850 fragment_ipv4_packet(via, packet);
853 } else if(type == ETH_P_IPV6 && packet->len > 1294) {
854 packet->len = via->mtu;
855 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
860 clamp_mss(source, via, packet);
862 send_packet(subnet->owner, packet);
865 void route(node_t *source, vpn_packet_t *packet) {
866 if(forwarding_mode == FMODE_KERNEL && source != myself) {
867 send_packet(myself, packet);
871 if(!checklength(source, packet, ether_size))
874 switch (routing_mode) {
877 uint16_t type = packet->data[12] << 8 | packet->data[13];
881 route_arp(source, packet);
885 route_ipv4(source, packet);
889 route_ipv6(source, packet);
893 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
900 route_mac(source, packet);
904 broadcast_packet(source, packet);