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.72 2003/12/20 19:47:53 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);
279 static __inline__ void route_ipv4_unicast(node_t *source, vpn_packet_t *packet)
285 subnet = lookup_subnet_ipv4((ipv4_t *) &packet->data[30]);
288 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d"),
289 source->name, source->hostname,
295 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
299 if(subnet->owner == source) {
300 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
304 if(!subnet->owner->status.reachable)
305 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
307 if(subnet->owner->options & OPTION_DONTFRAGMENT && packet->len > subnet->owner->mtu && subnet->owner != myself) {
308 ifdebug(TRAFFIC) logger(LOG_INFO, _("Packet for %s (%s) length %d larger than MTU %d"), subnet->owner->name, subnet->owner->hostname, packet->len, subnet->owner->mtu);
309 packet->len = subnet->owner->mtu;
310 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
314 if(priorityinheritance)
315 packet->priority = packet->data[15];
317 send_packet(subnet->owner, packet);
320 static __inline__ void route_ipv4(node_t *source, vpn_packet_t *packet)
324 if(!checklength(source, packet, ether_size + ip_size))
327 route_ipv4_unicast(source, packet);
332 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code)
335 struct icmp6_hdr icmp6 = {0};
339 struct in6_addr ip6_src; /* source address */
340 struct in6_addr ip6_dst; /* destination address */
350 /* Copy headers from packet to structs on the stack */
352 memcpy(&ip6, packet->data + ether_size, ip6_size);
354 /* Remember original source and destination */
356 pseudo.ip6_src = ip6.ip6_dst;
357 pseudo.ip6_dst = ip6.ip6_src;
359 pseudo.length = packet->len - ether_size;
361 if(type == ICMP6_PACKET_TOO_BIG)
362 icmp6.icmp6_mtu = htonl(pseudo.length);
364 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
365 pseudo.length = IP_MSS - ip6_size - icmp6_size;
367 /* Copy first part of original contents to ICMP message */
369 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
371 /* Fill in IPv6 header */
373 ip6.ip6_flow = htonl(0x60000000UL);
374 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
375 ip6.ip6_nxt = IPPROTO_ICMPV6;
377 ip6.ip6_src = pseudo.ip6_src;
378 ip6.ip6_dst = pseudo.ip6_dst;
380 /* Fill in ICMP header */
382 icmp6.icmp6_type = type;
383 icmp6.icmp6_code = code;
384 icmp6.icmp6_cksum = 0;
386 /* Create pseudo header */
388 pseudo.length = htonl(icmp6_size + pseudo.length);
389 pseudo.next = htonl(IPPROTO_ICMPV6);
391 /* Generate checksum */
393 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
394 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
395 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
397 icmp6.icmp6_cksum = checksum;
399 /* Copy structs on stack back to packet */
401 memcpy(packet->data + ether_size, &ip6, ip6_size);
402 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
404 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
406 send_packet(source, packet);
409 static __inline__ void route_ipv6_unicast(node_t *source, vpn_packet_t *packet)
415 subnet = lookup_subnet_ipv6((ipv6_t *) &packet->data[38]);
418 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
419 source->name, source->hostname,
420 ntohs(*(uint16_t *) &packet->data[38]),
421 ntohs(*(uint16_t *) &packet->data[40]),
422 ntohs(*(uint16_t *) &packet->data[42]),
423 ntohs(*(uint16_t *) &packet->data[44]),
424 ntohs(*(uint16_t *) &packet->data[46]),
425 ntohs(*(uint16_t *) &packet->data[48]),
426 ntohs(*(uint16_t *) &packet->data[50]),
427 ntohs(*(uint16_t *) &packet->data[52]));
429 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
433 if(subnet->owner == source) {
434 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
438 if(!subnet->owner->status.reachable)
439 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
441 if(subnet->owner->options & OPTION_DONTFRAGMENT && packet->len > subnet->owner->mtu && subnet->owner != myself) {
442 ifdebug(TRAFFIC) logger(LOG_INFO, _("Packet for %s (%s) length %d larger than MTU %d"), subnet->owner->name, subnet->owner->hostname, packet->len, subnet->owner->mtu);
443 packet->len = subnet->owner->mtu;
444 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
448 send_packet(subnet->owner, packet);
453 static void route_neighborsol(node_t *source, vpn_packet_t *packet)
456 struct nd_neighbor_solicit ns;
457 struct nd_opt_hdr opt;
462 struct in6_addr ip6_src; /* source address */
463 struct in6_addr ip6_dst; /* destination address */
470 if(!checklength(source, packet, ether_size + ip6_size + ns_size + opt_size + ETH_ALEN))
473 if(source != myself) {
474 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got neighbor solicitation request from %s (%s) while in router mode!"), source->name, source->hostname);
478 /* Copy headers from packet to structs on the stack */
480 memcpy(&ip6, packet->data + ether_size, ip6_size);
481 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
482 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
484 /* First, snatch the source address from the neighbor solicitation packet */
487 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
489 /* Check if this is a valid neighbor solicitation request */
491 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
492 opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR) {
493 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
497 /* Create pseudo header */
499 pseudo.ip6_src = ip6.ip6_src;
500 pseudo.ip6_dst = ip6.ip6_dst;
501 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
502 pseudo.next = htonl(IPPROTO_ICMPV6);
504 /* Generate checksum */
506 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
507 checksum = inet_checksum(&ns, ns_size, checksum);
508 checksum = inet_checksum(&opt, opt_size, checksum);
509 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
512 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
516 /* Check if the IPv6 address exists on the VPN */
518 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
521 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
522 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
523 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
524 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
525 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
526 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
527 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
528 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
529 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
534 /* Check if it is for our own subnet */
536 if(subnet->owner == myself)
537 return; /* silently ignore */
539 /* Create neighbor advertation reply */
541 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
542 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
544 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
545 ip6.ip6_src = ns.nd_ns_target;
547 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
550 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
551 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
552 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
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);
568 ns.nd_ns_hdr.icmp6_cksum = checksum;
570 /* Copy structs on stack back to packet */
572 memcpy(packet->data + ether_size, &ip6, ip6_size);
573 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
574 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
576 send_packet(source, packet);
579 static __inline__ void route_ipv6(node_t *source, vpn_packet_t *packet)
583 if(!checklength(source, packet, ether_size + ip6_size))
586 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
587 route_neighborsol(source, packet);
591 route_ipv6_unicast(source, packet);
596 static void route_arp(node_t *source, vpn_packet_t *packet)
598 struct ether_arp arp;
604 if(!checklength(source, packet, ether_size + arp_size))
607 if(source != myself) {
608 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got ARP request from %s (%s) while in router mode!"), source->name, source->hostname);
612 /* First, snatch the source address from the ARP packet */
615 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
617 /* Copy headers from packet to structs on the stack */
619 memcpy(&arp, packet->data + ether_size, arp_size);
621 /* Check if this is a valid ARP request */
623 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
624 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
625 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
629 /* Check if the IPv4 address exists on the VPN */
631 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
634 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
635 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
640 /* Check if it is for our own subnet */
642 if(subnet->owner == myself)
643 return; /* silently ignore */
645 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
646 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
648 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
649 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
650 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
652 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
653 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
654 arp.arp_op = htons(ARPOP_REPLY);
656 /* Copy structs on stack back to packet */
658 memcpy(packet->data + ether_size, &arp, arp_size);
660 send_packet(source, packet);
663 void route(node_t *source, vpn_packet_t *packet)
667 if(!checklength(source, packet, ether_size))
670 switch (routing_mode) {
675 type = ntohs(*((uint16_t *)(&packet->data[12])));
678 route_arp(source, packet);
682 route_ipv4(source, packet);
686 route_ipv6(source, packet);
690 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown type %hx"), source->name, source->hostname, type);
697 route_mac(source, packet);
701 broadcast_packet(source, packet);