3 Copyright (C) 2000-2005 Ivo Timmermans,
4 2000-2013 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 "connection.h"
24 #include "control_common.h"
36 rmode_t routing_mode = RMODE_ROUTER;
37 fmode_t forwarding_mode = FMODE_INTERNAL;
38 bmode_t broadcast_mode = BMODE_MST;
39 bool decrement_ttl = false;
40 bool directonly = false;
41 bool priorityinheritance = false;
43 bool overwrite_mac = false;
44 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
47 /* Sizes of various headers */
49 static const size_t ether_size = sizeof(struct ether_header);
50 static const size_t arp_size = sizeof(struct ether_arp);
51 static const size_t ip_size = sizeof(struct ip);
52 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
53 static const size_t ip6_size = sizeof(struct ip6_hdr);
54 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
55 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
56 static const size_t opt_size = sizeof(struct nd_opt_hdr);
59 #define MAX(a, b) ((a) > (b) ? (a) : (b))
62 static timeout_t age_subnets_timeout;
66 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
68 uint32_t checksum = prevsum ^ 0xFFFF;
76 checksum += *(uint8_t *)p;
79 checksum = (checksum & 0xFFFF) + (checksum >> 16);
84 static bool ratelimit(int frequency) {
85 static time_t lasttime = 0;
88 if(lasttime == now.tv_sec) {
89 if(count >= frequency)
92 lasttime = now.tv_sec;
100 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
101 if(packet->len < length) {
102 logger(DEBUG_TRAFFIC, LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
108 static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
109 if(!source || !via || !(via->options & OPTION_CLAMP_MSS))
112 uint16_t mtu = source->mtu;
113 if(via != myself && via->mtu < mtu)
116 /* Find TCP header */
117 int start = ether_size;
118 uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
120 if(type == ETH_P_8021Q) {
122 type = DATA(packet)[16] << 8 | DATA(packet)[17];
125 if(type == ETH_P_IP && DATA(packet)[start + 9] == 6)
126 start += (DATA(packet)[start] & 0xf) * 4;
127 else if(type == ETH_P_IPV6 && DATA(packet)[start + 6] == 6)
132 if(packet->len <= start + 20)
135 /* Use data offset field to calculate length of options field */
136 int len = ((DATA(packet)[start + 12] >> 4) - 5) * 4;
138 if(packet->len < start + 20 + len)
141 /* Search for MSS option header */
142 for(int i = 0; i < len;) {
143 if(DATA(packet)[start + 20 + i] == 0)
146 if(DATA(packet)[start + 20 + i] == 1) {
151 if(i > len - 2 || i > len - DATA(packet)[start + 21 + i])
154 if(DATA(packet)[start + 20 + i] != 2) {
155 if(DATA(packet)[start + 21 + i] < 2)
157 i += DATA(packet)[start + 21 + i];
161 if(DATA(packet)[start + 21] != 4)
165 uint16_t oldmss = DATA(packet)[start + 22 + i] << 8 | DATA(packet)[start + 23 + i];
166 uint16_t newmss = mtu - start - 20;
167 uint32_t csum = DATA(packet)[start + 16] << 8 | DATA(packet)[start + 17];
172 logger(DEBUG_TRAFFIC, LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
174 /* Update the MSS value and the checksum */
175 DATA(packet)[start + 22 + i] = newmss >> 8;
176 DATA(packet)[start + 23 + i] = newmss & 0xff;
178 csum += oldmss ^ 0xffff;
180 csum = (csum & 0xffff) + (csum >> 16);
183 DATA(packet)[start + 16] = csum >> 8;
184 DATA(packet)[start + 17] = csum;
189 static void swap_mac_addresses(vpn_packet_t *packet) {
191 memcpy(&tmp, &DATA(packet)[0], sizeof tmp);
192 memcpy(&DATA(packet)[0], &DATA(packet)[6], sizeof tmp);
193 memcpy(&DATA(packet)[6], &tmp, sizeof tmp);
196 static void age_subnets(void *data) {
199 for splay_each(subnet_t, s, myself->subnet_tree) {
200 if(s->expires && s->expires < now.tv_sec) {
201 if(debug_level >= DEBUG_TRAFFIC) {
202 char netstr[MAXNETSTR];
203 if(net2str(netstr, sizeof netstr, s))
204 logger(DEBUG_TRAFFIC, LOG_INFO, "Subnet %s expired", netstr);
207 for list_each(connection_t, c, connection_list)
209 send_del_subnet(c, s);
211 subnet_del(myself, s);
219 timeout_set(&age_subnets_timeout, &(struct timeval){10, rand() % 100000});
222 static void learn_mac(mac_t *address) {
223 subnet_t *subnet = lookup_subnet_mac(myself, address);
225 /* If we don't know this MAC address yet, store it */
228 logger(DEBUG_TRAFFIC, LOG_INFO, "Learned new MAC address %x:%x:%x:%x:%x:%x",
229 address->x[0], address->x[1], address->x[2], address->x[3],
230 address->x[4], address->x[5]);
232 subnet = new_subnet();
233 subnet->type = SUBNET_MAC;
234 subnet->expires = now.tv_sec + macexpire;
235 subnet->net.mac.address = *address;
237 subnet_add(myself, subnet);
238 subnet_update(myself, subnet, true);
240 /* And tell all other tinc daemons it's our MAC */
242 for list_each(connection_t, c, connection_list)
244 send_add_subnet(c, subnet);
246 timeout_add(&age_subnets_timeout, age_subnets, NULL, &(struct timeval){10, rand() % 100000});
249 subnet->expires = now.tv_sec + macexpire;
255 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, 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, DATA(packet) + 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(DATA(packet) + ether_size + ip_size + icmp_size, DATA(packet) + 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(DATA(packet) + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
316 /* Copy structs on stack back to packet */
318 memcpy(DATA(packet) + ether_size, &ip, ip_size);
319 memcpy(DATA(packet) + 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, length_t ether_size) {
330 vpn_packet_t fragment;
331 int len, maxlen, todo;
333 uint16_t ip_off, origf;
335 memcpy(&ip, DATA(packet) + ether_size, ip_size);
336 fragment.priority = packet->priority;
337 fragment.offset = DEFAULT_PACKET_OFFSET;
339 if(ip.ip_hl != ip_size / 4)
342 todo = ntohs(ip.ip_len) - ip_size;
344 if(ether_size + ip_size + todo != packet->len) {
345 logger(DEBUG_TRAFFIC, LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%d)", packet->len, (int)(ether_size + ip_size + todo));
349 logger(DEBUG_TRAFFIC, LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
351 offset = DATA(packet) + ether_size + ip_size;
352 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
353 ip_off = ntohs(ip.ip_off);
354 origf = ip_off & ~IP_OFFMASK;
355 ip_off &= IP_OFFMASK;
358 len = todo > maxlen ? maxlen : todo;
359 memcpy(DATA(&fragment) + ether_size + ip_size, offset, len);
363 ip.ip_len = htons(ip_size + len);
364 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
366 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
367 memcpy(DATA(&fragment), DATA(packet), ether_size);
368 memcpy(DATA(&fragment) + ether_size, &ip, ip_size);
369 fragment.len = ether_size + ip_size + len;
371 send_packet(dest, &fragment);
377 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
378 if(!checklength(source, packet, ether_size + ip_size))
385 memcpy(&dest, &DATA(packet)[30], sizeof dest);
386 subnet = lookup_subnet_ipv4(&dest);
389 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
390 source->name, source->hostname,
396 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
400 if (!subnet->owner) {
401 broadcast_packet(source, packet);
405 if(subnet->owner == source) {
406 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
410 if(!subnet->owner->status.reachable)
411 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
413 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
414 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
416 if(priorityinheritance)
417 packet->priority = DATA(packet)[15];
419 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
422 logger(DEBUG_TRAFFIC, LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
426 if(directonly && subnet->owner != via)
427 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
429 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
430 logger(DEBUG_TRAFFIC, LOG_INFO, "Packet for %s (%s) length %d larger than MTU %d", subnet->owner->name, subnet->owner->hostname, packet->len, via->mtu);
431 if(DATA(packet)[20] & 0x40) {
432 packet->len = MAX(via->mtu, 590);
433 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
435 fragment_ipv4_packet(via, packet, ether_size);
441 clamp_mss(source, via, packet);
443 send_packet(subnet->owner, packet);
448 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, 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, DATA(packet) + 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(DATA(packet) + ether_size + ip6_size + icmp6_size, DATA(packet) + 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(DATA(packet) + 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(DATA(packet) + ether_size, &ip6, ip6_size);
519 memcpy(DATA(packet) + 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_neighborsol(node_t *source, vpn_packet_t *packet);
528 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
529 if(!checklength(source, packet, ether_size + ip6_size))
532 if(DATA(packet)[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && DATA(packet)[54] == ND_NEIGHBOR_SOLICIT) {
533 route_neighborsol(source, packet);
541 memcpy(&dest, &DATA(packet)[38], sizeof dest);
542 subnet = lookup_subnet_ipv6(&dest);
545 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
546 source->name, source->hostname,
556 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
560 if (!subnet->owner) {
561 broadcast_packet(source, packet);
565 if(subnet->owner == source) {
566 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
570 if(!subnet->owner->status.reachable)
571 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
573 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
574 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
576 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
579 logger(DEBUG_TRAFFIC, LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
583 if(directonly && subnet->owner != via)
584 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
586 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
587 logger(DEBUG_TRAFFIC, LOG_INFO, "Packet for %s (%s) length %d larger than MTU %d", subnet->owner->name, subnet->owner->hostname, packet->len, via->mtu);
588 packet->len = MAX(via->mtu, 1294);
589 route_ipv6_unreachable(source, packet, ether_size, ICMP6_PACKET_TOO_BIG, 0);
593 clamp_mss(source, via, packet);
595 send_packet(subnet->owner, packet);
600 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
602 struct nd_neighbor_solicit ns;
603 struct nd_opt_hdr opt;
609 struct in6_addr ip6_src;
610 struct in6_addr ip6_dst;
615 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
618 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
620 if(source != myself) {
621 logger(DEBUG_TRAFFIC, LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
625 /* Copy headers from packet to structs on the stack */
627 memcpy(&ip6, DATA(packet) + ether_size, ip6_size);
628 memcpy(&ns, DATA(packet) + ether_size + ip6_size, ns_size);
630 memcpy(&opt, DATA(packet) + ether_size + ip6_size + ns_size, opt_size);
632 /* First, snatch the source address from the neighbor solicitation packet */
635 memcpy(mymac.x, DATA(packet) + ETH_ALEN, ETH_ALEN);
637 /* Check if this is a valid neighbor solicitation request */
639 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
640 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
641 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
645 /* Create pseudo header */
647 pseudo.ip6_src = ip6.ip6_src;
648 pseudo.ip6_dst = ip6.ip6_dst;
650 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
652 pseudo.length = htonl(ns_size);
653 pseudo.next = htonl(IPPROTO_ICMPV6);
655 /* Generate checksum */
657 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
658 checksum = inet_checksum(&ns, ns_size, checksum);
660 checksum = inet_checksum(&opt, opt_size, checksum);
661 checksum = inet_checksum(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
665 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
669 /* Check if the IPv6 address exists on the VPN */
671 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
674 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
675 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
676 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
677 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
678 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
679 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
680 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
681 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
682 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
687 /* Check if it is for our own subnet */
689 if(subnet->owner == myself)
690 return; /* silently ignore */
692 /* Create neighbor advertation reply */
694 memcpy(DATA(packet), DATA(packet) + ETH_ALEN, ETH_ALEN); /* copy destination address */
695 DATA(packet)[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
697 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
698 ip6.ip6_src = ns.nd_ns_target;
701 memcpy(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, DATA(packet) + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
704 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
705 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
706 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
708 /* Create pseudo header */
710 pseudo.ip6_src = ip6.ip6_src;
711 pseudo.ip6_dst = ip6.ip6_dst;
713 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
715 pseudo.length = htonl(ns_size);
716 pseudo.next = htonl(IPPROTO_ICMPV6);
718 /* Generate checksum */
720 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
721 checksum = inet_checksum(&ns, ns_size, checksum);
723 checksum = inet_checksum(&opt, opt_size, checksum);
724 checksum = inet_checksum(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
727 ns.nd_ns_hdr.icmp6_cksum = checksum;
729 /* Copy structs on stack back to packet */
731 memcpy(DATA(packet) + ether_size, &ip6, ip6_size);
732 memcpy(DATA(packet) + ether_size + ip6_size, &ns, ns_size);
734 memcpy(DATA(packet) + ether_size + ip6_size + ns_size, &opt, opt_size);
736 send_packet(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 logger(DEBUG_TRAFFIC, 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, DATA(packet) + ETH_ALEN, ETH_ALEN);
759 /* Copy headers from packet to structs on the stack */
761 memcpy(&arp, DATA(packet) + 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 logger(DEBUG_TRAFFIC, 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 logger(DEBUG_TRAFFIC, 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(DATA(packet), DATA(packet) + ETH_ALEN, ETH_ALEN); /* copy destination address */
788 DATA(packet)[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, DATA(packet) + 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(DATA(packet) + 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, &DATA(packet)[6], sizeof src);
817 /* Lookup destination address */
819 memcpy(&dest, &DATA(packet)[0], sizeof dest);
820 subnet = lookup_subnet_mac(NULL, &dest);
822 if(!subnet || !subnet->owner) {
823 broadcast_packet(source, packet);
827 if(subnet->owner == source) {
828 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
832 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
835 uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
837 if(priorityinheritance && type == ETH_P_IP && packet->len >= ether_size + ip_size)
838 packet->priority = DATA(packet)[15];
840 // Handle packets larger than PMTU
842 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
844 if(directonly && subnet->owner != via)
847 if(via && packet->len > via->mtu && via != myself) {
848 logger(DEBUG_TRAFFIC, LOG_INFO, "Packet for %s (%s) length %d larger than MTU %d", subnet->owner->name, subnet->owner->hostname, packet->len, via->mtu);
849 length_t ethlen = 14;
851 if(type == ETH_P_8021Q) {
852 type = DATA(packet)[16] << 8 | DATA(packet)[17];
856 if(type == ETH_P_IP && packet->len > 576 + ethlen) {
857 if(DATA(packet)[6 + ethlen] & 0x40) {
858 packet->len = via->mtu;
859 route_ipv4_unreachable(source, packet, ethlen, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
861 fragment_ipv4_packet(via, packet, ethlen);
864 } else if(type == ETH_P_IPV6 && packet->len > 1280 + ethlen) {
865 packet->len = via->mtu;
866 route_ipv6_unreachable(source, packet, ethlen, ICMP6_PACKET_TOO_BIG, 0);
871 clamp_mss(source, via, packet);
873 send_packet(subnet->owner, packet);
876 static void send_pcap(vpn_packet_t *packet) {
879 for list_each(connection_t, c, connection_list) {
884 int len = packet->len;
885 if(c->outmaclength && c->outmaclength < len)
886 len = c->outmaclength;
888 if(send_request(c, "%d %d %d", CONTROL, REQ_PCAP, len))
889 send_meta(c, (char *)DATA(packet), len);
893 static bool do_decrement_ttl(node_t *source, vpn_packet_t *packet) {
894 uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
895 length_t ethlen = ether_size;
897 if(type == ETH_P_8021Q) {
898 type = DATA(packet)[16] << 8 | DATA(packet)[17];
904 if(!checklength(source, packet, ethlen + ip_size))
907 if(DATA(packet)[ethlen + 8] < 1) {
908 if(DATA(packet)[ethlen + 11] != IPPROTO_ICMP || DATA(packet)[ethlen + 32] != ICMP_TIME_EXCEEDED)
909 route_ipv4_unreachable(source, packet, ethlen, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL);
913 uint16_t old = DATA(packet)[ethlen + 8] << 8 | DATA(packet)[ethlen + 9];
914 DATA(packet)[ethlen + 8]--;
915 uint16_t new = DATA(packet)[ethlen + 8] << 8 | DATA(packet)[ethlen + 9];
917 uint32_t checksum = DATA(packet)[ethlen + 10] << 8 | DATA(packet)[ethlen + 11];
918 checksum += old + (~new & 0xFFFF);
919 while(checksum >> 16)
920 checksum = (checksum & 0xFFFF) + (checksum >> 16);
921 DATA(packet)[ethlen + 10] = checksum >> 8;
922 DATA(packet)[ethlen + 11] = checksum & 0xff;
927 if(!checklength(source, packet, ethlen + ip6_size))
930 if(DATA(packet)[ethlen + 7] < 1) {
931 if(DATA(packet)[ethlen + 6] != IPPROTO_ICMPV6 || DATA(packet)[ethlen + 40] != ICMP6_TIME_EXCEEDED)
932 route_ipv6_unreachable(source, packet, ethlen, ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT);
936 DATA(packet)[ethlen + 7]--;
945 void route(node_t *source, vpn_packet_t *packet) {
949 if(forwarding_mode == FMODE_KERNEL && source != myself) {
950 send_packet(myself, packet);
954 if(!checklength(source, packet, ether_size))
957 if(decrement_ttl && source != myself)
958 if(!do_decrement_ttl(source, packet))
961 uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
963 switch (routing_mode) {
967 route_arp(source, packet);
971 route_ipv4(source, packet);
975 route_ipv6(source, packet);
979 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
985 route_mac(source, packet);
989 broadcast_packet(source, packet);