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.
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))
60 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
62 uint32_t checksum = prevsum ^ 0xFFFF;
70 checksum += *(uint8_t *)p;
73 checksum = (checksum & 0xFFFF) + (checksum >> 16);
78 static bool ratelimit(int frequency) {
79 static time_t lasttime = 0;
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 learn_mac(mac_t *address) {
185 subnet = lookup_subnet_mac(myself, address);
187 /* If we don't know this MAC address yet, store it */
190 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
191 address->x[0], address->x[1], address->x[2], address->x[3],
192 address->x[4], address->x[5]);
194 subnet = new_subnet();
195 subnet->type = SUBNET_MAC;
196 subnet->expires = now + macexpire;
197 subnet->net.mac.address = *address;
199 subnet_add(myself, subnet);
200 subnet_update(myself, subnet, true);
202 /* And tell all other tinc daemons it's our MAC */
204 for(node = connection_tree->head; node; node = node->next) {
207 send_add_subnet(c, subnet);
212 subnet->expires = now + macexpire;
215 void age_subnets(void) {
218 avl_node_t *node, *next, *node2;
220 for(node = myself->subnet_tree->head; node; node = next) {
223 if(s->expires && s->expires < now) {
225 char netstr[MAXNETSTR];
226 if(net2str(netstr, sizeof netstr, s))
227 logger(LOG_INFO, "Subnet %s expired", netstr);
230 for(node2 = connection_tree->head; node2; node2 = node2->next) {
233 send_del_subnet(c, s);
236 subnet_update(myself, s, false);
237 subnet_del(myself, s);
244 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
246 struct icmp icmp = {0};
248 struct in_addr ip_src;
249 struct in_addr ip_dst;
255 /* Swap Ethernet source and destination addresses */
257 swap_mac_addresses(packet);
259 /* Copy headers from packet into properly aligned structs on the stack */
261 memcpy(&ip, packet->data + ether_size, ip_size);
263 /* Remember original source and destination */
268 oldlen = packet->len - ether_size;
270 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
271 icmp.icmp_nextmtu = htons(packet->len - ether_size);
273 if(oldlen >= IP_MSS - ip_size - icmp_size)
274 oldlen = IP_MSS - ip_size - icmp_size;
276 /* Copy first part of original contents to ICMP message */
278 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
280 /* Fill in IPv4 header */
283 ip.ip_hl = ip_size / 4;
285 ip.ip_len = htons(ip_size + icmp_size + oldlen);
289 ip.ip_p = IPPROTO_ICMP;
294 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
296 /* Fill in ICMP header */
298 icmp.icmp_type = type;
299 icmp.icmp_code = code;
302 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
303 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
305 /* Copy structs on stack back to packet */
307 memcpy(packet->data + ether_size, &ip, ip_size);
308 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
310 packet->len = ether_size + ip_size + icmp_size + oldlen;
312 send_packet(source, packet);
317 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
319 vpn_packet_t fragment;
320 int len, maxlen, todo;
322 uint16_t ip_off, origf;
324 memcpy(&ip, packet->data + ether_size, ip_size);
325 fragment.priority = packet->priority;
327 if(ip.ip_hl != ip_size / 4)
330 todo = ntohs(ip.ip_len) - ip_size;
332 if(ether_size + ip_size + todo != packet->len) {
333 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%zd)", packet->len, ether_size + ip_size + todo);
337 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
339 offset = packet->data + ether_size + ip_size;
340 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
341 ip_off = ntohs(ip.ip_off);
342 origf = ip_off & ~IP_OFFMASK;
343 ip_off &= IP_OFFMASK;
346 len = todo > maxlen ? maxlen : todo;
347 memcpy(fragment.data + ether_size + ip_size, offset, len);
351 ip.ip_len = htons(ip_size + len);
352 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
354 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
355 memcpy(fragment.data, packet->data, ether_size);
356 memcpy(fragment.data + ether_size, &ip, ip_size);
357 fragment.len = ether_size + ip_size + len;
359 send_packet(dest, &fragment);
365 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
370 memcpy(&dest, &packet->data[30], sizeof dest);
371 subnet = lookup_subnet_ipv4(&dest);
374 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
375 source->name, source->hostname,
381 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
385 if(subnet->owner == source) {
386 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
390 if(!subnet->owner->status.reachable)
391 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
393 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
394 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
396 if(priorityinheritance)
397 packet->priority = packet->data[15];
399 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
401 if(directonly && subnet->owner != via)
402 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
404 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
405 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);
406 if(packet->data[20] & 0x40) {
407 packet->len = MAX(via->mtu, 590);
408 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
410 fragment_ipv4_packet(via, packet);
416 clamp_mss(source, via, packet);
418 send_packet(subnet->owner, packet);
421 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
422 if(!checklength(source, packet, ether_size + ip_size))
425 if(((packet->data[30] & 0xf0) == 0xe0) || (
426 packet->data[30] == 255 &&
427 packet->data[31] == 255 &&
428 packet->data[32] == 255 &&
429 packet->data[33] == 255))
430 broadcast_packet(source, packet);
432 route_ipv4_unicast(source, packet);
437 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
439 struct icmp6_hdr icmp6 = {0};
443 struct in6_addr ip6_src; /* source address */
444 struct in6_addr ip6_dst; /* destination address */
452 /* Swap Ethernet source and destination addresses */
454 swap_mac_addresses(packet);
456 /* Copy headers from packet to structs on the stack */
458 memcpy(&ip6, packet->data + ether_size, ip6_size);
460 /* Remember original source and destination */
462 pseudo.ip6_src = ip6.ip6_dst;
463 pseudo.ip6_dst = ip6.ip6_src;
465 pseudo.length = packet->len - ether_size;
467 if(type == ICMP6_PACKET_TOO_BIG)
468 icmp6.icmp6_mtu = htonl(pseudo.length);
470 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
471 pseudo.length = IP_MSS - ip6_size - icmp6_size;
473 /* Copy first part of original contents to ICMP message */
475 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
477 /* Fill in IPv6 header */
479 ip6.ip6_flow = htonl(0x60000000UL);
480 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
481 ip6.ip6_nxt = IPPROTO_ICMPV6;
483 ip6.ip6_src = pseudo.ip6_src;
484 ip6.ip6_dst = pseudo.ip6_dst;
486 /* Fill in ICMP header */
488 icmp6.icmp6_type = type;
489 icmp6.icmp6_code = code;
490 icmp6.icmp6_cksum = 0;
492 /* Create pseudo header */
494 pseudo.length = htonl(icmp6_size + pseudo.length);
495 pseudo.next = htonl(IPPROTO_ICMPV6);
497 /* Generate checksum */
499 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
500 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
501 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
503 icmp6.icmp6_cksum = checksum;
505 /* Copy structs on stack back to packet */
507 memcpy(packet->data + ether_size, &ip6, ip6_size);
508 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
510 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
512 send_packet(source, packet);
515 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
520 memcpy(&dest, &packet->data[38], sizeof dest);
521 subnet = lookup_subnet_ipv6(&dest);
524 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
525 source->name, source->hostname,
535 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
539 if(subnet->owner == source) {
540 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
544 if(!subnet->owner->status.reachable)
545 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
547 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
548 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
550 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
552 if(directonly && subnet->owner != via)
553 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
555 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
556 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);
557 packet->len = MAX(via->mtu, 1294);
558 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
562 clamp_mss(source, via, packet);
564 send_packet(subnet->owner, packet);
569 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
571 struct nd_neighbor_solicit ns;
572 struct nd_opt_hdr opt;
578 struct in6_addr ip6_src; /* source address */
579 struct in6_addr ip6_dst; /* destination address */
584 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
587 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
589 if(source != myself) {
590 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
594 /* Copy headers from packet to structs on the stack */
596 memcpy(&ip6, packet->data + ether_size, ip6_size);
597 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
599 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
601 /* First, snatch the source address from the neighbor solicitation packet */
604 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
606 /* Check if this is a valid neighbor solicitation request */
608 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
609 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
610 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
614 /* Create pseudo header */
616 pseudo.ip6_src = ip6.ip6_src;
617 pseudo.ip6_dst = ip6.ip6_dst;
619 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
621 pseudo.length = htonl(ns_size);
622 pseudo.next = htonl(IPPROTO_ICMPV6);
624 /* Generate checksum */
626 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
627 checksum = inet_checksum(&ns, ns_size, checksum);
629 checksum = inet_checksum(&opt, opt_size, checksum);
630 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
634 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
638 /* Check if the IPv6 address exists on the VPN */
640 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
643 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
644 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
645 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
646 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
647 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
648 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
649 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
650 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
651 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
656 /* Check if it is for our own subnet */
658 if(subnet->owner == myself)
659 return; /* silently ignore */
661 /* Create neighbor advertation reply */
663 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
664 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
666 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
667 ip6.ip6_src = ns.nd_ns_target;
670 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
673 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
674 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
675 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
677 /* Create pseudo header */
679 pseudo.ip6_src = ip6.ip6_src;
680 pseudo.ip6_dst = ip6.ip6_dst;
682 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
684 pseudo.length = htonl(ns_size);
685 pseudo.next = htonl(IPPROTO_ICMPV6);
687 /* Generate checksum */
689 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
690 checksum = inet_checksum(&ns, ns_size, checksum);
692 checksum = inet_checksum(&opt, opt_size, checksum);
693 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
696 ns.nd_ns_hdr.icmp6_cksum = checksum;
698 /* Copy structs on stack back to packet */
700 memcpy(packet->data + ether_size, &ip6, ip6_size);
701 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
703 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
705 send_packet(source, packet);
708 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
709 if(!checklength(source, packet, ether_size + ip6_size))
712 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
713 route_neighborsol(source, packet);
717 if(packet->data[38] == 255)
718 broadcast_packet(source, packet);
720 route_ipv6_unicast(source, packet);
725 static void route_arp(node_t *source, vpn_packet_t *packet) {
726 struct ether_arp arp;
730 if(!checklength(source, packet, ether_size + arp_size))
733 if(source != myself) {
734 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
738 /* First, snatch the source address from the ARP packet */
741 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
743 /* Copy headers from packet to structs on the stack */
745 memcpy(&arp, packet->data + ether_size, arp_size);
747 /* Check if this is a valid ARP request */
749 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
750 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
751 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
755 /* Check if the IPv4 address exists on the VPN */
757 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
760 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
761 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
766 /* Check if it is for our own subnet */
768 if(subnet->owner == myself)
769 return; /* silently ignore */
771 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
772 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
774 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
775 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
776 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
778 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
779 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
780 arp.arp_op = htons(ARPOP_REPLY);
782 /* Copy structs on stack back to packet */
784 memcpy(packet->data + ether_size, &arp, arp_size);
786 send_packet(source, packet);
789 static void route_mac(node_t *source, vpn_packet_t *packet) {
793 /* Learn source address */
795 if(source == myself) {
797 memcpy(&src, &packet->data[6], sizeof src);
801 /* Lookup destination address */
803 memcpy(&dest, &packet->data[0], sizeof dest);
804 subnet = lookup_subnet_mac(NULL, &dest);
807 broadcast_packet(source, packet);
811 if(subnet->owner == source) {
812 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
816 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
819 // Handle packets larger than PMTU
821 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
823 if(directonly && subnet->owner != via)
826 if(via && packet->len > via->mtu && via != myself) {
827 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);
828 uint16_t type = packet->data[12] << 8 | packet->data[13];
829 if(type == ETH_P_IP && packet->len > 590) {
830 if(packet->data[20] & 0x40) {
831 packet->len = via->mtu;
832 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
834 fragment_ipv4_packet(via, packet);
837 } else if(type == ETH_P_IPV6 && packet->len > 1294) {
838 packet->len = via->mtu;
839 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
844 clamp_mss(source, via, packet);
846 send_packet(subnet->owner, packet);
849 void route(node_t *source, vpn_packet_t *packet) {
850 if(forwarding_mode == FMODE_KERNEL && source != myself) {
851 send_packet(myself, packet);
855 if(!checklength(source, packet, ether_size))
858 switch (routing_mode) {
861 uint16_t type = packet->data[12] << 8 | packet->data[13];
865 route_arp(source, packet);
869 route_ipv4(source, packet);
873 route_ipv6(source, packet);
877 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
884 route_mac(source, packet);
888 broadcast_packet(source, packet);