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) {
501 for splay_each(subnet_t, s, myself->subnet_tree) {
502 if(s->expires && s->expires < now.tv_sec) {
503 if(debug_level >= DEBUG_TRAFFIC) {
504 char netstr[MAXNETSTR];
506 if(net2str(netstr, sizeof(netstr), s)) {
507 logger(DEBUG_TRAFFIC, LOG_INFO, "Subnet %s expired", netstr);
511 for list_each(connection_t, c, connection_list)
513 send_del_subnet(c, s);
516 subnet_del(myself, s);
525 timeout_set(&age_subnets_timeout, &(struct timeval) {
530 static void learn_mac(mac_t *address) {
531 subnet_t *subnet = lookup_subnet_mac(myself, address);
533 /* If we don't know this MAC address yet, store it */
536 logger(DEBUG_TRAFFIC, LOG_INFO, "Learned new MAC address %x:%x:%x:%x:%x:%x",
537 address->x[0], address->x[1], address->x[2], address->x[3],
538 address->x[4], address->x[5]);
540 subnet = new_subnet();
541 subnet->type = SUBNET_MAC;
542 subnet->expires = now.tv_sec + macexpire;
543 subnet->net.mac.address = *address;
545 subnet_add(myself, subnet);
546 subnet_update(myself, subnet, true);
548 /* And tell all other tinc daemons it's our MAC */
550 for list_each(connection_t, c, connection_list)
552 send_add_subnet(c, subnet);
555 timeout_add(&age_subnets_timeout, age_subnets, NULL, &(struct timeval) {
559 if(subnet->expires) {
560 subnet->expires = now.tv_sec + macexpire;
565 static void route_broadcast(node_t *source, vpn_packet_t *packet) {
566 if(decrement_ttl && source != myself)
567 if(!do_decrement_ttl(source, packet)) {
571 broadcast_packet(source, packet);
576 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t ether_size) {
578 vpn_packet_t fragment;
581 uint16_t ip_off, origf;
583 memcpy(&ip, DATA(packet) + ether_size, ip_size);
584 fragment.priority = packet->priority;
585 fragment.offset = DEFAULT_PACKET_OFFSET;
587 if(ip.ip_hl != ip_size / 4) {
591 todo = ntohs(ip.ip_len) - ip_size;
593 if(ether_size + ip_size + todo != packet->len) {
594 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));
598 logger(DEBUG_TRAFFIC, LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
600 offset = DATA(packet) + ether_size + ip_size;
601 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
602 ip_off = ntohs(ip.ip_off);
603 origf = ip_off & ~IP_OFFMASK;
604 ip_off &= IP_OFFMASK;
607 int len = todo > maxlen ? maxlen : todo;
608 memcpy(DATA(&fragment) + ether_size + ip_size, offset, len);
612 ip.ip_len = htons(ip_size + len);
613 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
615 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
616 memcpy(DATA(&fragment), DATA(packet), ether_size);
617 memcpy(DATA(&fragment) + ether_size, &ip, ip_size);
618 fragment.len = ether_size + ip_size + len;
620 send_packet(dest, &fragment);
626 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
627 if(!checklength(source, packet, ether_size + ip_size)) {
635 memcpy(&dest, &DATA(packet)[30], sizeof(dest));
636 subnet = lookup_subnet_ipv4(&dest);
639 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
640 source->name, source->hostname,
646 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
651 route_broadcast(source, packet);
655 if(subnet->owner == source) {
656 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
660 if(!subnet->owner->status.reachable) {
661 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
665 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
666 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
670 if(decrement_ttl && source != myself && subnet->owner != myself)
671 if(!do_decrement_ttl(source, packet)) {
675 if(priorityinheritance) {
676 packet->priority = DATA(packet)[15];
679 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
682 logger(DEBUG_TRAFFIC, LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
686 if(directonly && subnet->owner != via) {
687 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
691 if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
692 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);
694 if(DATA(packet)[20] & 0x40) {
695 packet->len = MAX(via->mtu, 590);
696 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
698 fragment_ipv4_packet(via, packet, ether_size);
704 clamp_mss(source, via, packet);
706 send_packet(subnet->owner, packet);
709 static void route_neighborsol(node_t *source, vpn_packet_t *packet);
711 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
712 if(!checklength(source, packet, ether_size + ip6_size)) {
716 if(DATA(packet)[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && DATA(packet)[54] == ND_NEIGHBOR_SOLICIT) {
717 route_neighborsol(source, packet);
725 memcpy(&dest, &DATA(packet)[38], sizeof(dest));
726 subnet = lookup_subnet_ipv6(&dest);
729 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
730 source->name, source->hostname,
740 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
745 route_broadcast(source, packet);
749 if(subnet->owner == source) {
750 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
754 if(!subnet->owner->status.reachable) {
755 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
759 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
760 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
764 if(decrement_ttl && source != myself && subnet->owner != myself)
765 if(!do_decrement_ttl(source, packet)) {
769 if(priorityinheritance) {
770 packet->priority = ((DATA(packet)[14] & 0x0f) << 4) | (DATA(packet)[15] >> 4);
773 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
776 logger(DEBUG_TRAFFIC, LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
780 if(directonly && subnet->owner != via) {
781 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
785 if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
786 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);
787 packet->len = MAX(via->mtu, 1294);
788 route_ipv6_unreachable(source, packet, ether_size, ICMP6_PACKET_TOO_BIG, 0);
792 clamp_mss(source, via, packet);
794 send_packet(subnet->owner, packet);
799 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
801 struct nd_neighbor_solicit ns;
802 struct nd_opt_hdr opt;
808 struct in6_addr ip6_src;
809 struct in6_addr ip6_dst;
814 if(!checklength(source, packet, ether_size + ip6_size + ns_size)) {
818 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
820 if(source != myself) {
821 logger(DEBUG_TRAFFIC, LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
825 /* Copy headers from packet to structs on the stack */
827 memcpy(&ip6, DATA(packet) + ether_size, ip6_size);
828 memcpy(&ns, DATA(packet) + ether_size + ip6_size, ns_size);
831 memcpy(&opt, DATA(packet) + ether_size + ip6_size + ns_size, opt_size);
834 /* First, snatch the source address from the neighbor solicitation packet */
837 memcpy(mymac.x, DATA(packet) + ETH_ALEN, ETH_ALEN);
840 /* Check if this is a valid neighbor solicitation request */
842 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
843 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
844 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
848 /* Create pseudo header */
850 pseudo.ip6_src = ip6.ip6_src;
851 pseudo.ip6_dst = ip6.ip6_dst;
854 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
856 pseudo.length = htonl(ns_size);
859 pseudo.next = htonl(IPPROTO_ICMPV6);
861 /* Generate checksum */
863 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
864 checksum = inet_checksum(&ns, ns_size, checksum);
867 checksum = inet_checksum(&opt, opt_size, checksum);
868 checksum = inet_checksum(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
872 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
876 /* Check if the IPv6 address exists on the VPN */
878 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
881 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
882 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
883 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
884 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
885 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
886 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
887 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
888 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
889 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
894 /* Check if it is for our own subnet */
896 if(subnet->owner == myself) {
897 return; /* silently ignore */
901 if(!do_decrement_ttl(source, packet)) {
905 /* Create neighbor advertation reply */
907 memcpy(DATA(packet), DATA(packet) + ETH_ALEN, ETH_ALEN); /* copy destination address */
908 DATA(packet)[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
910 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocol address */
911 ip6.ip6_src = ns.nd_ns_target;
914 memcpy(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, DATA(packet) + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
918 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
919 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
920 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
922 /* Create pseudo header */
924 pseudo.ip6_src = ip6.ip6_src;
925 pseudo.ip6_dst = ip6.ip6_dst;
928 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
930 pseudo.length = htonl(ns_size);
933 pseudo.next = htonl(IPPROTO_ICMPV6);
935 /* Generate checksum */
937 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
938 checksum = inet_checksum(&ns, ns_size, checksum);
941 checksum = inet_checksum(&opt, opt_size, checksum);
942 checksum = inet_checksum(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
945 ns.nd_ns_hdr.icmp6_cksum = checksum;
947 /* Copy structs on stack back to packet */
949 memcpy(DATA(packet) + ether_size, &ip6, ip6_size);
950 memcpy(DATA(packet) + ether_size + ip6_size, &ns, ns_size);
953 memcpy(DATA(packet) + ether_size + ip6_size + ns_size, &opt, opt_size);
956 send_packet(source, packet);
961 static void route_arp(node_t *source, vpn_packet_t *packet) {
962 struct ether_arp arp;
966 if(!checklength(source, packet, ether_size + arp_size)) {
970 if(source != myself) {
971 logger(DEBUG_TRAFFIC, LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
975 /* First, snatch the source address from the ARP packet */
978 memcpy(mymac.x, DATA(packet) + ETH_ALEN, ETH_ALEN);
981 /* Copy headers from packet to structs on the stack */
983 memcpy(&arp, DATA(packet) + ether_size, arp_size);
985 /* Check if this is a valid ARP request */
987 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
988 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
989 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: received unknown type ARP request");
993 /* Check if the IPv4 address exists on the VPN */
995 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
998 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
999 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
1004 /* Check if it is for our own subnet */
1006 if(subnet->owner == myself) {
1007 return; /* silently ignore */
1011 if(!do_decrement_ttl(source, packet)) {
1015 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
1016 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
1017 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
1019 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
1020 memcpy(arp.arp_sha, DATA(packet) + ETH_ALEN, ETH_ALEN); /* set source hard/proto addr */
1021 arp.arp_sha[ETH_ALEN - 1] ^= 0xFF; /* for consistency with route_packet() */
1022 arp.arp_op = htons(ARPOP_REPLY);
1024 /* Copy structs on stack back to packet */
1026 memcpy(DATA(packet) + ether_size, &arp, arp_size);
1028 send_packet(source, packet);
1031 static void route_mac(node_t *source, vpn_packet_t *packet) {
1035 /* Learn source address */
1037 if(source == myself) {
1039 memcpy(&src, &DATA(packet)[6], sizeof(src));
1043 /* Lookup destination address */
1045 memcpy(&dest, &DATA(packet)[0], sizeof(dest));
1046 subnet = lookup_subnet_mac(NULL, &dest);
1048 if(!subnet || !subnet->owner) {
1049 route_broadcast(source, packet);
1053 if(subnet->owner == source) {
1054 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
1058 if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
1062 if(decrement_ttl && source != myself && subnet->owner != myself)
1063 if(!do_decrement_ttl(source, packet)) {
1067 uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
1069 if(priorityinheritance) {
1070 if(type == ETH_P_IP && packet->len >= ether_size + ip_size) {
1071 packet->priority = DATA(packet)[15];
1072 } else if(type == ETH_P_IPV6 && packet->len >= ether_size + ip6_size) {
1073 packet->priority = ((DATA(packet)[14] & 0x0f) << 4) | (DATA(packet)[15] >> 4);
1077 // Handle packets larger than PMTU
1079 node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
1081 if(directonly && subnet->owner != via) {
1085 if(via && packet->len > via->mtu && via != myself) {
1086 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);
1087 length_t ethlen = 14;
1089 if(type == ETH_P_8021Q) {
1090 type = DATA(packet)[16] << 8 | DATA(packet)[17];
1094 if(type == ETH_P_IP && packet->len > 576 + ethlen) {
1095 if(DATA(packet)[6 + ethlen] & 0x40) {
1096 packet->len = via->mtu;
1097 route_ipv4_unreachable(source, packet, ethlen, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
1099 fragment_ipv4_packet(via, packet, ethlen);
1103 } else if(type == ETH_P_IPV6 && packet->len > 1280 + ethlen) {
1104 packet->len = via->mtu;
1105 route_ipv6_unreachable(source, packet, ethlen, ICMP6_PACKET_TOO_BIG, 0);
1110 clamp_mss(source, via, packet);
1112 send_packet(subnet->owner, packet);
1115 static void send_pcap(vpn_packet_t *packet) {
1118 for list_each(connection_t, c, connection_list) {
1119 if(!c->status.pcap) {
1124 int len = packet->len;
1126 if(c->outmaclength && c->outmaclength < len) {
1127 len = c->outmaclength;
1130 if(send_request(c, "%d %d %d", CONTROL, REQ_PCAP, len)) {
1131 send_meta(c, (char *)DATA(packet), len);
1136 void route(node_t *source, vpn_packet_t *packet) {
1141 if(forwarding_mode == FMODE_KERNEL && source != myself) {
1142 send_packet(myself, packet);
1146 if(!checklength(source, packet, ether_size)) {
1150 uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
1152 switch(routing_mode) {
1156 route_arp(source, packet);
1160 route_ipv4(source, packet);
1164 route_ipv6(source, packet);
1168 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
1175 route_mac(source, packet);
1179 route_broadcast(source, packet);