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 priorityinheritance = false;
39 bool overwrite_mac = false;
40 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
42 /* Sizes of various headers */
44 static const size_t ether_size = sizeof(struct ether_header);
45 static const size_t arp_size = sizeof(struct ether_arp);
46 static const size_t ip_size = sizeof(struct ip);
47 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
48 static const size_t ip6_size = sizeof(struct ip6_hdr);
49 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
50 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
51 static const size_t opt_size = sizeof(struct nd_opt_hdr);
52 #define max(a, b) ((a) > (b) ? (a) : (b))
56 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
58 uint32_t checksum = prevsum ^ 0xFFFF;
66 checksum += *(uint8_t *)p;
69 checksum = (checksum & 0xFFFF) + (checksum >> 16);
74 static bool ratelimit(int frequency) {
75 static time_t lasttime = 0;
79 if(++count > frequency)
89 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
90 if(packet->len < length) {
91 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
97 static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
98 if(!source || !via || !(via->options & OPTION_CLAMP_MSS))
101 uint16_t mtu = source->mtu;
102 if(via != myself && via->mtu < mtu)
105 /* Find TCP header */
107 uint16_t type = packet->data[12] << 8 | packet->data[13];
109 if(type == ETH_P_IP && packet->data[23] == 6)
110 start = 14 + (packet->data[14] & 0xf) * 4;
111 else if(type == ETH_P_IPV6 && packet->data[20] == 6)
114 if(!start || packet->len <= start + 20)
117 /* Use data offset field to calculate length of options field */
118 int len = ((packet->data[start + 12] >> 4) - 5) * 4;
120 if(packet->len < start + 20 + len)
123 /* Search for MSS option header */
124 for(int i = 0; i < len;) {
125 if(packet->data[start + 20 + i] == 0)
128 if(packet->data[start + 20 + i] == 1) {
133 if(i > len - 2 || i > len - packet->data[start + 21 + i])
136 if(packet->data[start + 20 + i] != 2) {
137 if(packet->data[start + 21 + i] < 2)
139 i += packet->data[start + 21 + i];
143 if(packet->data[start + 21] != 4)
147 uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
148 uint16_t newmss = mtu - start - 20;
149 uint16_t csum = packet->data[start + 16] << 8 | packet->data[start + 17];
154 ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
156 /* Update the MSS value and the checksum */
157 packet->data[start + 22 + i] = newmss >> 8;
158 packet->data[start + 23 + i] = newmss & 0xff;
163 packet->data[start + 16] = csum >> 8;
164 packet->data[start + 17] = csum & 0xff;
169 static void swap_mac_addresses(vpn_packet_t *packet) {
171 memcpy(&tmp, &packet->data[0], sizeof tmp);
172 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
173 memcpy(&packet->data[6], &tmp, sizeof tmp);
176 static void learn_mac(mac_t *address) {
181 subnet = lookup_subnet_mac(myself, address);
183 /* If we don't know this MAC address yet, store it */
186 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
187 address->x[0], address->x[1], address->x[2], address->x[3],
188 address->x[4], address->x[5]);
190 subnet = new_subnet();
191 subnet->type = SUBNET_MAC;
192 subnet->expires = now + macexpire;
193 subnet->net.mac.address = *address;
195 subnet_add(myself, subnet);
196 subnet_update(myself, subnet, true);
198 /* And tell all other tinc daemons it's our MAC */
200 for(node = connection_tree->head; node; node = node->next) {
203 send_add_subnet(c, subnet);
208 subnet->expires = now + macexpire;
211 void age_subnets(void) {
214 avl_node_t *node, *next, *node2;
216 for(node = myself->subnet_tree->head; node; node = next) {
219 if(s->expires && s->expires < now) {
221 char netstr[MAXNETSTR];
222 if(net2str(netstr, sizeof netstr, s))
223 logger(LOG_INFO, "Subnet %s expired", netstr);
226 for(node2 = connection_tree->head; node2; node2 = node2->next) {
229 send_del_subnet(c, s);
232 subnet_update(myself, s, false);
233 subnet_del(myself, s);
240 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
242 struct icmp icmp = {0};
244 struct in_addr ip_src;
245 struct in_addr ip_dst;
251 /* Swap Ethernet source and destination addresses */
253 swap_mac_addresses(packet);
255 /* Copy headers from packet into properly aligned structs on the stack */
257 memcpy(&ip, packet->data + ether_size, ip_size);
259 /* Remember original source and destination */
264 oldlen = packet->len - ether_size;
266 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
267 icmp.icmp_nextmtu = htons(packet->len - ether_size);
269 if(oldlen >= IP_MSS - ip_size - icmp_size)
270 oldlen = IP_MSS - ip_size - icmp_size;
272 /* Copy first part of original contents to ICMP message */
274 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
276 /* Fill in IPv4 header */
279 ip.ip_hl = ip_size / 4;
281 ip.ip_len = htons(ip_size + icmp_size + oldlen);
285 ip.ip_p = IPPROTO_ICMP;
290 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
292 /* Fill in ICMP header */
294 icmp.icmp_type = type;
295 icmp.icmp_code = code;
298 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
299 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
301 /* Copy structs on stack back to packet */
303 memcpy(packet->data + ether_size, &ip, ip_size);
304 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
306 packet->len = ether_size + ip_size + icmp_size + oldlen;
308 send_packet(source, packet);
313 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
315 vpn_packet_t fragment;
316 int len, maxlen, todo;
318 uint16_t ip_off, origf;
320 memcpy(&ip, packet->data + ether_size, ip_size);
321 fragment.priority = packet->priority;
323 if(ip.ip_hl != ip_size / 4)
326 todo = ntohs(ip.ip_len) - ip_size;
328 if(ether_size + ip_size + todo != packet->len) {
329 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%zd)", packet->len, ether_size + ip_size + todo);
333 ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
335 offset = packet->data + ether_size + ip_size;
336 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
337 ip_off = ntohs(ip.ip_off);
338 origf = ip_off & ~IP_OFFMASK;
339 ip_off &= IP_OFFMASK;
342 len = todo > maxlen ? maxlen : todo;
343 memcpy(fragment.data + ether_size + ip_size, offset, len);
347 ip.ip_len = htons(ip_size + len);
348 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
350 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
351 memcpy(fragment.data, packet->data, ether_size);
352 memcpy(fragment.data + ether_size, &ip, ip_size);
353 fragment.len = ether_size + ip_size + len;
355 send_packet(dest, &fragment);
361 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
366 memcpy(&dest, &packet->data[30], sizeof dest);
367 subnet = lookup_subnet_ipv4(&dest);
370 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
371 source->name, source->hostname,
377 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
381 if(subnet->owner == source) {
382 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
386 if(!subnet->owner->status.reachable)
387 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
389 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
390 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
392 if(priorityinheritance)
393 packet->priority = packet->data[15];
395 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
397 if(via && packet->len > max(via->mtu, 590) && via != myself) {
398 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);
399 if(packet->data[20] & 0x40) {
400 packet->len = max(via->mtu, 590);
401 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
403 fragment_ipv4_packet(via, packet);
409 clamp_mss(source, via, packet);
411 send_packet(subnet->owner, packet);
414 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
415 if(!checklength(source, packet, ether_size + ip_size))
418 if(((packet->data[30] & 0xf0) == 0xe0) || (
419 packet->data[30] == 255 &&
420 packet->data[31] == 255 &&
421 packet->data[32] == 255 &&
422 packet->data[33] == 255))
423 broadcast_packet(source, packet);
425 route_ipv4_unicast(source, packet);
430 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
432 struct icmp6_hdr icmp6 = {0};
436 struct in6_addr ip6_src; /* source address */
437 struct in6_addr ip6_dst; /* destination address */
445 /* Swap Ethernet source and destination addresses */
447 swap_mac_addresses(packet);
449 /* Copy headers from packet to structs on the stack */
451 memcpy(&ip6, packet->data + ether_size, ip6_size);
453 /* Remember original source and destination */
455 pseudo.ip6_src = ip6.ip6_dst;
456 pseudo.ip6_dst = ip6.ip6_src;
458 pseudo.length = packet->len - ether_size;
460 if(type == ICMP6_PACKET_TOO_BIG)
461 icmp6.icmp6_mtu = htonl(pseudo.length);
463 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
464 pseudo.length = IP_MSS - ip6_size - icmp6_size;
466 /* Copy first part of original contents to ICMP message */
468 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
470 /* Fill in IPv6 header */
472 ip6.ip6_flow = htonl(0x60000000UL);
473 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
474 ip6.ip6_nxt = IPPROTO_ICMPV6;
476 ip6.ip6_src = pseudo.ip6_src;
477 ip6.ip6_dst = pseudo.ip6_dst;
479 /* Fill in ICMP header */
481 icmp6.icmp6_type = type;
482 icmp6.icmp6_code = code;
483 icmp6.icmp6_cksum = 0;
485 /* Create pseudo header */
487 pseudo.length = htonl(icmp6_size + pseudo.length);
488 pseudo.next = htonl(IPPROTO_ICMPV6);
490 /* Generate checksum */
492 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
493 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
494 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
496 icmp6.icmp6_cksum = checksum;
498 /* Copy structs on stack back to packet */
500 memcpy(packet->data + ether_size, &ip6, ip6_size);
501 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
503 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
505 send_packet(source, packet);
508 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
513 memcpy(&dest, &packet->data[38], sizeof dest);
514 subnet = lookup_subnet_ipv6(&dest);
517 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
518 source->name, source->hostname,
528 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
532 if(subnet->owner == source) {
533 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
537 if(!subnet->owner->status.reachable)
538 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
540 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
541 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
543 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
545 if(via && packet->len > max(via->mtu, 1294) && via != myself) {
546 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);
547 packet->len = max(via->mtu, 1294);
548 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
552 clamp_mss(source, via, packet);
554 send_packet(subnet->owner, packet);
559 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
561 struct nd_neighbor_solicit ns;
562 struct nd_opt_hdr opt;
568 struct in6_addr ip6_src; /* source address */
569 struct in6_addr ip6_dst; /* destination address */
574 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
577 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
579 if(source != myself) {
580 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
584 /* Copy headers from packet to structs on the stack */
586 memcpy(&ip6, packet->data + ether_size, ip6_size);
587 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
589 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
591 /* First, snatch the source address from the neighbor solicitation packet */
594 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
596 /* Check if this is a valid neighbor solicitation request */
598 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
599 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
600 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
604 /* Create pseudo header */
606 pseudo.ip6_src = ip6.ip6_src;
607 pseudo.ip6_dst = ip6.ip6_dst;
609 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
611 pseudo.length = htonl(ns_size);
612 pseudo.next = htonl(IPPROTO_ICMPV6);
614 /* Generate checksum */
616 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
617 checksum = inet_checksum(&ns, ns_size, checksum);
619 checksum = inet_checksum(&opt, opt_size, checksum);
620 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
624 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
628 /* Check if the IPv6 address exists on the VPN */
630 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
633 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
634 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
635 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
636 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
637 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
638 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
639 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
640 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
641 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
646 /* Check if it is for our own subnet */
648 if(subnet->owner == myself)
649 return; /* silently ignore */
651 /* Create neighbor advertation reply */
653 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
654 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
656 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
657 ip6.ip6_src = ns.nd_ns_target;
660 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
663 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
664 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
665 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
667 /* Create pseudo header */
669 pseudo.ip6_src = ip6.ip6_src;
670 pseudo.ip6_dst = ip6.ip6_dst;
672 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
674 pseudo.length = htonl(ns_size);
675 pseudo.next = htonl(IPPROTO_ICMPV6);
677 /* Generate checksum */
679 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
680 checksum = inet_checksum(&ns, ns_size, checksum);
682 checksum = inet_checksum(&opt, opt_size, checksum);
683 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
686 ns.nd_ns_hdr.icmp6_cksum = checksum;
688 /* Copy structs on stack back to packet */
690 memcpy(packet->data + ether_size, &ip6, ip6_size);
691 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
693 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
695 send_packet(source, packet);
698 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
699 if(!checklength(source, packet, ether_size + ip6_size))
702 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
703 route_neighborsol(source, packet);
707 if(packet->data[38] == 255)
708 broadcast_packet(source, packet);
710 route_ipv6_unicast(source, packet);
715 static void route_arp(node_t *source, vpn_packet_t *packet) {
716 struct ether_arp arp;
720 if(!checklength(source, packet, ether_size + arp_size))
723 if(source != myself) {
724 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
728 /* First, snatch the source address from the ARP packet */
731 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
733 /* Copy headers from packet to structs on the stack */
735 memcpy(&arp, packet->data + ether_size, arp_size);
737 /* Check if this is a valid ARP request */
739 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
740 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
741 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
745 /* Check if the IPv4 address exists on the VPN */
747 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
750 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
751 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
756 /* Check if it is for our own subnet */
758 if(subnet->owner == myself)
759 return; /* silently ignore */
761 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
762 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
764 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
765 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
766 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
768 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
769 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
770 arp.arp_op = htons(ARPOP_REPLY);
772 /* Copy structs on stack back to packet */
774 memcpy(packet->data + ether_size, &arp, arp_size);
776 send_packet(source, packet);
779 static void route_mac(node_t *source, vpn_packet_t *packet) {
783 /* Learn source address */
785 if(source == myself) {
787 memcpy(&src, &packet->data[6], sizeof src);
791 /* Lookup destination address */
793 memcpy(&dest, &packet->data[0], sizeof dest);
794 subnet = lookup_subnet_mac(NULL, &dest);
797 broadcast_packet(source, packet);
801 if(subnet->owner == source) {
802 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
806 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
809 // Handle packets larger than PMTU
811 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
813 if(via && packet->len > via->mtu && via != myself) {
814 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);
815 uint16_t type = packet->data[12] << 8 | packet->data[13];
816 if(type == ETH_P_IP && packet->len > 590) {
817 if(packet->data[20] & 0x40) {
818 packet->len = via->mtu;
819 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
821 fragment_ipv4_packet(via, packet);
824 } else if(type == ETH_P_IPV6 && packet->len > 1294) {
825 packet->len = via->mtu;
826 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
831 clamp_mss(source, via, packet);
833 send_packet(subnet->owner, packet);
836 void route(node_t *source, vpn_packet_t *packet) {
837 if(forwarding_mode == FMODE_KERNEL) {
838 send_packet(myself, packet);
842 if(!checklength(source, packet, ether_size))
845 switch (routing_mode) {
848 uint16_t type = packet->data[12] << 8 | packet->data[13];
852 route_arp(source, packet);
856 route_ipv4(source, packet);
860 route_ipv6(source, packet);
864 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
871 route_mac(source, packet);
875 broadcast_packet(source, packet);