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))
63 static timeout_t age_subnets_timeout;
67 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
69 uint32_t checksum = prevsum ^ 0xFFFF;
77 checksum += *(uint8_t *)p;
80 while(checksum >> 16) {
81 checksum = (checksum & 0xFFFF) + (checksum >> 16);
84 // Work around a compiler optimization bug.
92 static bool ratelimit(int frequency) {
93 static time_t lasttime = 0;
96 if(lasttime == now.tv_sec) {
97 if(count >= frequency) {
101 lasttime = now.tv_sec;
109 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
110 if(packet->len < length) {
111 logger(DEBUG_TRAFFIC, LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
118 static void swap_mac_addresses(vpn_packet_t *packet) {
120 memcpy(&tmp, &DATA(packet)[0], sizeof(tmp));
121 memcpy(&DATA(packet)[0], &DATA(packet)[6], sizeof(tmp));
122 memcpy(&DATA(packet)[6], &tmp, sizeof(tmp));
127 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
129 struct icmp icmp = {0};
131 struct in_addr ip_src;
132 struct in_addr ip_dst;
139 /* Swap Ethernet source and destination addresses */
141 swap_mac_addresses(packet);
143 /* Copy headers from packet into properly aligned structs on the stack */
145 memcpy(&ip, DATA(packet) + ether_size, ip_size);
147 /* Remember original source and destination */
152 /* Try to reply with an IP address assigned to the local machine */
154 if(type == ICMP_TIME_EXCEEDED && code == ICMP_EXC_TTL) {
155 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
158 struct sockaddr_in addr;
159 memset(&addr, 0, sizeof(addr));
160 addr.sin_family = AF_INET;
161 addr.sin_addr = ip.ip_src;
163 if(!connect(sockfd, (const struct sockaddr *) &addr, sizeof(addr))) {
164 memset(&addr, 0, sizeof(addr));
165 addr.sin_family = AF_INET;
166 socklen_t addrlen = sizeof(addr);
168 if(!getsockname(sockfd, (struct sockaddr *) &addr, &addrlen) && addrlen <= sizeof(addr)) {
169 ip_dst = addr.sin_addr;
177 oldlen = packet->len - ether_size;
179 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
180 icmp.icmp_nextmtu = htons(packet->len - ether_size);
183 if(oldlen >= IP_MSS - ip_size - icmp_size) {
184 oldlen = IP_MSS - ip_size - icmp_size;
187 /* Copy first part of original contents to ICMP message */
189 memmove(DATA(packet) + ether_size + ip_size + icmp_size, DATA(packet) + ether_size, oldlen);
191 /* Fill in IPv4 header */
194 ip.ip_hl = ip_size / 4;
196 ip.ip_len = htons(ip_size + icmp_size + oldlen);
200 ip.ip_p = IPPROTO_ICMP;
205 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
207 /* Fill in ICMP header */
209 icmp.icmp_type = type;
210 icmp.icmp_code = code;
213 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
214 icmp.icmp_cksum = inet_checksum(DATA(packet) + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
216 /* Copy structs on stack back to packet */
218 memcpy(DATA(packet) + ether_size, &ip, ip_size);
219 memcpy(DATA(packet) + ether_size + ip_size, &icmp, icmp_size);
221 packet->len = ether_size + ip_size + icmp_size + oldlen;
223 send_packet(source, packet);
228 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
230 struct icmp6_hdr icmp6 = {0};
234 struct in6_addr ip6_src; /* source address */
235 struct in6_addr ip6_dst; /* destination address */
244 /* Swap Ethernet source and destination addresses */
246 swap_mac_addresses(packet);
248 /* Copy headers from packet to structs on the stack */
250 memcpy(&ip6, DATA(packet) + ether_size, ip6_size);
252 /* Remember original source and destination */
254 pseudo.ip6_src = ip6.ip6_dst;
255 pseudo.ip6_dst = ip6.ip6_src;
257 /* Try to reply with an IP address assigned to the local machine */
259 if(type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT) {
260 int sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
263 struct sockaddr_in6 addr;
264 memset(&addr, 0, sizeof(addr));
265 addr.sin6_family = AF_INET6;
266 addr.sin6_addr = ip6.ip6_src;
268 if(!connect(sockfd, (const struct sockaddr *) &addr, sizeof(addr))) {
269 memset(&addr, 0, sizeof(addr));
270 addr.sin6_family = AF_INET6;
271 socklen_t addrlen = sizeof(addr);
273 if(!getsockname(sockfd, (struct sockaddr *) &addr, &addrlen) && addrlen <= sizeof(addr)) {
274 pseudo.ip6_src = addr.sin6_addr;
282 pseudo.length = packet->len - ether_size;
284 if(type == ICMP6_PACKET_TOO_BIG) {
285 icmp6.icmp6_mtu = htonl(pseudo.length);
288 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size) {
289 pseudo.length = IP_MSS - ip6_size - icmp6_size;
292 /* Copy first part of original contents to ICMP message */
294 memmove(DATA(packet) + ether_size + ip6_size + icmp6_size, DATA(packet) + ether_size, pseudo.length);
296 /* Fill in IPv6 header */
298 ip6.ip6_flow = htonl(0x60000000UL);
299 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
300 ip6.ip6_nxt = IPPROTO_ICMPV6;
302 ip6.ip6_src = pseudo.ip6_src;
303 ip6.ip6_dst = pseudo.ip6_dst;
305 /* Fill in ICMP header */
307 icmp6.icmp6_type = type;
308 icmp6.icmp6_code = code;
309 icmp6.icmp6_cksum = 0;
311 /* Create pseudo header */
313 pseudo.length = htonl(icmp6_size + pseudo.length);
314 pseudo.next = htonl(IPPROTO_ICMPV6);
316 /* Generate checksum */
318 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
319 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
320 checksum = inet_checksum(DATA(packet) + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
322 icmp6.icmp6_cksum = checksum;
324 /* Copy structs on stack back to packet */
326 memcpy(DATA(packet) + ether_size, &ip6, ip6_size);
327 memcpy(DATA(packet) + ether_size + ip6_size, &icmp6, icmp6_size);
329 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
331 send_packet(source, packet);
334 static bool do_decrement_ttl(node_t *source, vpn_packet_t *packet) {
335 uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
336 length_t ethlen = ether_size;
338 if(type == ETH_P_8021Q) {
339 type = DATA(packet)[16] << 8 | DATA(packet)[17];
345 if(!checklength(source, packet, ethlen + ip_size)) {
349 if(DATA(packet)[ethlen + 8] <= 1) {
350 if(DATA(packet)[ethlen + 11] != IPPROTO_ICMP || DATA(packet)[ethlen + 32] != ICMP_TIME_EXCEEDED) {
351 route_ipv4_unreachable(source, packet, ethlen, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL);
357 uint16_t old = DATA(packet)[ethlen + 8] << 8 | DATA(packet)[ethlen + 9];
358 DATA(packet)[ethlen + 8]--;
359 uint16_t new = DATA(packet)[ethlen + 8] << 8 | DATA(packet)[ethlen + 9];
361 uint32_t checksum = DATA(packet)[ethlen + 10] << 8 | DATA(packet)[ethlen + 11];
362 checksum += old + (~new & 0xFFFF);
364 while(checksum >> 16) {
365 checksum = (checksum & 0xFFFF) + (checksum >> 16);
368 DATA(packet)[ethlen + 10] = checksum >> 8;
369 DATA(packet)[ethlen + 11] = checksum & 0xff;
374 if(!checklength(source, packet, ethlen + ip6_size)) {
378 if(DATA(packet)[ethlen + 7] <= 1) {
379 if(DATA(packet)[ethlen + 6] != IPPROTO_ICMPV6 || DATA(packet)[ethlen + 40] != ICMP6_TIME_EXCEEDED) {
380 route_ipv6_unreachable(source, packet, ethlen, ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT);
386 DATA(packet)[ethlen + 7]--;
395 static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
396 if(!source || !via || !(via->options & OPTION_CLAMP_MSS)) {
400 uint16_t mtu = source->mtu;
402 if(via != myself && via->mtu < mtu) {
406 /* Find TCP header */
407 int start = ether_size;
408 uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
410 if(type == ETH_P_8021Q) {
412 type = DATA(packet)[16] << 8 | DATA(packet)[17];
415 /* IP in IP (RFC 2003) packet */
416 if(type == ETH_P_IP && DATA(packet)[start + 9] == 4) {
420 if(packet->len <= start + 20) {
424 if(type == ETH_P_IP && DATA(packet)[start + 9] == 6) {
425 start += (DATA(packet)[start] & 0xf) * 4;
426 } else if(type == ETH_P_IPV6 && DATA(packet)[start + 6] == 6) {
432 if(packet->len <= start + 20) {
436 /* Use data offset field to calculate length of options field */
437 int len = ((DATA(packet)[start + 12] >> 4) - 5) * 4;
439 if(packet->len < start + 20 + len) {
443 /* Search for MSS option header */
444 for(int i = 0; i < len;) {
445 if(DATA(packet)[start + 20 + i] == 0) {
449 if(DATA(packet)[start + 20 + i] == 1) {
454 if(i > len - 2 || i > len - DATA(packet)[start + 21 + i]) {
458 if(DATA(packet)[start + 20 + i] != 2) {
459 if(DATA(packet)[start + 21 + i] < 2) {
463 i += DATA(packet)[start + 21 + i];
467 if(DATA(packet)[start + 21] != 4) {
472 uint16_t oldmss = DATA(packet)[start + 22 + i] << 8 | DATA(packet)[start + 23 + i];
473 uint16_t newmss = mtu - start - 20;
474 uint32_t csum = DATA(packet)[start + 16] << 8 | DATA(packet)[start + 17];
476 if(oldmss <= newmss) {
480 logger(DEBUG_TRAFFIC, LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
482 /* Update the MSS value and the checksum */
483 DATA(packet)[start + 22 + i] = newmss >> 8;
484 DATA(packet)[start + 23 + i] = newmss & 0xff;
486 csum += oldmss ^ 0xffff;
488 csum = (csum & 0xffff) + (csum >> 16);
491 DATA(packet)[start + 16] = csum >> 8;
492 DATA(packet)[start + 17] = csum;
497 static void age_subnets(void *data) {
500 for splay_each(subnet_t, s, myself->subnet_tree) {
501 if(s->expires && s->expires < now.tv_sec) {
502 if(debug_level >= DEBUG_TRAFFIC) {
503 char netstr[MAXNETSTR];
505 if(net2str(netstr, sizeof(netstr), s)) {
506 logger(DEBUG_TRAFFIC, LOG_INFO, "Subnet %s expired", netstr);
510 for list_each(connection_t, c, connection_list)
512 send_del_subnet(c, s);
515 subnet_del(myself, s);
524 timeout_set(&age_subnets_timeout, &(struct timeval) {
529 static void learn_mac(mac_t *address) {
530 subnet_t *subnet = lookup_subnet_mac(myself, address);
532 /* If we don't know this MAC address yet, store it */
535 logger(DEBUG_TRAFFIC, LOG_INFO, "Learned new MAC address %x:%x:%x:%x:%x:%x",
536 address->x[0], address->x[1], address->x[2], address->x[3],
537 address->x[4], address->x[5]);
539 subnet = new_subnet();
540 subnet->type = SUBNET_MAC;
541 subnet->expires = now.tv_sec + macexpire;
542 subnet->net.mac.address = *address;
544 subnet_add(myself, subnet);
545 subnet_update(myself, subnet, true);
547 /* And tell all other tinc daemons it's our MAC */
549 for list_each(connection_t, c, connection_list)
551 send_add_subnet(c, subnet);
554 timeout_add(&age_subnets_timeout, age_subnets, NULL, &(struct timeval) {
558 if(subnet->expires) {
559 subnet->expires = now.tv_sec + macexpire;
564 static void route_broadcast(node_t *source, vpn_packet_t *packet) {
565 if(decrement_ttl && source != myself)
566 if(!do_decrement_ttl(source, packet)) {
570 broadcast_packet(source, packet);
575 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t ether_size) {
577 vpn_packet_t fragment;
580 uint16_t ip_off, origf;
582 memcpy(&ip, DATA(packet) + ether_size, ip_size);
583 fragment.priority = packet->priority;
584 fragment.offset = DEFAULT_PACKET_OFFSET;
586 if(ip.ip_hl != ip_size / 4) {
590 todo = ntohs(ip.ip_len) - ip_size;
592 if(ether_size + ip_size + todo != packet->len) {
593 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));
597 logger(DEBUG_TRAFFIC, LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
599 offset = DATA(packet) + ether_size + ip_size;
600 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
601 ip_off = ntohs(ip.ip_off);
602 origf = ip_off & ~IP_OFFMASK;
603 ip_off &= IP_OFFMASK;
606 int len = todo > maxlen ? maxlen : todo;
607 memcpy(DATA(&fragment) + ether_size + ip_size, offset, len);
611 ip.ip_len = htons(ip_size + len);
612 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
614 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
615 memcpy(DATA(&fragment), DATA(packet), ether_size);
616 memcpy(DATA(&fragment) + ether_size, &ip, ip_size);
617 fragment.len = ether_size + ip_size + len;
619 send_packet(dest, &fragment);
625 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
626 if(!checklength(source, packet, ether_size + ip_size)) {
634 memcpy(&dest, &DATA(packet)[30], sizeof(dest));
635 subnet = lookup_subnet_ipv4(&dest);
638 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
639 source->name, source->hostname,
645 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
650 route_broadcast(source, packet);
654 if(subnet->owner == source) {
655 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
659 if(!subnet->owner->status.reachable) {
660 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
664 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
665 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
669 if(decrement_ttl && source != myself && subnet->owner != myself)
670 if(!do_decrement_ttl(source, packet)) {
674 if(priorityinheritance) {
675 packet->priority = DATA(packet)[15];
678 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
681 logger(DEBUG_TRAFFIC, LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
685 if(directonly && subnet->owner != via) {
686 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
690 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
691 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);
693 if(DATA(packet)[20] & 0x40) {
694 packet->len = MAX(via->mtu, 590);
695 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
697 fragment_ipv4_packet(via, packet, ether_size);
703 clamp_mss(source, via, packet);
705 send_packet(subnet->owner, packet);
708 static void route_neighborsol(node_t *source, vpn_packet_t *packet);
710 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
711 if(!checklength(source, packet, ether_size + ip6_size)) {
715 if(DATA(packet)[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && DATA(packet)[54] == ND_NEIGHBOR_SOLICIT) {
716 route_neighborsol(source, packet);
724 memcpy(&dest, &DATA(packet)[38], sizeof(dest));
725 subnet = lookup_subnet_ipv6(&dest);
728 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
729 source->name, source->hostname,
739 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
744 route_broadcast(source, packet);
748 if(subnet->owner == source) {
749 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
753 if(!subnet->owner->status.reachable) {
754 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
758 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
759 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
763 if(decrement_ttl && source != myself && subnet->owner != myself)
764 if(!do_decrement_ttl(source, packet)) {
768 if(priorityinheritance) {
769 packet->priority = ((DATA(packet)[14] & 0x0f) << 4) | (DATA(packet)[15] >> 4);
772 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
775 logger(DEBUG_TRAFFIC, LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
779 if(directonly && subnet->owner != via) {
780 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
784 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
785 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);
786 packet->len = MAX(via->mtu, 1294);
787 route_ipv6_unreachable(source, packet, ether_size, ICMP6_PACKET_TOO_BIG, 0);
791 clamp_mss(source, via, packet);
793 send_packet(subnet->owner, packet);
798 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
800 struct nd_neighbor_solicit ns;
801 struct nd_opt_hdr opt;
807 struct in6_addr ip6_src;
808 struct in6_addr ip6_dst;
813 if(!checklength(source, packet, ether_size + ip6_size + ns_size)) {
817 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
819 if(source != myself) {
820 logger(DEBUG_TRAFFIC, LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
824 /* Copy headers from packet to structs on the stack */
826 memcpy(&ip6, DATA(packet) + ether_size, ip6_size);
827 memcpy(&ns, DATA(packet) + ether_size + ip6_size, ns_size);
830 memcpy(&opt, DATA(packet) + ether_size + ip6_size + ns_size, opt_size);
833 /* First, snatch the source address from the neighbor solicitation packet */
836 memcpy(mymac.x, DATA(packet) + ETH_ALEN, ETH_ALEN);
839 /* Check if this is a valid neighbor solicitation request */
841 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
842 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
843 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
847 /* Create pseudo header */
849 pseudo.ip6_src = ip6.ip6_src;
850 pseudo.ip6_dst = ip6.ip6_dst;
853 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
855 pseudo.length = htonl(ns_size);
858 pseudo.next = htonl(IPPROTO_ICMPV6);
860 /* Generate checksum */
862 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
863 checksum = inet_checksum(&ns, ns_size, checksum);
866 checksum = inet_checksum(&opt, opt_size, checksum);
867 checksum = inet_checksum(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
871 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
875 /* Check if the IPv6 address exists on the VPN */
877 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
880 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
881 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
882 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
883 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
884 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
885 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
886 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
887 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
888 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
893 /* Check if it is for our own subnet */
895 if(subnet->owner == myself) {
896 return; /* silently ignore */
900 if(!do_decrement_ttl(source, packet)) {
904 /* Create neighbor advertation reply */
906 memcpy(DATA(packet), DATA(packet) + ETH_ALEN, ETH_ALEN); /* copy destination address */
907 DATA(packet)[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
909 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
910 ip6.ip6_src = ns.nd_ns_target;
913 memcpy(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, DATA(packet) + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
917 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
918 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
919 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
921 /* Create pseudo header */
923 pseudo.ip6_src = ip6.ip6_src;
924 pseudo.ip6_dst = ip6.ip6_dst;
927 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
929 pseudo.length = htonl(ns_size);
932 pseudo.next = htonl(IPPROTO_ICMPV6);
934 /* Generate checksum */
936 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
937 checksum = inet_checksum(&ns, ns_size, checksum);
940 checksum = inet_checksum(&opt, opt_size, checksum);
941 checksum = inet_checksum(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
944 ns.nd_ns_hdr.icmp6_cksum = checksum;
946 /* Copy structs on stack back to packet */
948 memcpy(DATA(packet) + ether_size, &ip6, ip6_size);
949 memcpy(DATA(packet) + ether_size + ip6_size, &ns, ns_size);
952 memcpy(DATA(packet) + ether_size + ip6_size + ns_size, &opt, opt_size);
955 send_packet(source, packet);
960 static void route_arp(node_t *source, vpn_packet_t *packet) {
961 struct ether_arp arp;
965 if(!checklength(source, packet, ether_size + arp_size)) {
969 if(source != myself) {
970 logger(DEBUG_TRAFFIC, LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
974 /* First, snatch the source address from the ARP packet */
977 memcpy(mymac.x, DATA(packet) + ETH_ALEN, ETH_ALEN);
980 /* Copy headers from packet to structs on the stack */
982 memcpy(&arp, DATA(packet) + ether_size, arp_size);
984 /* Check if this is a valid ARP request */
986 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
987 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
988 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: received unknown type ARP request");
992 /* Check if the IPv4 address exists on the VPN */
994 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
997 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
998 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
1003 /* Check if it is for our own subnet */
1005 if(subnet->owner == myself) {
1006 return; /* silently ignore */
1010 if(!do_decrement_ttl(source, packet)) {
1014 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
1015 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
1016 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
1018 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
1019 memcpy(arp.arp_sha, DATA(packet) + ETH_ALEN, ETH_ALEN); /* set source hard/proto addr */
1020 arp.arp_sha[ETH_ALEN - 1] ^= 0xFF; /* for consistency with route_packet() */
1021 arp.arp_op = htons(ARPOP_REPLY);
1023 /* Copy structs on stack back to packet */
1025 memcpy(DATA(packet) + ether_size, &arp, arp_size);
1027 send_packet(source, packet);
1030 static void route_mac(node_t *source, vpn_packet_t *packet) {
1034 /* Learn source address */
1036 if(source == myself) {
1038 memcpy(&src, &DATA(packet)[6], sizeof(src));
1042 /* Lookup destination address */
1044 memcpy(&dest, &DATA(packet)[0], sizeof(dest));
1045 subnet = lookup_subnet_mac(NULL, &dest);
1047 if(!subnet || !subnet->owner) {
1048 route_broadcast(source, packet);
1052 if(subnet->owner == source) {
1053 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
1057 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
1061 if(decrement_ttl && source != myself && subnet->owner != myself)
1062 if(!do_decrement_ttl(source, packet)) {
1066 uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
1068 if(priorityinheritance) {
1069 if(type == ETH_P_IP && packet->len >= ether_size + ip_size) {
1070 packet->priority = DATA(packet)[15];
1071 } else if(type == ETH_P_IPV6 && packet->len >= ether_size + ip6_size) {
1072 packet->priority = ((DATA(packet)[14] & 0x0f) << 4) | (DATA(packet)[15] >> 4);
1076 // Handle packets larger than PMTU
1078 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
1080 if(directonly && subnet->owner != via) {
1084 if(via && packet->len > via->mtu && via != myself) {
1085 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);
1086 length_t ethlen = 14;
1088 if(type == ETH_P_8021Q) {
1089 type = DATA(packet)[16] << 8 | DATA(packet)[17];
1093 if(type == ETH_P_IP && packet->len > 576 + ethlen) {
1094 if(DATA(packet)[6 + ethlen] & 0x40) {
1095 packet->len = via->mtu;
1096 route_ipv4_unreachable(source, packet, ethlen, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
1098 fragment_ipv4_packet(via, packet, ethlen);
1102 } else if(type == ETH_P_IPV6 && packet->len > 1280 + ethlen) {
1103 packet->len = via->mtu;
1104 route_ipv6_unreachable(source, packet, ethlen, ICMP6_PACKET_TOO_BIG, 0);
1109 clamp_mss(source, via, packet);
1111 send_packet(subnet->owner, packet);
1114 static void send_pcap(vpn_packet_t *packet) {
1117 for list_each(connection_t, c, connection_list) {
1118 if(!c->status.pcap) {
1123 int len = packet->len;
1125 if(c->outmaclength && c->outmaclength < len) {
1126 len = c->outmaclength;
1129 if(send_request(c, "%d %d %d", CONTROL, REQ_PCAP, len)) {
1130 send_meta(c, (char *)DATA(packet), len);
1135 void route(node_t *source, vpn_packet_t *packet) {
1140 if(forwarding_mode == FMODE_KERNEL && source != myself) {
1141 send_packet(myself, packet);
1145 if(!checklength(source, packet, ether_size)) {
1149 uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
1151 switch(routing_mode) {
1155 route_arp(source, packet);
1159 route_ipv4(source, packet);
1163 route_ipv6(source, packet);
1167 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
1174 route_mac(source, packet);
1178 route_broadcast(source, packet);