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;
267 /* Swap Ethernet source and destination addresses */
269 swap_mac_addresses(packet);
271 /* Copy headers from packet into properly aligned structs on the stack */
273 memcpy(&ip, DATA(packet) + ether_size, ip_size);
275 /* Remember original source and destination */
280 /* Try to reply with an IP address assigned to the local machine */
282 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
284 struct sockaddr_in addr;
285 memset(&addr, 0, sizeof(addr));
286 addr.sin_family = AF_INET;
287 addr.sin_addr = ip.ip_src;
288 if (!connect(sockfd, (const struct sockaddr*) &addr, sizeof(addr))) {
289 memset(&addr, 0, sizeof(addr));
290 addr.sin_family = AF_INET;
291 socklen_t addrlen = sizeof(addr);
292 if (!getsockname(sockfd, (struct sockaddr*) &addr, &addrlen) && addrlen <= sizeof(addr)) {
293 ip_dst = addr.sin_addr;
299 oldlen = packet->len - ether_size;
301 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
302 icmp.icmp_nextmtu = htons(packet->len - ether_size);
304 if(oldlen >= IP_MSS - ip_size - icmp_size)
305 oldlen = IP_MSS - ip_size - icmp_size;
307 /* Copy first part of original contents to ICMP message */
309 memmove(DATA(packet) + ether_size + ip_size + icmp_size, DATA(packet) + ether_size, oldlen);
311 /* Fill in IPv4 header */
314 ip.ip_hl = ip_size / 4;
316 ip.ip_len = htons(ip_size + icmp_size + oldlen);
320 ip.ip_p = IPPROTO_ICMP;
325 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
327 /* Fill in ICMP header */
329 icmp.icmp_type = type;
330 icmp.icmp_code = code;
333 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
334 icmp.icmp_cksum = inet_checksum(DATA(packet) + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
336 /* Copy structs on stack back to packet */
338 memcpy(DATA(packet) + ether_size, &ip, ip_size);
339 memcpy(DATA(packet) + ether_size + ip_size, &icmp, icmp_size);
341 packet->len = ether_size + ip_size + icmp_size + oldlen;
343 send_packet(source, packet);
348 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t ether_size) {
350 vpn_packet_t fragment;
351 int len, maxlen, todo;
353 uint16_t ip_off, origf;
355 memcpy(&ip, DATA(packet) + ether_size, ip_size);
356 fragment.priority = packet->priority;
357 fragment.offset = DEFAULT_PACKET_OFFSET;
359 if(ip.ip_hl != ip_size / 4)
362 todo = ntohs(ip.ip_len) - ip_size;
364 if(ether_size + ip_size + todo != packet->len) {
365 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));
369 logger(DEBUG_TRAFFIC, LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
371 offset = DATA(packet) + ether_size + ip_size;
372 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
373 ip_off = ntohs(ip.ip_off);
374 origf = ip_off & ~IP_OFFMASK;
375 ip_off &= IP_OFFMASK;
378 len = todo > maxlen ? maxlen : todo;
379 memcpy(DATA(&fragment) + ether_size + ip_size, offset, len);
383 ip.ip_len = htons(ip_size + len);
384 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
386 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
387 memcpy(DATA(&fragment), DATA(packet), ether_size);
388 memcpy(DATA(&fragment) + ether_size, &ip, ip_size);
389 fragment.len = ether_size + ip_size + len;
391 send_packet(dest, &fragment);
397 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
398 if(!checklength(source, packet, ether_size + ip_size))
405 memcpy(&dest, &DATA(packet)[30], sizeof dest);
406 subnet = lookup_subnet_ipv4(&dest);
409 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
410 source->name, source->hostname,
416 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
420 if (!subnet->owner) {
421 broadcast_packet(source, packet);
425 if(subnet->owner == source) {
426 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
430 if(!subnet->owner->status.reachable)
431 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
433 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
434 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
436 if(priorityinheritance)
437 packet->priority = DATA(packet)[15];
439 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
442 logger(DEBUG_TRAFFIC, LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
446 if(directonly && subnet->owner != via)
447 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
449 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
450 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);
451 if(DATA(packet)[20] & 0x40) {
452 packet->len = MAX(via->mtu, 590);
453 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
455 fragment_ipv4_packet(via, packet, ether_size);
461 clamp_mss(source, via, packet);
463 send_packet(subnet->owner, packet);
468 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
470 struct icmp6_hdr icmp6 = {0};
475 struct in6_addr ip6_src; /* source address */
476 struct in6_addr ip6_dst; /* destination address */
484 /* Swap Ethernet source and destination addresses */
486 swap_mac_addresses(packet);
488 /* Copy headers from packet to structs on the stack */
490 memcpy(&ip6, DATA(packet) + ether_size, ip6_size);
492 /* Remember original source and destination */
494 pseudo.ip6_src = ip6.ip6_dst;
495 pseudo.ip6_dst = ip6.ip6_src;
497 /* Try to reply with an IP address assigned to the local machine */
499 sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
501 struct sockaddr_in6 addr;
502 memset(&addr, 0, sizeof(addr));
503 addr.sin6_family = AF_INET6;
504 addr.sin6_addr = ip6.ip6_src;
505 if (!connect(sockfd, (const struct sockaddr*) &addr, sizeof(addr))) {
506 memset(&addr, 0, sizeof(addr));
507 addr.sin6_family = AF_INET6;
508 socklen_t addrlen = sizeof(addr);
509 if (!getsockname(sockfd, (struct sockaddr*) &addr, &addrlen) && addrlen <= sizeof(addr)) {
510 pseudo.ip6_src = addr.sin6_addr;
516 pseudo.length = packet->len - ether_size;
518 if(type == ICMP6_PACKET_TOO_BIG)
519 icmp6.icmp6_mtu = htonl(pseudo.length);
521 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
522 pseudo.length = IP_MSS - ip6_size - icmp6_size;
524 /* Copy first part of original contents to ICMP message */
526 memmove(DATA(packet) + ether_size + ip6_size + icmp6_size, DATA(packet) + ether_size, pseudo.length);
528 /* Fill in IPv6 header */
530 ip6.ip6_flow = htonl(0x60000000UL);
531 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
532 ip6.ip6_nxt = IPPROTO_ICMPV6;
534 ip6.ip6_src = pseudo.ip6_src;
535 ip6.ip6_dst = pseudo.ip6_dst;
537 /* Fill in ICMP header */
539 icmp6.icmp6_type = type;
540 icmp6.icmp6_code = code;
541 icmp6.icmp6_cksum = 0;
543 /* Create pseudo header */
545 pseudo.length = htonl(icmp6_size + pseudo.length);
546 pseudo.next = htonl(IPPROTO_ICMPV6);
548 /* Generate checksum */
550 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
551 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
552 checksum = inet_checksum(DATA(packet) + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
554 icmp6.icmp6_cksum = checksum;
556 /* Copy structs on stack back to packet */
558 memcpy(DATA(packet) + ether_size, &ip6, ip6_size);
559 memcpy(DATA(packet) + ether_size + ip6_size, &icmp6, icmp6_size);
561 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
563 send_packet(source, packet);
566 static void route_neighborsol(node_t *source, vpn_packet_t *packet);
568 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
569 if(!checklength(source, packet, ether_size + ip6_size))
572 if(DATA(packet)[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && DATA(packet)[54] == ND_NEIGHBOR_SOLICIT) {
573 route_neighborsol(source, packet);
581 memcpy(&dest, &DATA(packet)[38], sizeof dest);
582 subnet = lookup_subnet_ipv6(&dest);
585 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
586 source->name, source->hostname,
596 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
600 if (!subnet->owner) {
601 broadcast_packet(source, packet);
605 if(subnet->owner == source) {
606 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
610 if(!subnet->owner->status.reachable)
611 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
613 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
614 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
616 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
619 logger(DEBUG_TRAFFIC, LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
623 if(directonly && subnet->owner != via)
624 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
626 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
627 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);
628 packet->len = MAX(via->mtu, 1294);
629 route_ipv6_unreachable(source, packet, ether_size, ICMP6_PACKET_TOO_BIG, 0);
633 clamp_mss(source, via, packet);
635 send_packet(subnet->owner, packet);
640 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
642 struct nd_neighbor_solicit ns;
643 struct nd_opt_hdr opt;
649 struct in6_addr ip6_src;
650 struct in6_addr ip6_dst;
655 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
658 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
660 if(source != myself) {
661 logger(DEBUG_TRAFFIC, LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
665 /* Copy headers from packet to structs on the stack */
667 memcpy(&ip6, DATA(packet) + ether_size, ip6_size);
668 memcpy(&ns, DATA(packet) + ether_size + ip6_size, ns_size);
670 memcpy(&opt, DATA(packet) + ether_size + ip6_size + ns_size, opt_size);
672 /* First, snatch the source address from the neighbor solicitation packet */
675 memcpy(mymac.x, DATA(packet) + ETH_ALEN, ETH_ALEN);
677 /* Check if this is a valid neighbor solicitation request */
679 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
680 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
681 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
685 /* Create pseudo header */
687 pseudo.ip6_src = ip6.ip6_src;
688 pseudo.ip6_dst = ip6.ip6_dst;
690 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
692 pseudo.length = htonl(ns_size);
693 pseudo.next = htonl(IPPROTO_ICMPV6);
695 /* Generate checksum */
697 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
698 checksum = inet_checksum(&ns, ns_size, checksum);
700 checksum = inet_checksum(&opt, opt_size, checksum);
701 checksum = inet_checksum(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
705 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
709 /* Check if the IPv6 address exists on the VPN */
711 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
714 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
715 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
716 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
717 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
718 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
719 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
720 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
721 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
722 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
727 /* Check if it is for our own subnet */
729 if(subnet->owner == myself)
730 return; /* silently ignore */
732 /* Create neighbor advertation reply */
734 memcpy(DATA(packet), DATA(packet) + ETH_ALEN, ETH_ALEN); /* copy destination address */
735 DATA(packet)[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
737 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
738 ip6.ip6_src = ns.nd_ns_target;
741 memcpy(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, DATA(packet) + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
744 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
745 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
746 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
748 /* Create pseudo header */
750 pseudo.ip6_src = ip6.ip6_src;
751 pseudo.ip6_dst = ip6.ip6_dst;
753 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
755 pseudo.length = htonl(ns_size);
756 pseudo.next = htonl(IPPROTO_ICMPV6);
758 /* Generate checksum */
760 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
761 checksum = inet_checksum(&ns, ns_size, checksum);
763 checksum = inet_checksum(&opt, opt_size, checksum);
764 checksum = inet_checksum(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
767 ns.nd_ns_hdr.icmp6_cksum = checksum;
769 /* Copy structs on stack back to packet */
771 memcpy(DATA(packet) + ether_size, &ip6, ip6_size);
772 memcpy(DATA(packet) + ether_size + ip6_size, &ns, ns_size);
774 memcpy(DATA(packet) + ether_size + ip6_size + ns_size, &opt, opt_size);
776 send_packet(source, packet);
781 static void route_arp(node_t *source, vpn_packet_t *packet) {
782 struct ether_arp arp;
786 if(!checklength(source, packet, ether_size + arp_size))
789 if(source != myself) {
790 logger(DEBUG_TRAFFIC, LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
794 /* First, snatch the source address from the ARP packet */
797 memcpy(mymac.x, DATA(packet) + ETH_ALEN, ETH_ALEN);
799 /* Copy headers from packet to structs on the stack */
801 memcpy(&arp, DATA(packet) + ether_size, arp_size);
803 /* Check if this is a valid ARP request */
805 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
806 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof addr || ntohs(arp.arp_op) != ARPOP_REQUEST) {
807 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: received unknown type ARP request");
811 /* Check if the IPv4 address exists on the VPN */
813 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
816 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
817 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
822 /* Check if it is for our own subnet */
824 if(subnet->owner == myself)
825 return; /* silently ignore */
827 memcpy(&addr, arp.arp_tpa, sizeof addr); /* save protocol addr */
828 memcpy(arp.arp_tpa, arp.arp_spa, sizeof addr); /* swap destination and source protocol address */
829 memcpy(arp.arp_spa, &addr, sizeof addr); /* ... */
831 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
832 memcpy(arp.arp_sha, DATA(packet) + ETH_ALEN, ETH_ALEN); /* set source hard/proto addr */
833 arp.arp_sha[ETH_ALEN - 1] ^= 0xFF; /* for consistency with route_packet() */
834 arp.arp_op = htons(ARPOP_REPLY);
836 /* Copy structs on stack back to packet */
838 memcpy(DATA(packet) + ether_size, &arp, arp_size);
840 send_packet(source, packet);
843 static void route_mac(node_t *source, vpn_packet_t *packet) {
847 /* Learn source address */
849 if(source == myself) {
851 memcpy(&src, &DATA(packet)[6], sizeof src);
855 /* Lookup destination address */
857 memcpy(&dest, &DATA(packet)[0], sizeof dest);
858 subnet = lookup_subnet_mac(NULL, &dest);
860 if(!subnet || !subnet->owner) {
861 broadcast_packet(source, packet);
865 if(subnet->owner == source) {
866 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
870 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
873 uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
875 if(priorityinheritance && type == ETH_P_IP && packet->len >= ether_size + ip_size)
876 packet->priority = DATA(packet)[15];
878 // Handle packets larger than PMTU
880 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
882 if(directonly && subnet->owner != via)
885 if(via && packet->len > via->mtu && via != myself) {
886 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);
887 length_t ethlen = 14;
889 if(type == ETH_P_8021Q) {
890 type = DATA(packet)[16] << 8 | DATA(packet)[17];
894 if(type == ETH_P_IP && packet->len > 576 + ethlen) {
895 if(DATA(packet)[6 + ethlen] & 0x40) {
896 packet->len = via->mtu;
897 route_ipv4_unreachable(source, packet, ethlen, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
899 fragment_ipv4_packet(via, packet, ethlen);
902 } else if(type == ETH_P_IPV6 && packet->len > 1280 + ethlen) {
903 packet->len = via->mtu;
904 route_ipv6_unreachable(source, packet, ethlen, ICMP6_PACKET_TOO_BIG, 0);
909 clamp_mss(source, via, packet);
911 send_packet(subnet->owner, packet);
914 static void send_pcap(vpn_packet_t *packet) {
917 for list_each(connection_t, c, connection_list) {
922 int len = packet->len;
923 if(c->outmaclength && c->outmaclength < len)
924 len = c->outmaclength;
926 if(send_request(c, "%d %d %d", CONTROL, REQ_PCAP, len))
927 send_meta(c, (char *)DATA(packet), len);
931 static bool do_decrement_ttl(node_t *source, vpn_packet_t *packet) {
932 uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
933 length_t ethlen = ether_size;
935 if(type == ETH_P_8021Q) {
936 type = DATA(packet)[16] << 8 | DATA(packet)[17];
942 if(!checklength(source, packet, ethlen + ip_size))
945 if(DATA(packet)[ethlen + 8] <= 1) {
946 if(DATA(packet)[ethlen + 11] != IPPROTO_ICMP || DATA(packet)[ethlen + 32] != ICMP_TIME_EXCEEDED)
947 route_ipv4_unreachable(source, packet, ethlen, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL);
951 uint16_t old = DATA(packet)[ethlen + 8] << 8 | DATA(packet)[ethlen + 9];
952 DATA(packet)[ethlen + 8]--;
953 uint16_t new = DATA(packet)[ethlen + 8] << 8 | DATA(packet)[ethlen + 9];
955 uint32_t checksum = DATA(packet)[ethlen + 10] << 8 | DATA(packet)[ethlen + 11];
956 checksum += old + (~new & 0xFFFF);
957 while(checksum >> 16)
958 checksum = (checksum & 0xFFFF) + (checksum >> 16);
959 DATA(packet)[ethlen + 10] = checksum >> 8;
960 DATA(packet)[ethlen + 11] = checksum & 0xff;
965 if(!checklength(source, packet, ethlen + ip6_size))
968 if(DATA(packet)[ethlen + 7] <= 1) {
969 if(DATA(packet)[ethlen + 6] != IPPROTO_ICMPV6 || DATA(packet)[ethlen + 40] != ICMP6_TIME_EXCEEDED)
970 route_ipv6_unreachable(source, packet, ethlen, ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT);
974 DATA(packet)[ethlen + 7]--;
983 void route(node_t *source, vpn_packet_t *packet) {
987 if(forwarding_mode == FMODE_KERNEL && source != myself) {
988 send_packet(myself, packet);
992 if(!checklength(source, packet, ether_size))
995 if(decrement_ttl && source != myself)
996 if(!do_decrement_ttl(source, packet))
999 uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
1001 switch (routing_mode) {
1005 route_arp(source, packet);
1009 route_ipv4(source, packet);
1013 route_ipv6(source, packet);
1017 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
1023 route_mac(source, packet);
1027 broadcast_packet(source, packet);