3 Copyright (C) 2000-2003 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2003 Guus Sliepen <guus@sliepen.eu.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.
20 $Id: route.c,v 1.1.2.75 2003/12/24 10:48:15 guus Exp $
25 #ifdef HAVE_NET_ETHERNET_H
26 #include <net/ethernet.h>
28 #ifdef HAVE_NET_IF_ARP_H
29 #include <net/if_arp.h>
31 #ifdef HAVE_NETINET_IP_ICMP_H
32 #include <netinet/ip_icmp.h>
34 #ifdef HAVE_NETINET_ICMP6_H
35 #include <netinet/icmp6.h>
37 #ifdef HAVE_NETINET_IF_ETHER_H
38 #include <netinet/if_ether.h>
42 #include "connection.h"
53 rmode_t routing_mode = RMODE_ROUTER;
54 bool priorityinheritance = false;
56 bool overwrite_mac = false;
57 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
59 /* Sizes of various headers */
61 static const size_t ether_size = sizeof(struct ether_header);
62 static const size_t arp_size = sizeof(struct ether_arp);
63 static const size_t ip_size = sizeof(struct ip);
64 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
65 static const size_t ip6_size = sizeof(struct ip6_hdr);
66 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
67 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
68 static const size_t opt_size = sizeof(struct nd_opt_hdr);
72 static __inline__ uint16_t inet_checksum(void *data, int len, uint16_t prevsum)
75 uint32_t checksum = prevsum ^ 0xFFFF;
83 checksum += *(uint8_t *)p;
86 checksum = (checksum & 0xFFFF) + (checksum >> 16);
91 static __inline__ bool ratelimit(int frequency) {
92 static time_t lasttime = 0;
96 if(++count > frequency)
106 static __inline__ bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
107 if(packet->len < length) {
108 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got too short packet from %s (%s)"), source->name, source->hostname);
114 static __inline__ void learn_mac(mac_t *address)
122 subnet = lookup_subnet_mac(address);
124 /* If we don't know this MAC address yet, store it */
127 ifdebug(TRAFFIC) logger(LOG_INFO, _("Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx"),
128 address->x[0], address->x[1], address->x[2], address->x[3],
129 address->x[4], address->x[5]);
131 subnet = new_subnet();
132 subnet->type = SUBNET_MAC;
133 subnet->expires = now + macexpire;
134 subnet->net.mac.address = *address;
135 subnet_add(myself, subnet);
137 /* And tell all other tinc daemons it's our MAC */
139 for(node = connection_tree->head; node; node = node->next) {
142 send_add_subnet(c, subnet);
147 subnet->expires = now + macexpire;
150 void age_subnets(void)
154 avl_node_t *node, *next, *node2;
158 for(node = myself->subnet_tree->head; node; node = next) {
161 if(s->expires && s->expires < now) {
163 char netstr[MAXNETSTR];
164 if(net2str(netstr, sizeof netstr, s))
165 logger(LOG_INFO, _("Subnet %s expired"), netstr);
168 for(node2 = connection_tree->head; node2; node2 = node2->next) {
171 send_del_subnet(c, s);
174 subnet_del(myself, s);
179 static __inline__ void route_mac(node_t *source, vpn_packet_t *packet)
185 /* Learn source address */
188 learn_mac((mac_t *)(&packet->data[6]));
190 /* Lookup destination address */
192 subnet = lookup_subnet_mac((mac_t *)(&packet->data[0]));
195 broadcast_packet(source, packet);
199 if(subnet->owner == source) {
200 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
204 send_packet(subnet->owner, packet);
209 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code)
212 struct icmp icmp = {0};
214 struct in_addr ip_src;
215 struct in_addr ip_dst;
223 /* Copy headers from packet into properly aligned structs on the stack */
225 memcpy(&ip, packet->data + ether_size, ip_size);
227 /* Remember original source and destination */
232 oldlen = packet->len - ether_size;
234 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
235 icmp.icmp_nextmtu = htons(packet->len - ether_size);
237 if(oldlen >= IP_MSS - ip_size - icmp_size)
238 oldlen = IP_MSS - ip_size - icmp_size;
240 /* Copy first part of original contents to ICMP message */
242 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
244 /* Fill in IPv4 header */
247 ip.ip_hl = ip_size / 4;
249 ip.ip_len = htons(ip_size + icmp_size + oldlen);
253 ip.ip_p = IPPROTO_ICMP;
258 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
260 /* Fill in ICMP header */
262 icmp.icmp_type = type;
263 icmp.icmp_code = code;
266 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
267 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
269 /* Copy structs on stack back to packet */
271 memcpy(packet->data + ether_size, &ip, ip_size);
272 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
274 packet->len = ether_size + ip_size + icmp_size + oldlen;
276 send_packet(source, packet);
281 static __inline__ void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
283 vpn_packet_t fragment;
284 int len, maxlen, todo;
286 uint16_t ip_off, origf;
290 memcpy(&ip, packet->data + ether_size, ip_size);
291 fragment.priority = packet->priority;
293 if(ip.ip_hl != ip_size / 4)
296 todo = ntohs(ip.ip_len) - ip_size;
298 if(ether_size + ip_size + todo != packet->len) {
299 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Length of packet (%d) doesn't match length in IPv4 header (%d)"), packet->len, ether_size + ip_size + todo);
303 ifdebug(TRAFFIC) logger(LOG_INFO, _("Fragmenting packet of %d bytes to %s (%s)"), packet->len, dest->name, dest->hostname);
305 offset = packet->data + ether_size + ip_size;
306 maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
307 ip_off = ntohs(ip.ip_off);
308 origf = ip_off & ~IP_OFFMASK;
309 ip_off &= IP_OFFMASK;
312 len = todo > maxlen ? maxlen : todo;
313 memcpy(fragment.data + ether_size + ip_size, offset, len);
317 ip.ip_len = htons(ip_size + len);
318 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
320 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
321 memcpy(fragment.data, packet->data, ether_size);
322 memcpy(fragment.data + ether_size, &ip, ip_size);
323 fragment.len = ether_size + ip_size + len;
325 send_packet(dest, &fragment);
331 static __inline__ void route_ipv4_unicast(node_t *source, vpn_packet_t *packet)
338 subnet = lookup_subnet_ipv4((ipv4_t *) &packet->data[30]);
341 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d"),
342 source->name, source->hostname,
348 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
352 if(subnet->owner == source) {
353 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
357 if(!subnet->owner->status.reachable)
358 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
360 if(priorityinheritance)
361 packet->priority = packet->data[15];
363 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
365 if(packet->len > via->mtu && via != myself) {
366 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);
367 if(packet->data[20] & 0x40) {
368 packet->len = via->mtu;
369 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
371 fragment_ipv4_packet(via, packet);
377 send_packet(subnet->owner, packet);
380 static __inline__ void route_ipv4(node_t *source, vpn_packet_t *packet)
384 if(!checklength(source, packet, ether_size + ip_size))
387 route_ipv4_unicast(source, packet);
392 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code)
395 struct icmp6_hdr icmp6 = {0};
399 struct in6_addr ip6_src; /* source address */
400 struct in6_addr ip6_dst; /* destination address */
410 /* Copy headers from packet to structs on the stack */
412 memcpy(&ip6, packet->data + ether_size, ip6_size);
414 /* Remember original source and destination */
416 pseudo.ip6_src = ip6.ip6_dst;
417 pseudo.ip6_dst = ip6.ip6_src;
419 pseudo.length = packet->len - ether_size;
421 if(type == ICMP6_PACKET_TOO_BIG)
422 icmp6.icmp6_mtu = htonl(pseudo.length);
424 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
425 pseudo.length = IP_MSS - ip6_size - icmp6_size;
427 /* Copy first part of original contents to ICMP message */
429 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
431 /* Fill in IPv6 header */
433 ip6.ip6_flow = htonl(0x60000000UL);
434 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
435 ip6.ip6_nxt = IPPROTO_ICMPV6;
437 ip6.ip6_src = pseudo.ip6_src;
438 ip6.ip6_dst = pseudo.ip6_dst;
440 /* Fill in ICMP header */
442 icmp6.icmp6_type = type;
443 icmp6.icmp6_code = code;
444 icmp6.icmp6_cksum = 0;
446 /* Create pseudo header */
448 pseudo.length = htonl(icmp6_size + pseudo.length);
449 pseudo.next = htonl(IPPROTO_ICMPV6);
451 /* Generate checksum */
453 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
454 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
455 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
457 icmp6.icmp6_cksum = checksum;
459 /* Copy structs on stack back to packet */
461 memcpy(packet->data + ether_size, &ip6, ip6_size);
462 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
464 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
466 send_packet(source, packet);
469 static __inline__ void route_ipv6_unicast(node_t *source, vpn_packet_t *packet)
476 subnet = lookup_subnet_ipv6((ipv6_t *) &packet->data[38]);
479 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
480 source->name, source->hostname,
481 ntohs(*(uint16_t *) &packet->data[38]),
482 ntohs(*(uint16_t *) &packet->data[40]),
483 ntohs(*(uint16_t *) &packet->data[42]),
484 ntohs(*(uint16_t *) &packet->data[44]),
485 ntohs(*(uint16_t *) &packet->data[46]),
486 ntohs(*(uint16_t *) &packet->data[48]),
487 ntohs(*(uint16_t *) &packet->data[50]),
488 ntohs(*(uint16_t *) &packet->data[52]));
490 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
494 if(subnet->owner == source) {
495 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
499 if(!subnet->owner->status.reachable)
500 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
502 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
504 if(packet->len > via->mtu && via != myself) {
505 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);
506 packet->len = via->mtu;
507 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
511 send_packet(subnet->owner, packet);
516 static void route_neighborsol(node_t *source, vpn_packet_t *packet)
519 struct nd_neighbor_solicit ns;
520 struct nd_opt_hdr opt;
525 struct in6_addr ip6_src; /* source address */
526 struct in6_addr ip6_dst; /* destination address */
533 if(!checklength(source, packet, ether_size + ip6_size + ns_size + opt_size + ETH_ALEN))
536 if(source != myself) {
537 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got neighbor solicitation request from %s (%s) while in router mode!"), source->name, source->hostname);
541 /* Copy headers from packet to structs on the stack */
543 memcpy(&ip6, packet->data + ether_size, ip6_size);
544 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
545 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
547 /* First, snatch the source address from the neighbor solicitation packet */
550 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
552 /* Check if this is a valid neighbor solicitation request */
554 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
555 opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR) {
556 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
560 /* Create pseudo header */
562 pseudo.ip6_src = ip6.ip6_src;
563 pseudo.ip6_dst = ip6.ip6_dst;
564 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
565 pseudo.next = htonl(IPPROTO_ICMPV6);
567 /* Generate checksum */
569 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
570 checksum = inet_checksum(&ns, ns_size, checksum);
571 checksum = inet_checksum(&opt, opt_size, checksum);
572 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
575 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
579 /* Check if the IPv6 address exists on the VPN */
581 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
584 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
585 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
586 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
587 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
588 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
589 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
590 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
591 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
592 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
597 /* Check if it is for our own subnet */
599 if(subnet->owner == myself)
600 return; /* silently ignore */
602 /* Create neighbor advertation reply */
604 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
605 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
607 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
608 ip6.ip6_src = ns.nd_ns_target;
610 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
613 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
614 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
615 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
617 /* Create pseudo header */
619 pseudo.ip6_src = ip6.ip6_src;
620 pseudo.ip6_dst = ip6.ip6_dst;
621 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
622 pseudo.next = htonl(IPPROTO_ICMPV6);
624 /* Generate checksum */
626 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
627 checksum = inet_checksum(&ns, ns_size, checksum);
628 checksum = inet_checksum(&opt, opt_size, checksum);
629 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
631 ns.nd_ns_hdr.icmp6_cksum = checksum;
633 /* Copy structs on stack back to packet */
635 memcpy(packet->data + ether_size, &ip6, ip6_size);
636 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
637 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
639 send_packet(source, packet);
642 static __inline__ void route_ipv6(node_t *source, vpn_packet_t *packet)
646 if(!checklength(source, packet, ether_size + ip6_size))
649 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
650 route_neighborsol(source, packet);
654 route_ipv6_unicast(source, packet);
659 static void route_arp(node_t *source, vpn_packet_t *packet)
661 struct ether_arp arp;
667 if(!checklength(source, packet, ether_size + arp_size))
670 if(source != myself) {
671 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got ARP request from %s (%s) while in router mode!"), source->name, source->hostname);
675 /* First, snatch the source address from the ARP packet */
678 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
680 /* Copy headers from packet to structs on the stack */
682 memcpy(&arp, packet->data + ether_size, arp_size);
684 /* Check if this is a valid ARP request */
686 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
687 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
688 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
692 /* Check if the IPv4 address exists on the VPN */
694 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
697 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
698 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
703 /* Check if it is for our own subnet */
705 if(subnet->owner == myself)
706 return; /* silently ignore */
708 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
709 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
711 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
712 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
713 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
715 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
716 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
717 arp.arp_op = htons(ARPOP_REPLY);
719 /* Copy structs on stack back to packet */
721 memcpy(packet->data + ether_size, &arp, arp_size);
723 send_packet(source, packet);
726 void route(node_t *source, vpn_packet_t *packet)
730 if(!checklength(source, packet, ether_size))
733 switch (routing_mode) {
738 type = ntohs(*((uint16_t *)(&packet->data[12])));
741 route_arp(source, packet);
745 route_ipv4(source, packet);
749 route_ipv6(source, packet);
753 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown type %hx"), source->name, source->hostname, type);
760 route_mac(source, packet);
764 broadcast_packet(source, packet);