3 Copyright (C) 2000-2005 Ivo Timmermans,
4 2000-2009 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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "splay_tree.h"
26 #include "connection.h"
37 rmode_t routing_mode = RMODE_ROUTER;
38 bool priorityinheritance = false;
40 bool overwrite_mac = false;
41 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
43 /* Sizes of various headers */
45 static const size_t ether_size = sizeof(struct ether_header);
46 static const size_t arp_size = sizeof(struct ether_arp);
47 static const size_t ip_size = sizeof(struct ip);
48 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
49 static const size_t ip6_size = sizeof(struct ip6_hdr);
50 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
51 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
52 static const size_t opt_size = sizeof(struct nd_opt_hdr);
54 static struct event age_subnets_event;
58 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum)
61 uint32_t checksum = prevsum ^ 0xFFFF;
69 checksum += *(uint8_t *)p;
72 checksum = (checksum & 0xFFFF) + (checksum >> 16);
77 static bool ratelimit(int frequency) {
78 static time_t lasttime = 0;
80 time_t now = time(NULL);
83 if(++count > frequency)
93 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
94 if(packet->len < length) {
95 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got too short packet from %s (%s)"), source->name, source->hostname);
101 static void age_subnets(int fd, short events, void *data)
105 splay_node_t *node, *next, *node2;
107 time_t now = time(NULL);
111 for(node = myself->subnet_tree->head; node; node = next) {
114 if(s->expires && s->expires < now) {
116 char netstr[MAXNETSTR];
117 if(net2str(netstr, sizeof netstr, s))
118 logger(LOG_INFO, _("Subnet %s expired"), netstr);
121 for(node2 = connection_tree->head; node2; node2 = node2->next) {
124 send_del_subnet(c, s);
127 subnet_del(myself, s);
135 event_add(&age_subnets_event, &(struct timeval){10, 0});
138 static void learn_mac(mac_t *address)
146 subnet = lookup_subnet_mac(address);
148 /* If we don't know this MAC address yet, store it */
151 ifdebug(TRAFFIC) logger(LOG_INFO, _("Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx"),
152 address->x[0], address->x[1], address->x[2], address->x[3],
153 address->x[4], address->x[5]);
155 subnet = new_subnet();
156 subnet->type = SUBNET_MAC;
157 subnet->expires = time(NULL) + macexpire;
158 subnet->net.mac.address = *address;
159 subnet_add(myself, subnet);
161 /* And tell all other tinc daemons it's our MAC */
163 for(node = connection_tree->head; node; node = node->next) {
166 send_add_subnet(c, subnet);
169 if(!timeout_initialized(&age_subnets_event))
170 timeout_set(&age_subnets_event, age_subnets, NULL);
171 event_add(&age_subnets_event, &(struct timeval){10, 0});
174 subnet->expires = time(NULL) + macexpire;
178 static void route_mac(node_t *source, vpn_packet_t *packet)
186 /* Learn source address */
188 if(source == myself) {
190 memcpy(&src, &packet->data[6], sizeof src);
194 /* Lookup destination address */
196 memcpy(&dest, &packet->data[0], sizeof dest);
197 subnet = lookup_subnet_mac(&dest);
200 broadcast_packet(source, packet);
204 if(subnet->owner == source) {
205 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
209 send_packet(subnet->owner, packet);
214 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code)
217 struct icmp icmp = {0};
219 struct in_addr ip_src;
220 struct in_addr ip_dst;
228 /* Copy headers from packet into properly aligned structs on the stack */
230 memcpy(&ip, packet->data + ether_size, ip_size);
232 /* Remember original source and destination */
237 oldlen = packet->len - ether_size;
239 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
240 icmp.icmp_nextmtu = htons(packet->len - ether_size);
242 if(oldlen >= IP_MSS - ip_size - icmp_size)
243 oldlen = IP_MSS - ip_size - icmp_size;
245 /* Copy first part of original contents to ICMP message */
247 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
249 /* Fill in IPv4 header */
252 ip.ip_hl = ip_size / 4;
254 ip.ip_len = htons(ip_size + icmp_size + oldlen);
258 ip.ip_p = IPPROTO_ICMP;
263 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
265 /* Fill in ICMP header */
267 icmp.icmp_type = type;
268 icmp.icmp_code = code;
271 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
272 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
274 /* Copy structs on stack back to packet */
276 memcpy(packet->data + ether_size, &ip, ip_size);
277 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
279 packet->len = ether_size + ip_size + icmp_size + oldlen;
281 send_packet(source, packet);
286 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
288 vpn_packet_t fragment;
289 int len, maxlen, todo;
291 uint16_t ip_off, origf;
295 memcpy(&ip, packet->data + ether_size, ip_size);
296 fragment.priority = packet->priority;
298 if(ip.ip_hl != ip_size / 4)
301 todo = ntohs(ip.ip_len) - ip_size;
303 if(ether_size + ip_size + todo != packet->len) {
304 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Length of packet (%d) doesn't match length in IPv4 header (%zd)"), packet->len, ether_size + ip_size + todo);
308 ifdebug(TRAFFIC) logger(LOG_INFO, _("Fragmenting packet of %d bytes to %s (%s)"), packet->len, dest->name, dest->hostname);
310 offset = packet->data + ether_size + ip_size;
311 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
312 ip_off = ntohs(ip.ip_off);
313 origf = ip_off & ~IP_OFFMASK;
314 ip_off &= IP_OFFMASK;
317 len = todo > maxlen ? maxlen : todo;
318 memcpy(fragment.data + ether_size + ip_size, offset, len);
322 ip.ip_len = htons(ip_size + len);
323 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
325 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
326 memcpy(fragment.data, packet->data, ether_size);
327 memcpy(fragment.data + ether_size, &ip, ip_size);
328 fragment.len = ether_size + ip_size + len;
330 send_packet(dest, &fragment);
336 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet)
344 memcpy(&dest, &packet->data[30], sizeof dest);
345 subnet = lookup_subnet_ipv4(&dest);
348 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d"),
349 source->name, source->hostname,
355 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
359 if(subnet->owner == source) {
360 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
364 if(!subnet->owner->status.reachable)
365 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
367 if(priorityinheritance)
368 packet->priority = packet->data[15];
370 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
372 if(via && packet->len > via->mtu && via != myself) {
373 ifdebug(TRAFFIC) logger(LOG_INFO, _("Packet for %s (%s) length %d larger than MTU %d"), subnet->owner->name, subnet->owner->hostname, packet->len, via->mtu);
374 if(packet->data[20] & 0x40) {
375 packet->len = via->mtu;
376 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
378 fragment_ipv4_packet(via, packet);
384 send_packet(subnet->owner, packet);
387 static void route_ipv4(node_t *source, vpn_packet_t *packet)
391 if(!checklength(source, packet, ether_size + ip_size))
394 if(((packet->data[30] & 0xf0) == 0xe0) || (
395 packet->data[30] == 255 &&
396 packet->data[31] == 255 &&
397 packet->data[32] == 255 &&
398 packet->data[33] == 255))
399 broadcast_packet(source, packet);
401 route_ipv4_unicast(source, packet);
406 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code)
409 struct icmp6_hdr icmp6 = {0};
413 struct in6_addr ip6_src; /* source address */
414 struct in6_addr ip6_dst; /* destination address */
424 /* Copy headers from packet to structs on the stack */
426 memcpy(&ip6, packet->data + ether_size, ip6_size);
428 /* Remember original source and destination */
430 pseudo.ip6_src = ip6.ip6_dst;
431 pseudo.ip6_dst = ip6.ip6_src;
433 pseudo.length = packet->len - ether_size;
435 if(type == ICMP6_PACKET_TOO_BIG)
436 icmp6.icmp6_mtu = htonl(pseudo.length);
438 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
439 pseudo.length = IP_MSS - ip6_size - icmp6_size;
441 /* Copy first part of original contents to ICMP message */
443 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
445 /* Fill in IPv6 header */
447 ip6.ip6_flow = htonl(0x60000000UL);
448 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
449 ip6.ip6_nxt = IPPROTO_ICMPV6;
451 ip6.ip6_src = pseudo.ip6_src;
452 ip6.ip6_dst = pseudo.ip6_dst;
454 /* Fill in ICMP header */
456 icmp6.icmp6_type = type;
457 icmp6.icmp6_code = code;
458 icmp6.icmp6_cksum = 0;
460 /* Create pseudo header */
462 pseudo.length = htonl(icmp6_size + pseudo.length);
463 pseudo.next = htonl(IPPROTO_ICMPV6);
465 /* Generate checksum */
467 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
468 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
469 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
471 icmp6.icmp6_cksum = checksum;
473 /* Copy structs on stack back to packet */
475 memcpy(packet->data + ether_size, &ip6, ip6_size);
476 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
478 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
480 send_packet(source, packet);
483 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet)
491 memcpy(&dest, &packet->data[38], sizeof dest);
492 subnet = lookup_subnet_ipv6(&dest);
495 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
496 source->name, source->hostname,
506 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
510 if(subnet->owner == source) {
511 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
515 if(!subnet->owner->status.reachable)
516 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
518 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
520 if(via && packet->len > via->mtu && via != myself) {
521 ifdebug(TRAFFIC) logger(LOG_INFO, _("Packet for %s (%s) length %d larger than MTU %d"), subnet->owner->name, subnet->owner->hostname, packet->len, via->mtu);
522 packet->len = via->mtu;
523 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
527 send_packet(subnet->owner, packet);
532 static void route_neighborsol(node_t *source, vpn_packet_t *packet)
535 struct nd_neighbor_solicit ns;
536 struct nd_opt_hdr opt;
542 struct in6_addr ip6_src; /* source address */
543 struct in6_addr ip6_dst; /* destination address */
550 if(!checklength(source, packet, ether_size + ip6_size + ns_size))
553 has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
555 if(source != myself) {
556 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got neighbor solicitation request from %s (%s) while in router mode!"), source->name, source->hostname);
560 /* Copy headers from packet to structs on the stack */
562 memcpy(&ip6, packet->data + ether_size, ip6_size);
563 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
565 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
567 /* First, snatch the source address from the neighbor solicitation packet */
570 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
572 /* Check if this is a valid neighbor solicitation request */
574 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
575 (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
576 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
580 /* Create pseudo header */
582 pseudo.ip6_src = ip6.ip6_src;
583 pseudo.ip6_dst = ip6.ip6_dst;
585 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
587 pseudo.length = htonl(ns_size);
588 pseudo.next = htonl(IPPROTO_ICMPV6);
590 /* Generate checksum */
592 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
593 checksum = inet_checksum(&ns, ns_size, checksum);
595 checksum = inet_checksum(&opt, opt_size, checksum);
596 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
600 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
604 /* Check if the IPv6 address exists on the VPN */
606 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
609 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
610 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
611 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
612 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
613 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
614 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
615 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
616 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
617 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
622 /* Check if it is for our own subnet */
624 if(subnet->owner == myself)
625 return; /* silently ignore */
627 /* Create neighbor advertation reply */
629 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
630 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
632 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
633 ip6.ip6_src = ns.nd_ns_target;
636 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
639 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
640 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
641 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
643 /* Create pseudo header */
645 pseudo.ip6_src = ip6.ip6_src;
646 pseudo.ip6_dst = ip6.ip6_dst;
648 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
650 pseudo.length = htonl(ns_size);
651 pseudo.next = htonl(IPPROTO_ICMPV6);
653 /* Generate checksum */
655 checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
656 checksum = inet_checksum(&ns, ns_size, checksum);
658 checksum = inet_checksum(&opt, opt_size, checksum);
659 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
662 ns.nd_ns_hdr.icmp6_cksum = checksum;
664 /* Copy structs on stack back to packet */
666 memcpy(packet->data + ether_size, &ip6, ip6_size);
667 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
669 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
671 send_packet(source, packet);
674 static void route_ipv6(node_t *source, vpn_packet_t *packet)
678 if(!checklength(source, packet, ether_size + ip6_size))
681 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
682 route_neighborsol(source, packet);
686 if(packet->data[38] == 255)
687 broadcast_packet(source, packet);
689 route_ipv6_unicast(source, packet);
694 static void route_arp(node_t *source, vpn_packet_t *packet)
696 struct ether_arp arp;
702 if(!checklength(source, packet, ether_size + arp_size))
705 if(source != myself) {
706 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got ARP request from %s (%s) while in router mode!"), source->name, source->hostname);
710 /* First, snatch the source address from the ARP packet */
713 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
715 /* Copy headers from packet to structs on the stack */
717 memcpy(&arp, packet->data + ether_size, arp_size);
719 /* Check if this is a valid ARP request */
721 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
722 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof addr || ntohs(arp.arp_op) != ARPOP_REQUEST) {
723 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
727 /* Check if the IPv4 address exists on the VPN */
729 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
732 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
733 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
738 /* Check if it is for our own subnet */
740 if(subnet->owner == myself)
741 return; /* silently ignore */
743 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
744 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
746 memcpy(&addr, arp.arp_tpa, sizeof addr); /* save protocol addr */
747 memcpy(arp.arp_tpa, arp.arp_spa, sizeof addr); /* swap destination and source protocol address */
748 memcpy(arp.arp_spa, &addr, sizeof addr); /* ... */
750 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
751 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
752 arp.arp_op = htons(ARPOP_REPLY);
754 /* Copy structs on stack back to packet */
756 memcpy(packet->data + ether_size, &arp, arp_size);
758 send_packet(source, packet);
761 void route(node_t *source, vpn_packet_t *packet)
765 if(!checklength(source, packet, ether_size))
768 switch (routing_mode) {
773 type = ntohs(*((uint16_t *)(&packet->data[12])));
776 route_arp(source, packet);
780 route_ipv4(source, packet);
784 route_ipv6(source, packet);
788 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown type %hx"), source->name, source->hostname, type);
795 route_mac(source, packet);
799 broadcast_packet(source, packet);