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 = packet->data[12] << 8 | packet->data[13];
120 if(type == ETH_P_8021Q) {
122 type = packet->data[16] << 8 | packet->data[17];
125 if(type == ETH_P_IP && packet->data[start + 9] == 6)
126 start += (packet->data[start] & 0xf) * 4;
127 else if(type == ETH_P_IPV6 && packet->data[start + 6] == 6)
132 if(packet->len <= start + 20)
135 /* Use data offset field to calculate length of options field */
136 int len = ((packet->data[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(packet->data[start + 20 + i] == 0)
146 if(packet->data[start + 20 + i] == 1) {
151 if(i > len - 2 || i > len - packet->data[start + 21 + i])
154 if(packet->data[start + 20 + i] != 2) {
155 if(packet->data[start + 21 + i] < 2)
157 i += packet->data[start + 21 + i];
161 if(packet->data[start + 21] != 4)
165 uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
166 uint16_t newmss = mtu - start - 20;
167 uint16_t csum = packet->data[start + 16] << 8 | packet->data[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 packet->data[start + 22 + i] = newmss >> 8;
176 packet->data[start + 23 + i] = newmss & 0xff;
181 packet->data[start + 16] = csum >> 8;
182 packet->data[start + 17] = csum & 0xff;
187 static void swap_mac_addresses(vpn_packet_t *packet) {
189 memcpy(&tmp, &packet->data[0], sizeof tmp);
190 memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
191 memcpy(&packet->data[6], &tmp, sizeof tmp);
194 static void age_subnets(void *data) {
197 for splay_each(subnet_t, s, myself->subnet_tree) {
198 if(s->expires && s->expires < now.tv_sec) {
199 if(debug_level >= DEBUG_TRAFFIC) {
200 char netstr[MAXNETSTR];
201 if(net2str(netstr, sizeof netstr, s))
202 logger(DEBUG_TRAFFIC, LOG_INFO, "Subnet %s expired", netstr);
205 for list_each(connection_t, c, connection_list)
207 send_del_subnet(c, s);
209 subnet_del(myself, s);
217 timeout_set(&age_subnets_timeout, &(struct timeval){10, rand() % 100000});
220 static void learn_mac(mac_t *address) {
221 subnet_t *subnet = lookup_subnet_mac(myself, address);
223 /* If we don't know this MAC address yet, store it */
226 logger(DEBUG_TRAFFIC, LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
227 address->x[0], address->x[1], address->x[2], address->x[3],
228 address->x[4], address->x[5]);
230 subnet = new_subnet();
231 subnet->type = SUBNET_MAC;
232 subnet->expires = now.tv_sec + macexpire;
233 subnet->net.mac.address = *address;
235 subnet_add(myself, subnet);
236 subnet_update(myself, subnet, true);
238 /* And tell all other tinc daemons it's our MAC */
240 for list_each(connection_t, c, connection_list)
242 send_add_subnet(c, subnet);
244 timeout_add(&age_subnets_timeout, age_subnets, NULL, &(struct timeval){10, rand() % 100000});
247 subnet->expires = now.tv_sec + macexpire;
253 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
255 struct icmp icmp = {0};
257 struct in_addr ip_src;
258 struct in_addr ip_dst;
264 /* Swap Ethernet source and destination addresses */
266 swap_mac_addresses(packet);
268 /* Copy headers from packet into properly aligned structs on the stack */
270 memcpy(&ip, packet->data + ether_size, ip_size);
272 /* Remember original source and destination */
277 oldlen = packet->len - ether_size;
279 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
280 icmp.icmp_nextmtu = htons(packet->len - ether_size);
282 if(oldlen >= IP_MSS - ip_size - icmp_size)
283 oldlen = IP_MSS - ip_size - icmp_size;
285 /* Copy first part of original contents to ICMP message */
287 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
289 /* Fill in IPv4 header */
292 ip.ip_hl = ip_size / 4;
294 ip.ip_len = htons(ip_size + icmp_size + oldlen);
298 ip.ip_p = IPPROTO_ICMP;
303 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
305 /* Fill in ICMP header */
307 icmp.icmp_type = type;
308 icmp.icmp_code = code;
311 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
312 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
314 /* Copy structs on stack back to packet */
316 memcpy(packet->data + ether_size, &ip, ip_size);
317 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
319 packet->len = ether_size + ip_size + icmp_size + oldlen;
321 send_packet(source, packet);
326 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t ether_size) {
328 vpn_packet_t fragment;
329 int len, maxlen, todo;
331 uint16_t ip_off, origf;
333 memcpy(&ip, packet->data + ether_size, ip_size);
334 fragment.priority = packet->priority;
336 if(ip.ip_hl != ip_size / 4)
339 todo = ntohs(ip.ip_len) - ip_size;
341 if(ether_size + ip_size + todo != packet->len) {
342 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));
346 logger(DEBUG_TRAFFIC, LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
348 offset = packet->data + ether_size + ip_size;
349 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
350 ip_off = ntohs(ip.ip_off);
351 origf = ip_off & ~IP_OFFMASK;
352 ip_off &= IP_OFFMASK;
355 len = todo > maxlen ? maxlen : todo;
356 memcpy(fragment.data + ether_size + ip_size, offset, len);
360 ip.ip_len = htons(ip_size + len);
361 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
363 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
364 memcpy(fragment.data, packet->data, ether_size);
365 memcpy(fragment.data + ether_size, &ip, ip_size);
366 fragment.len = ether_size + ip_size + len;
368 send_packet(dest, &fragment);
374 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
379 memcpy(&dest, &packet->data[30], sizeof dest);
380 subnet = lookup_subnet_ipv4(&dest);
383 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
384 source->name, source->hostname,
390 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
394 if(subnet->owner == source) {
395 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
399 if(!subnet->owner->status.reachable)
400 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
402 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
403 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
405 if(priorityinheritance)
406 packet->priority = packet->data[15];
408 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
411 logger(DEBUG_TRAFFIC, LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
415 if(directonly && subnet->owner != via)
416 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
418 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
419 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);
420 if(packet->data[20] & 0x40) {
421 packet->len = MAX(via->mtu, 590);
422 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
424 fragment_ipv4_packet(via, packet, ether_size);
430 clamp_mss(source, via, packet);
432 send_packet(subnet->owner, packet);
435 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
436 if(!checklength(source, packet, ether_size + ip_size))
439 if(broadcast_mode && (((packet->data[30] & 0xf0) == 0xe0) || (
440 packet->data[30] == 255 &&
441 packet->data[31] == 255 &&
442 packet->data[32] == 255 &&
443 packet->data[33] == 255)))
444 broadcast_packet(source, packet);
446 route_ipv4_unicast(source, packet);
451 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
453 struct icmp6_hdr icmp6 = {0};
457 struct in6_addr ip6_src; /* source address */
458 struct in6_addr ip6_dst; /* destination address */
466 /* Swap Ethernet source and destination addresses */
468 swap_mac_addresses(packet);
470 /* Copy headers from packet to structs on the stack */
472 memcpy(&ip6, packet->data + ether_size, ip6_size);
474 /* Remember original source and destination */
476 pseudo.ip6_src = ip6.ip6_dst;
477 pseudo.ip6_dst = ip6.ip6_src;
479 pseudo.length = packet->len - ether_size;
481 if(type == ICMP6_PACKET_TOO_BIG)
482 icmp6.icmp6_mtu = htonl(pseudo.length);
484 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
485 pseudo.length = IP_MSS - ip6_size - icmp6_size;
487 /* Copy first part of original contents to ICMP message */
489 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
491 /* Fill in IPv6 header */
493 ip6.ip6_flow = htonl(0x60000000UL);
494 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
495 ip6.ip6_nxt = IPPROTO_ICMPV6;
497 ip6.ip6_src = pseudo.ip6_src;
498 ip6.ip6_dst = pseudo.ip6_dst;
500 /* Fill in ICMP header */
502 icmp6.icmp6_type = type;
503 icmp6.icmp6_code = code;
504 icmp6.icmp6_cksum = 0;
506 /* Create pseudo header */
508 pseudo.length = htonl(icmp6_size + pseudo.length);
509 pseudo.next = htonl(IPPROTO_ICMPV6);
511 /* Generate checksum */
513 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
514 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
515 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
517 icmp6.icmp6_cksum = checksum;
519 /* Copy structs on stack back to packet */
521 memcpy(packet->data + ether_size, &ip6, ip6_size);
522 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
524 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
526 send_packet(source, packet);
529 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
534 memcpy(&dest, &packet->data[38], sizeof dest);
535 subnet = lookup_subnet_ipv6(&dest);
538 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
539 source->name, source->hostname,
549 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
553 if(subnet->owner == source) {
554 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
558 if(!subnet->owner->status.reachable)
559 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
561 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
562 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
564 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
567 logger(DEBUG_TRAFFIC, LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
571 if(directonly && subnet->owner != via)
572 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
574 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
575 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);
576 packet->len = MAX(via->mtu, 1294);
577 route_ipv6_unreachable(source, packet, ether_size, ICMP6_PACKET_TOO_BIG, 0);
581 clamp_mss(source, via, packet);
583 send_packet(subnet->owner, packet);
588 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
590 struct nd_neighbor_solicit ns;
591 struct nd_opt_hdr opt;
597 struct in6_addr ip6_src;
598 struct in6_addr ip6_dst;
603 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
606 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
608 if(source != myself) {
609 logger(DEBUG_TRAFFIC, LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
613 /* Copy headers from packet to structs on the stack */
615 memcpy(&ip6, packet->data + ether_size, ip6_size);
616 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
618 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
620 /* First, snatch the source address from the neighbor solicitation packet */
623 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
625 /* Check if this is a valid neighbor solicitation request */
627 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
628 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
629 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
633 /* Create pseudo header */
635 pseudo.ip6_src = ip6.ip6_src;
636 pseudo.ip6_dst = ip6.ip6_dst;
638 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
640 pseudo.length = htonl(ns_size);
641 pseudo.next = htonl(IPPROTO_ICMPV6);
643 /* Generate checksum */
645 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
646 checksum = inet_checksum(&ns, ns_size, checksum);
648 checksum = inet_checksum(&opt, opt_size, checksum);
649 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
653 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
657 /* Check if the IPv6 address exists on the VPN */
659 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
662 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
663 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
664 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
665 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
666 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
667 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
668 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
669 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
670 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
675 /* Check if it is for our own subnet */
677 if(subnet->owner == myself)
678 return; /* silently ignore */
680 /* Create neighbor advertation reply */
682 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
683 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
685 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
686 ip6.ip6_src = ns.nd_ns_target;
689 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
692 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
693 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
694 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
696 /* Create pseudo header */
698 pseudo.ip6_src = ip6.ip6_src;
699 pseudo.ip6_dst = ip6.ip6_dst;
701 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
703 pseudo.length = htonl(ns_size);
704 pseudo.next = htonl(IPPROTO_ICMPV6);
706 /* Generate checksum */
708 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
709 checksum = inet_checksum(&ns, ns_size, checksum);
711 checksum = inet_checksum(&opt, opt_size, checksum);
712 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
715 ns.nd_ns_hdr.icmp6_cksum = checksum;
717 /* Copy structs on stack back to packet */
719 memcpy(packet->data + ether_size, &ip6, ip6_size);
720 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
722 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
724 send_packet(source, packet);
727 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
728 if(!checklength(source, packet, ether_size + ip6_size))
731 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
732 route_neighborsol(source, packet);
736 if(broadcast_mode && packet->data[38] == 255)
737 broadcast_packet(source, packet);
739 route_ipv6_unicast(source, packet);
744 static void route_arp(node_t *source, vpn_packet_t *packet) {
745 struct ether_arp arp;
749 if(!checklength(source, packet, ether_size + arp_size))
752 if(source != myself) {
753 logger(DEBUG_TRAFFIC, LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
757 /* First, snatch the source address from the ARP packet */
760 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
762 /* Copy headers from packet to structs on the stack */
764 memcpy(&arp, packet->data + ether_size, arp_size);
766 /* Check if this is a valid ARP request */
768 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
769 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof addr || ntohs(arp.arp_op) != ARPOP_REQUEST) {
770 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: received unknown type ARP request");
774 /* Check if the IPv4 address exists on the VPN */
776 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
779 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
780 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
785 /* Check if it is for our own subnet */
787 if(subnet->owner == myself)
788 return; /* silently ignore */
790 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
791 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
793 memcpy(&addr, arp.arp_tpa, sizeof addr); /* save protocol addr */
794 memcpy(arp.arp_tpa, arp.arp_spa, sizeof addr); /* swap destination and source protocol address */
795 memcpy(arp.arp_spa, &addr, sizeof addr); /* ... */
797 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
798 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
799 arp.arp_op = htons(ARPOP_REPLY);
801 /* Copy structs on stack back to packet */
803 memcpy(packet->data + ether_size, &arp, arp_size);
805 send_packet(source, packet);
808 static void route_mac(node_t *source, vpn_packet_t *packet) {
812 /* Learn source address */
814 if(source == myself) {
816 memcpy(&src, &packet->data[6], sizeof src);
820 /* Lookup destination address */
822 memcpy(&dest, &packet->data[0], sizeof dest);
823 subnet = lookup_subnet_mac(NULL, &dest);
826 broadcast_packet(source, packet);
830 if(subnet->owner == source) {
831 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
835 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
838 uint16_t type = packet->data[12] << 8 | packet->data[13];
840 if(priorityinheritance && type == ETH_P_IP && packet->len >= ether_size + ip_size)
841 packet->priority = packet->data[15];
843 // Handle packets larger than PMTU
845 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
847 if(directonly && subnet->owner != via)
850 if(via && packet->len > via->mtu && via != myself) {
851 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);
852 length_t ethlen = 14;
854 if(type == ETH_P_8021Q) {
855 type = packet->data[16] << 8 | packet->data[17];
859 if(type == ETH_P_IP && packet->len > 576 + ethlen) {
860 if(packet->data[6 + ethlen] & 0x40) {
861 packet->len = via->mtu;
862 route_ipv4_unreachable(source, packet, ethlen, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
864 fragment_ipv4_packet(via, packet, ethlen);
867 } else if(type == ETH_P_IPV6 && packet->len > 1280 + ethlen) {
868 packet->len = via->mtu;
869 route_ipv6_unreachable(source, packet, ethlen, ICMP6_PACKET_TOO_BIG, 0);
874 clamp_mss(source, via, packet);
876 send_packet(subnet->owner, packet);
879 static void send_pcap(vpn_packet_t *packet) {
882 for list_each(connection_t, c, connection_list) {
887 int len = packet->len;
888 if(c->outmaclength && c->outmaclength < len)
889 len = c->outmaclength;
891 if(send_request(c, "%d %d %d", CONTROL, REQ_PCAP, len))
892 send_meta(c, (char *)packet->data, len);
896 static bool do_decrement_ttl(node_t *source, vpn_packet_t *packet) {
897 uint16_t type = packet->data[12] << 8 | packet->data[13];
898 length_t ethlen = ether_size;
900 if(type == ETH_P_8021Q) {
901 type = packet->data[16] << 8 | packet->data[17];
907 if(!checklength(source, packet, ethlen + ip_size))
910 if(packet->data[ethlen + 8] < 1) {
911 if(packet->data[ethlen + 11] != IPPROTO_ICMP || packet->data[ethlen + 32] != ICMP_TIME_EXCEEDED)
912 route_ipv4_unreachable(source, packet, ethlen, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL);
916 uint16_t old = packet->data[ethlen + 8] << 8 | packet->data[ethlen + 9];
917 packet->data[ethlen + 8]--;
918 uint16_t new = packet->data[ethlen + 8] << 8 | packet->data[ethlen + 9];
920 uint32_t checksum = packet->data[ethlen + 10] << 8 | packet->data[ethlen + 11];
921 checksum += old + (~new & 0xFFFF);
922 while(checksum >> 16)
923 checksum = (checksum & 0xFFFF) + (checksum >> 16);
924 packet->data[ethlen + 10] = checksum >> 8;
925 packet->data[ethlen + 11] = checksum & 0xff;
930 if(!checklength(source, packet, ethlen + ip6_size))
933 if(packet->data[ethlen + 7] < 1) {
934 if(packet->data[ethlen + 6] != IPPROTO_ICMPV6 || packet->data[ethlen + 40] != ICMP6_TIME_EXCEEDED)
935 route_ipv6_unreachable(source, packet, ethlen, ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT);
939 packet->data[ethlen + 7]--;
948 void route(node_t *source, vpn_packet_t *packet) {
952 if(forwarding_mode == FMODE_KERNEL && source != myself) {
953 send_packet(myself, packet);
957 if(!checklength(source, packet, ether_size))
960 if(decrement_ttl && source != myself)
961 if(!do_decrement_ttl(source, packet))
964 uint16_t type = packet->data[12] << 8 | packet->data[13];
966 switch (routing_mode) {
970 route_arp(source, packet);
974 route_ipv4(source, packet);
978 route_ipv6(source, packet);
982 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
988 route_mac(source, packet);
992 broadcast_packet(source, packet);