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);
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;
82 if(++count > frequency)
92 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
93 if(packet->len < length) {
94 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got too short packet from %s (%s)"), source->name, source->hostname);
100 static void age_subnets(int fd, short events, void *data)
104 avl_node_t *node, *next, *node2;
109 for(node = myself->subnet_tree->head; node; node = next) {
112 if(s->expires && s->expires < now) {
114 char netstr[MAXNETSTR];
115 if(net2str(netstr, sizeof netstr, s))
116 logger(LOG_INFO, _("Subnet %s expired"), netstr);
119 for(node2 = connection_tree->head; node2; node2 = node2->next) {
122 send_del_subnet(c, s);
125 subnet_del(myself, s);
133 event_add(&age_subnets_event, &(struct timeval){10, 0});
136 static void learn_mac(mac_t *address)
144 subnet = lookup_subnet_mac(address);
146 /* If we don't know this MAC address yet, store it */
149 ifdebug(TRAFFIC) logger(LOG_INFO, _("Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx"),
150 address->x[0], address->x[1], address->x[2], address->x[3],
151 address->x[4], address->x[5]);
153 subnet = new_subnet();
154 subnet->type = SUBNET_MAC;
155 subnet->expires = now + macexpire;
156 subnet->net.mac.address = *address;
157 subnet_add(myself, subnet);
159 /* And tell all other tinc daemons it's our MAC */
161 for(node = connection_tree->head; node; node = node->next) {
164 send_add_subnet(c, subnet);
167 if(!timeout_initialized(&age_subnets_event))
168 timeout_set(&age_subnets_event, age_subnets, NULL);
169 event_add(&age_subnets_event, &(struct timeval){10, 0});
173 subnet->expires = now + macexpire;
176 static void route_mac(node_t *source, vpn_packet_t *packet)
184 /* Learn source address */
186 if(source == myself) {
188 memcpy(&src, &packet->data[6], sizeof src);
192 /* Lookup destination address */
194 memcpy(&dest, &packet->data[0], sizeof dest);
195 subnet = lookup_subnet_mac(&dest);
198 broadcast_packet(source, packet);
202 if(subnet->owner == source) {
203 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
207 send_packet(subnet->owner, packet);
212 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code)
215 struct icmp icmp = {0};
217 struct in_addr ip_src;
218 struct in_addr ip_dst;
226 /* Copy headers from packet into properly aligned structs on the stack */
228 memcpy(&ip, packet->data + ether_size, ip_size);
230 /* Remember original source and destination */
235 oldlen = packet->len - ether_size;
237 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
238 icmp.icmp_nextmtu = htons(packet->len - ether_size);
240 if(oldlen >= IP_MSS - ip_size - icmp_size)
241 oldlen = IP_MSS - ip_size - icmp_size;
243 /* Copy first part of original contents to ICMP message */
245 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
247 /* Fill in IPv4 header */
250 ip.ip_hl = ip_size / 4;
252 ip.ip_len = htons(ip_size + icmp_size + oldlen);
256 ip.ip_p = IPPROTO_ICMP;
261 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
263 /* Fill in ICMP header */
265 icmp.icmp_type = type;
266 icmp.icmp_code = code;
269 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
270 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
272 /* Copy structs on stack back to packet */
274 memcpy(packet->data + ether_size, &ip, ip_size);
275 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
277 packet->len = ether_size + ip_size + icmp_size + oldlen;
279 send_packet(source, packet);
284 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
286 vpn_packet_t fragment;
287 int len, maxlen, todo;
289 uint16_t ip_off, origf;
293 memcpy(&ip, packet->data + ether_size, ip_size);
294 fragment.priority = packet->priority;
296 if(ip.ip_hl != ip_size / 4)
299 todo = ntohs(ip.ip_len) - ip_size;
301 if(ether_size + ip_size + todo != packet->len) {
302 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Length of packet (%d) doesn't match length in IPv4 header (%zd)"), packet->len, ether_size + ip_size + todo);
306 ifdebug(TRAFFIC) logger(LOG_INFO, _("Fragmenting packet of %d bytes to %s (%s)"), packet->len, dest->name, dest->hostname);
308 offset = packet->data + ether_size + ip_size;
309 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
310 ip_off = ntohs(ip.ip_off);
311 origf = ip_off & ~IP_OFFMASK;
312 ip_off &= IP_OFFMASK;
315 len = todo > maxlen ? maxlen : todo;
316 memcpy(fragment.data + ether_size + ip_size, offset, len);
320 ip.ip_len = htons(ip_size + len);
321 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
323 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
324 memcpy(fragment.data, packet->data, ether_size);
325 memcpy(fragment.data + ether_size, &ip, ip_size);
326 fragment.len = ether_size + ip_size + len;
328 send_packet(dest, &fragment);
334 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet)
342 memcpy(&dest, &packet->data[30], sizeof dest);
343 subnet = lookup_subnet_ipv4(&dest);
346 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d"),
347 source->name, source->hostname,
353 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
357 if(subnet->owner == source) {
358 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
362 if(!subnet->owner->status.reachable)
363 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
365 if(priorityinheritance)
366 packet->priority = packet->data[15];
368 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
370 if(via && packet->len > via->mtu && via != myself) {
371 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);
372 if(packet->data[20] & 0x40) {
373 packet->len = via->mtu;
374 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
376 fragment_ipv4_packet(via, packet);
382 send_packet(subnet->owner, packet);
385 static void route_ipv4(node_t *source, vpn_packet_t *packet)
389 if(!checklength(source, packet, ether_size + ip_size))
392 route_ipv4_unicast(source, packet);
397 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code)
400 struct icmp6_hdr icmp6 = {0};
404 struct in6_addr ip6_src; /* source address */
405 struct in6_addr ip6_dst; /* destination address */
415 /* Copy headers from packet to structs on the stack */
417 memcpy(&ip6, packet->data + ether_size, ip6_size);
419 /* Remember original source and destination */
421 pseudo.ip6_src = ip6.ip6_dst;
422 pseudo.ip6_dst = ip6.ip6_src;
424 pseudo.length = packet->len - ether_size;
426 if(type == ICMP6_PACKET_TOO_BIG)
427 icmp6.icmp6_mtu = htonl(pseudo.length);
429 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
430 pseudo.length = IP_MSS - ip6_size - icmp6_size;
432 /* Copy first part of original contents to ICMP message */
434 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
436 /* Fill in IPv6 header */
438 ip6.ip6_flow = htonl(0x60000000UL);
439 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
440 ip6.ip6_nxt = IPPROTO_ICMPV6;
442 ip6.ip6_src = pseudo.ip6_src;
443 ip6.ip6_dst = pseudo.ip6_dst;
445 /* Fill in ICMP header */
447 icmp6.icmp6_type = type;
448 icmp6.icmp6_code = code;
449 icmp6.icmp6_cksum = 0;
451 /* Create pseudo header */
453 pseudo.length = htonl(icmp6_size + pseudo.length);
454 pseudo.next = htonl(IPPROTO_ICMPV6);
456 /* Generate checksum */
458 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
459 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
460 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
462 icmp6.icmp6_cksum = checksum;
464 /* Copy structs on stack back to packet */
466 memcpy(packet->data + ether_size, &ip6, ip6_size);
467 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
469 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
471 send_packet(source, packet);
474 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet)
482 memcpy(&dest, &packet->data[38], sizeof dest);
483 subnet = lookup_subnet_ipv6(&dest);
486 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
487 source->name, source->hostname,
497 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
501 if(subnet->owner == source) {
502 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
506 if(!subnet->owner->status.reachable)
507 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
509 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
511 if(via && packet->len > via->mtu && via != myself) {
512 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);
513 packet->len = via->mtu;
514 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
518 send_packet(subnet->owner, packet);
523 static void route_neighborsol(node_t *source, vpn_packet_t *packet)
526 struct nd_neighbor_solicit ns;
527 struct nd_opt_hdr opt;
532 struct in6_addr ip6_src; /* source address */
533 struct in6_addr ip6_dst; /* destination address */
540 if(!checklength(source, packet, ether_size + ip6_size + ns_size + opt_size + ETH_ALEN))
543 if(source != myself) {
544 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got neighbor solicitation request from %s (%s) while in router mode!"), source->name, source->hostname);
548 /* Copy headers from packet to structs on the stack */
550 memcpy(&ip6, packet->data + ether_size, ip6_size);
551 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
552 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
554 /* First, snatch the source address from the neighbor solicitation packet */
557 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
559 /* Check if this is a valid neighbor solicitation request */
561 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
562 opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR) {
563 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
567 /* Create pseudo header */
569 pseudo.ip6_src = ip6.ip6_src;
570 pseudo.ip6_dst = ip6.ip6_dst;
571 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
572 pseudo.next = htonl(IPPROTO_ICMPV6);
574 /* Generate checksum */
576 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
577 checksum = inet_checksum(&ns, ns_size, checksum);
578 checksum = inet_checksum(&opt, opt_size, checksum);
579 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
582 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
586 /* Check if the IPv6 address exists on the VPN */
588 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
591 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
592 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
593 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
594 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
595 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
596 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
597 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
598 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
599 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
604 /* Check if it is for our own subnet */
606 if(subnet->owner == myself)
607 return; /* silently ignore */
609 /* Create neighbor advertation reply */
611 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
612 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
614 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
615 ip6.ip6_src = ns.nd_ns_target;
617 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
620 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
621 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
622 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
624 /* Create pseudo header */
626 pseudo.ip6_src = ip6.ip6_src;
627 pseudo.ip6_dst = ip6.ip6_dst;
628 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
629 pseudo.next = htonl(IPPROTO_ICMPV6);
631 /* Generate checksum */
633 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
634 checksum = inet_checksum(&ns, ns_size, checksum);
635 checksum = inet_checksum(&opt, opt_size, checksum);
636 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
638 ns.nd_ns_hdr.icmp6_cksum = checksum;
640 /* Copy structs on stack back to packet */
642 memcpy(packet->data + ether_size, &ip6, ip6_size);
643 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
644 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
646 send_packet(source, packet);
649 static void route_ipv6(node_t *source, vpn_packet_t *packet)
653 if(!checklength(source, packet, ether_size + ip6_size))
656 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
657 route_neighborsol(source, packet);
661 route_ipv6_unicast(source, packet);
666 static void route_arp(node_t *source, vpn_packet_t *packet)
668 struct ether_arp arp;
674 if(!checklength(source, packet, ether_size + arp_size))
677 if(source != myself) {
678 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got ARP request from %s (%s) while in router mode!"), source->name, source->hostname);
682 /* First, snatch the source address from the ARP packet */
685 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
687 /* Copy headers from packet to structs on the stack */
689 memcpy(&arp, packet->data + ether_size, arp_size);
691 /* Check if this is a valid ARP request */
693 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
694 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
695 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
699 /* Check if the IPv4 address exists on the VPN */
701 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
704 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
705 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
710 /* Check if it is for our own subnet */
712 if(subnet->owner == myself)
713 return; /* silently ignore */
715 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
716 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
718 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
719 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
720 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
722 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
723 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
724 arp.arp_op = htons(ARPOP_REPLY);
726 /* Copy structs on stack back to packet */
728 memcpy(packet->data + ether_size, &arp, arp_size);
730 send_packet(source, packet);
733 void route(node_t *source, vpn_packet_t *packet)
737 if(!checklength(source, packet, ether_size))
740 switch (routing_mode) {
745 type = ntohs(*((uint16_t *)(&packet->data[12])));
748 route_arp(source, packet);
752 route_ipv4(source, packet);
756 route_ipv6(source, packet);
760 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown type %hx"), source->name, source->hostname, type);
767 route_mac(source, packet);
771 broadcast_packet(source, packet);