3 Copyright (C) 2000-2005 Ivo Timmermans,
4 2000-2006 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.
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);
56 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum)
59 uint32_t checksum = prevsum ^ 0xFFFF;
67 checksum += *(uint8_t *)p;
70 checksum = (checksum & 0xFFFF) + (checksum >> 16);
75 static bool ratelimit(int frequency) {
76 static time_t lasttime = 0;
80 if(++count > frequency)
90 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
91 if(packet->len < length) {
92 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got too short packet from %s (%s)"), source->name, source->hostname);
98 static void learn_mac(mac_t *address)
106 subnet = lookup_subnet_mac(address);
108 /* If we don't know this MAC address yet, store it */
111 ifdebug(TRAFFIC) logger(LOG_INFO, _("Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx"),
112 address->x[0], address->x[1], address->x[2], address->x[3],
113 address->x[4], address->x[5]);
115 subnet = new_subnet();
116 subnet->type = SUBNET_MAC;
117 subnet->expires = now + macexpire;
118 subnet->net.mac.address = *address;
119 subnet_add(myself, subnet);
121 /* And tell all other tinc daemons it's our MAC */
123 for(node = connection_tree->head; node; node = node->next) {
126 send_add_subnet(c, subnet);
131 subnet->expires = now + macexpire;
134 void age_subnets(void)
138 avl_node_t *node, *next, *node2;
142 for(node = myself->subnet_tree->head; node; node = next) {
145 if(s->expires && s->expires < now) {
147 char netstr[MAXNETSTR];
148 if(net2str(netstr, sizeof netstr, s))
149 logger(LOG_INFO, _("Subnet %s expired"), netstr);
152 for(node2 = connection_tree->head; node2; node2 = node2->next) {
155 send_del_subnet(c, s);
158 subnet_del(myself, s);
163 static void route_mac(node_t *source, vpn_packet_t *packet)
171 /* Learn source address */
173 if(source == myself) {
175 memcpy(&src, &packet->data[6], sizeof src);
179 /* Lookup destination address */
181 memcpy(&dest, &packet->data[0], sizeof dest);
182 subnet = lookup_subnet_mac(&dest);
185 broadcast_packet(source, packet);
189 if(subnet->owner == source) {
190 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
194 send_packet(subnet->owner, packet);
199 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code)
202 struct icmp icmp = {0};
204 struct in_addr ip_src;
205 struct in_addr ip_dst;
213 /* Copy headers from packet into properly aligned structs on the stack */
215 memcpy(&ip, packet->data + ether_size, ip_size);
217 /* Remember original source and destination */
222 oldlen = packet->len - ether_size;
224 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
225 icmp.icmp_nextmtu = htons(packet->len - ether_size);
227 if(oldlen >= IP_MSS - ip_size - icmp_size)
228 oldlen = IP_MSS - ip_size - icmp_size;
230 /* Copy first part of original contents to ICMP message */
232 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
234 /* Fill in IPv4 header */
237 ip.ip_hl = ip_size / 4;
239 ip.ip_len = htons(ip_size + icmp_size + oldlen);
243 ip.ip_p = IPPROTO_ICMP;
248 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
250 /* Fill in ICMP header */
252 icmp.icmp_type = type;
253 icmp.icmp_code = code;
256 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
257 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
259 /* Copy structs on stack back to packet */
261 memcpy(packet->data + ether_size, &ip, ip_size);
262 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
264 packet->len = ether_size + ip_size + icmp_size + oldlen;
266 send_packet(source, packet);
271 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
273 vpn_packet_t fragment;
274 int len, maxlen, todo;
276 uint16_t ip_off, origf;
280 memcpy(&ip, packet->data + ether_size, ip_size);
281 fragment.priority = packet->priority;
283 if(ip.ip_hl != ip_size / 4)
286 todo = ntohs(ip.ip_len) - ip_size;
288 if(ether_size + ip_size + todo != packet->len) {
289 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Length of packet (%d) doesn't match length in IPv4 header (%zd)"), packet->len, ether_size + ip_size + todo);
293 ifdebug(TRAFFIC) logger(LOG_INFO, _("Fragmenting packet of %d bytes to %s (%s)"), packet->len, dest->name, dest->hostname);
295 offset = packet->data + ether_size + ip_size;
296 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
297 ip_off = ntohs(ip.ip_off);
298 origf = ip_off & ~IP_OFFMASK;
299 ip_off &= IP_OFFMASK;
302 len = todo > maxlen ? maxlen : todo;
303 memcpy(fragment.data + ether_size + ip_size, offset, len);
307 ip.ip_len = htons(ip_size + len);
308 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
310 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
311 memcpy(fragment.data, packet->data, ether_size);
312 memcpy(fragment.data + ether_size, &ip, ip_size);
313 fragment.len = ether_size + ip_size + len;
315 send_packet(dest, &fragment);
321 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet)
329 memcpy(&dest, &packet->data[30], sizeof dest);
330 subnet = lookup_subnet_ipv4(&dest);
333 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d"),
334 source->name, source->hostname,
340 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
344 if(subnet->owner == source) {
345 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
349 if(!subnet->owner->status.reachable)
350 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
352 if(priorityinheritance)
353 packet->priority = packet->data[15];
355 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
357 if(via && packet->len > via->mtu && via != myself) {
358 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);
359 if(packet->data[20] & 0x40) {
360 packet->len = via->mtu;
361 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
363 fragment_ipv4_packet(via, packet);
369 send_packet(subnet->owner, packet);
372 static void route_ipv4(node_t *source, vpn_packet_t *packet)
376 if(!checklength(source, packet, ether_size + ip_size))
379 route_ipv4_unicast(source, packet);
384 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code)
387 struct icmp6_hdr icmp6 = {0};
391 struct in6_addr ip6_src; /* source address */
392 struct in6_addr ip6_dst; /* destination address */
402 /* Copy headers from packet to structs on the stack */
404 memcpy(&ip6, packet->data + ether_size, ip6_size);
406 /* Remember original source and destination */
408 pseudo.ip6_src = ip6.ip6_dst;
409 pseudo.ip6_dst = ip6.ip6_src;
411 pseudo.length = packet->len - ether_size;
413 if(type == ICMP6_PACKET_TOO_BIG)
414 icmp6.icmp6_mtu = htonl(pseudo.length);
416 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
417 pseudo.length = IP_MSS - ip6_size - icmp6_size;
419 /* Copy first part of original contents to ICMP message */
421 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
423 /* Fill in IPv6 header */
425 ip6.ip6_flow = htonl(0x60000000UL);
426 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
427 ip6.ip6_nxt = IPPROTO_ICMPV6;
429 ip6.ip6_src = pseudo.ip6_src;
430 ip6.ip6_dst = pseudo.ip6_dst;
432 /* Fill in ICMP header */
434 icmp6.icmp6_type = type;
435 icmp6.icmp6_code = code;
436 icmp6.icmp6_cksum = 0;
438 /* Create pseudo header */
440 pseudo.length = htonl(icmp6_size + pseudo.length);
441 pseudo.next = htonl(IPPROTO_ICMPV6);
443 /* Generate checksum */
445 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
446 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
447 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
449 icmp6.icmp6_cksum = checksum;
451 /* Copy structs on stack back to packet */
453 memcpy(packet->data + ether_size, &ip6, ip6_size);
454 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
456 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
458 send_packet(source, packet);
461 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet)
469 memcpy(&dest, &packet->data[38], sizeof dest);
470 subnet = lookup_subnet_ipv6(&dest);
473 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
474 source->name, source->hostname,
484 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
488 if(subnet->owner == source) {
489 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
493 if(!subnet->owner->status.reachable)
494 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
496 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
498 if(via && packet->len > via->mtu && via != myself) {
499 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);
500 packet->len = via->mtu;
501 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
505 send_packet(subnet->owner, packet);
510 static void route_neighborsol(node_t *source, vpn_packet_t *packet)
513 struct nd_neighbor_solicit ns;
514 struct nd_opt_hdr opt;
519 struct in6_addr ip6_src; /* source address */
520 struct in6_addr ip6_dst; /* destination address */
527 if(!checklength(source, packet, ether_size + ip6_size + ns_size + opt_size + ETH_ALEN))
530 if(source != myself) {
531 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got neighbor solicitation request from %s (%s) while in router mode!"), source->name, source->hostname);
535 /* Copy headers from packet to structs on the stack */
537 memcpy(&ip6, packet->data + ether_size, ip6_size);
538 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
539 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
541 /* First, snatch the source address from the neighbor solicitation packet */
544 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
546 /* Check if this is a valid neighbor solicitation request */
548 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
549 opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR) {
550 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
554 /* Create pseudo header */
556 pseudo.ip6_src = ip6.ip6_src;
557 pseudo.ip6_dst = ip6.ip6_dst;
558 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
559 pseudo.next = htonl(IPPROTO_ICMPV6);
561 /* Generate checksum */
563 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
564 checksum = inet_checksum(&ns, ns_size, checksum);
565 checksum = inet_checksum(&opt, opt_size, checksum);
566 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
569 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
573 /* Check if the IPv6 address exists on the VPN */
575 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
578 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
579 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
580 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
581 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
582 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
583 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
584 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
585 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
586 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
591 /* Check if it is for our own subnet */
593 if(subnet->owner == myself)
594 return; /* silently ignore */
596 /* Create neighbor advertation reply */
598 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
599 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
601 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
602 ip6.ip6_src = ns.nd_ns_target;
604 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
607 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
608 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
609 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
611 /* Create pseudo header */
613 pseudo.ip6_src = ip6.ip6_src;
614 pseudo.ip6_dst = ip6.ip6_dst;
615 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
616 pseudo.next = htonl(IPPROTO_ICMPV6);
618 /* Generate checksum */
620 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
621 checksum = inet_checksum(&ns, ns_size, checksum);
622 checksum = inet_checksum(&opt, opt_size, checksum);
623 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
625 ns.nd_ns_hdr.icmp6_cksum = checksum;
627 /* Copy structs on stack back to packet */
629 memcpy(packet->data + ether_size, &ip6, ip6_size);
630 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
631 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
633 send_packet(source, packet);
636 static void route_ipv6(node_t *source, vpn_packet_t *packet)
640 if(!checklength(source, packet, ether_size + ip6_size))
643 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
644 route_neighborsol(source, packet);
648 route_ipv6_unicast(source, packet);
653 static void route_arp(node_t *source, vpn_packet_t *packet)
655 struct ether_arp arp;
661 if(!checklength(source, packet, ether_size + arp_size))
664 if(source != myself) {
665 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got ARP request from %s (%s) while in router mode!"), source->name, source->hostname);
669 /* First, snatch the source address from the ARP packet */
672 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
674 /* Copy headers from packet to structs on the stack */
676 memcpy(&arp, packet->data + ether_size, arp_size);
678 /* Check if this is a valid ARP request */
680 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
681 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
682 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
686 /* Check if the IPv4 address exists on the VPN */
688 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
691 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
692 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
697 /* Check if it is for our own subnet */
699 if(subnet->owner == myself)
700 return; /* silently ignore */
702 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
703 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
705 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
706 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
707 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
709 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
710 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
711 arp.arp_op = htons(ARPOP_REPLY);
713 /* Copy structs on stack back to packet */
715 memcpy(packet->data + ether_size, &arp, arp_size);
717 send_packet(source, packet);
720 void route(node_t *source, vpn_packet_t *packet)
724 if(!checklength(source, packet, ether_size))
727 switch (routing_mode) {
732 type = ntohs(*((uint16_t *)(&packet->data[12])));
735 route_arp(source, packet);
739 route_ipv4(source, packet);
743 route_ipv6(source, packet);
747 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown type %hx"), source->name, source->hostname, type);
754 route_mac(source, packet);
758 broadcast_packet(source, packet);