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.71 2003/12/13 21:50:26 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 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(oldlen >= IP_MSS - ip_size - icmp_size)
235 oldlen = IP_MSS - ip_size - icmp_size;
237 /* Copy first part of original contents to ICMP message */
239 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
241 /* Fill in IPv4 header */
244 ip.ip_hl = ip_size / 4;
246 ip.ip_len = htons(ip_size + icmp_size + oldlen);
250 ip.ip_p = IPPROTO_ICMP;
255 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
257 /* Fill in ICMP header */
259 icmp.icmp_type = ICMP_DEST_UNREACH;
260 icmp.icmp_code = code;
263 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
264 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
266 /* Copy structs on stack back to packet */
268 memcpy(packet->data + ether_size, &ip, ip_size);
269 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
271 packet->len = ether_size + ip_size + icmp_size + oldlen;
273 send_packet(source, packet);
276 static __inline__ void route_ipv4_unicast(node_t *source, vpn_packet_t *packet)
282 subnet = lookup_subnet_ipv4((ipv4_t *) &packet->data[30]);
285 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d"),
286 source->name, source->hostname,
292 route_ipv4_unreachable(source, packet, ICMP_NET_UNKNOWN);
296 if(subnet->owner == source) {
297 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
301 if(!subnet->owner->status.reachable)
302 route_ipv4_unreachable(source, packet, ICMP_NET_UNREACH);
304 if(priorityinheritance)
305 packet->priority = packet->data[15];
307 send_packet(subnet->owner, packet);
310 static __inline__ void route_ipv4(node_t *source, vpn_packet_t *packet)
314 if(!checklength(source, packet, ether_size + ip_size))
317 route_ipv4_unicast(source, packet);
322 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t code)
325 struct icmp6_hdr icmp6 = {0};
329 struct in6_addr ip6_src; /* source address */
330 struct in6_addr ip6_dst; /* destination address */
340 /* Copy headers from packet to structs on the stack */
342 memcpy(&ip6, packet->data + ether_size, ip6_size);
344 /* Remember original source and destination */
346 pseudo.ip6_src = ip6.ip6_dst;
347 pseudo.ip6_dst = ip6.ip6_src;
349 pseudo.length = packet->len - ether_size;
351 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
352 pseudo.length = IP_MSS - ip6_size - icmp6_size;
354 /* Copy first part of original contents to ICMP message */
356 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
358 /* Fill in IPv6 header */
360 ip6.ip6_flow = htonl(0x60000000UL);
361 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
362 ip6.ip6_nxt = IPPROTO_ICMPV6;
364 ip6.ip6_src = pseudo.ip6_src;
365 ip6.ip6_dst = pseudo.ip6_dst;
367 /* Fill in ICMP header */
369 icmp6.icmp6_type = ICMP6_DST_UNREACH;
370 icmp6.icmp6_code = code;
371 icmp6.icmp6_cksum = 0;
373 /* Create pseudo header */
375 pseudo.length = htonl(icmp6_size + pseudo.length);
376 pseudo.next = htonl(IPPROTO_ICMPV6);
378 /* Generate checksum */
380 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
381 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
382 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
384 icmp6.icmp6_cksum = checksum;
386 /* Copy structs on stack back to packet */
388 memcpy(packet->data + ether_size, &ip6, ip6_size);
389 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
391 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
393 send_packet(source, packet);
396 static __inline__ void route_ipv6_unicast(node_t *source, vpn_packet_t *packet)
402 subnet = lookup_subnet_ipv6((ipv6_t *) &packet->data[38]);
405 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
406 source->name, source->hostname,
407 ntohs(*(uint16_t *) &packet->data[38]),
408 ntohs(*(uint16_t *) &packet->data[40]),
409 ntohs(*(uint16_t *) &packet->data[42]),
410 ntohs(*(uint16_t *) &packet->data[44]),
411 ntohs(*(uint16_t *) &packet->data[46]),
412 ntohs(*(uint16_t *) &packet->data[48]),
413 ntohs(*(uint16_t *) &packet->data[50]),
414 ntohs(*(uint16_t *) &packet->data[52]));
416 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH_ADDR);
420 if(subnet->owner == source) {
421 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
425 if(!subnet->owner->status.reachable)
426 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH_NOROUTE);
428 send_packet(subnet->owner, packet);
433 static void route_neighborsol(node_t *source, vpn_packet_t *packet)
436 struct nd_neighbor_solicit ns;
437 struct nd_opt_hdr opt;
442 struct in6_addr ip6_src; /* source address */
443 struct in6_addr ip6_dst; /* destination address */
450 if(!checklength(source, packet, ether_size + ip6_size + ns_size + opt_size + ETH_ALEN))
453 if(source != myself) {
454 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got neighbor solicitation request from %s (%s) while in router mode!"), source->name, source->hostname);
458 /* Copy headers from packet to structs on the stack */
460 memcpy(&ip6, packet->data + ether_size, ip6_size);
461 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
462 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
464 /* First, snatch the source address from the neighbor solicitation packet */
467 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
469 /* Check if this is a valid neighbor solicitation request */
471 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
472 opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR) {
473 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
477 /* Create pseudo header */
479 pseudo.ip6_src = ip6.ip6_src;
480 pseudo.ip6_dst = ip6.ip6_dst;
481 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
482 pseudo.next = htonl(IPPROTO_ICMPV6);
484 /* Generate checksum */
486 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
487 checksum = inet_checksum(&ns, ns_size, checksum);
488 checksum = inet_checksum(&opt, opt_size, checksum);
489 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
492 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
496 /* Check if the IPv6 address exists on the VPN */
498 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
501 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
502 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
503 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
504 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
505 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
506 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
507 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
508 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
509 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
514 /* Check if it is for our own subnet */
516 if(subnet->owner == myself)
517 return; /* silently ignore */
519 /* Create neighbor advertation reply */
521 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
522 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
524 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
525 ip6.ip6_src = ns.nd_ns_target;
527 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
530 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
531 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
532 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
534 /* Create pseudo header */
536 pseudo.ip6_src = ip6.ip6_src;
537 pseudo.ip6_dst = ip6.ip6_dst;
538 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
539 pseudo.next = htonl(IPPROTO_ICMPV6);
541 /* Generate checksum */
543 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
544 checksum = inet_checksum(&ns, ns_size, checksum);
545 checksum = inet_checksum(&opt, opt_size, checksum);
546 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
548 ns.nd_ns_hdr.icmp6_cksum = checksum;
550 /* Copy structs on stack back to packet */
552 memcpy(packet->data + ether_size, &ip6, ip6_size);
553 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
554 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
556 send_packet(source, packet);
559 static __inline__ void route_ipv6(node_t *source, vpn_packet_t *packet)
563 if(!checklength(source, packet, ether_size + ip6_size))
566 if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
567 route_neighborsol(source, packet);
571 route_ipv6_unicast(source, packet);
576 static void route_arp(node_t *source, vpn_packet_t *packet)
578 struct ether_arp arp;
584 if(!checklength(source, packet, ether_size + arp_size))
587 if(source != myself) {
588 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got ARP request from %s (%s) while in router mode!"), source->name, source->hostname);
592 /* First, snatch the source address from the ARP packet */
595 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
597 /* Copy headers from packet to structs on the stack */
599 memcpy(&arp, packet->data + ether_size, arp_size);
601 /* Check if this is a valid ARP request */
603 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
604 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
605 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
609 /* Check if the IPv4 address exists on the VPN */
611 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
614 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
615 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
620 /* Check if it is for our own subnet */
622 if(subnet->owner == myself)
623 return; /* silently ignore */
625 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
626 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
628 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
629 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
630 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
632 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
633 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
634 arp.arp_op = htons(ARPOP_REPLY);
636 /* Copy structs on stack back to packet */
638 memcpy(packet->data + ether_size, &arp, arp_size);
640 send_packet(source, packet);
643 void route(node_t *source, vpn_packet_t *packet)
647 if(!checklength(source, packet, ether_size))
650 switch (routing_mode) {
655 type = ntohs(*((uint16_t *)(&packet->data[12])));
658 route_arp(source, packet);
662 route_ipv4(source, packet);
666 route_ipv6(source, packet);
670 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown type %hx"), source->name, source->hostname, type);
677 route_mac(source, packet);
681 broadcast_packet(source, packet);