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.68 2003/10/06 13:57:12 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"
54 rmode_t routing_mode = RMODE_ROUTER;
55 bool priorityinheritance = false;
57 bool overwrite_mac = false;
58 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
60 /* Sizes of various headers */
62 static const size_t ether_size = sizeof(struct ether_header);
63 static const size_t arp_size = sizeof(struct ether_arp);
64 static const size_t ip_size = sizeof(struct ip);
65 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
66 static const size_t ip6_size = sizeof(struct ip6_hdr);
67 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
68 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
69 static const size_t opt_size = sizeof(struct nd_opt_hdr);
73 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum)
76 uint32_t checksum = prevsum ^ 0xFFFF;
84 checksum += *(unsigned char *)p;
87 checksum = (checksum & 0xFFFF) + (checksum >> 16);
92 static bool ratelimit(int frequency) {
93 static time_t lasttime = 0;
97 if(++count > frequency)
107 static void learn_mac(mac_t *address)
115 subnet = lookup_subnet_mac(address);
117 /* If we don't know this MAC address yet, store it */
119 if(!subnet || subnet->owner != myself) {
120 ifdebug(TRAFFIC) logger(LOG_INFO, _("Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx"),
121 address->x[0], address->x[1], address->x[2], address->x[3],
122 address->x[4], address->x[5]);
124 subnet = new_subnet();
125 subnet->type = SUBNET_MAC;
126 memcpy(&subnet->net.mac.address, address, sizeof(mac_t));
127 subnet_add(myself, subnet);
129 /* And tell all other tinc daemons it's our MAC */
131 for(node = connection_tree->head; node; node = node->next) {
134 send_add_subnet(c, subnet);
138 subnet->net.mac.lastseen = now;
145 avl_node_t *node, *next, *node2;
149 for(node = myself->subnet_tree->head; node; node = next) {
152 if(s->type == SUBNET_MAC && s->net.mac.lastseen && s->net.mac.lastseen + macexpire < now) {
153 ifdebug(TRAFFIC) logger(LOG_INFO, _("MAC address %hx:%hx:%hx:%hx:%hx:%hx expired"),
154 s->net.mac.address.x[0], s->net.mac.address.x[1],
155 s->net.mac.address.x[2], s->net.mac.address.x[3],
156 s->net.mac.address.x[4], s->net.mac.address.x[5]);
158 for(node2 = connection_tree->head; node2; node2 = node2->next) {
161 send_del_subnet(c, s);
164 subnet_del(myself, s);
169 static node_t *route_mac(vpn_packet_t *packet)
175 /* Learn source address */
177 learn_mac((mac_t *)(&packet->data[6]));
179 /* Lookup destination address */
181 subnet = lookup_subnet_mac((mac_t *)(&packet->data[0]));
184 return subnet->owner;
191 static void route_ipv4_unreachable(vpn_packet_t *packet, uint8_t code)
196 struct in_addr ip_src;
197 struct in_addr ip_dst;
205 /* Copy headers from packet into properly aligned structs on the stack */
207 memcpy(&ip, packet->data + ether_size, ip_size);
208 memcpy(&icmp, packet->data + ether_size + ip_size, icmp_size);
210 /* Remember original source and destination */
212 memcpy(&ip_src, &ip.ip_src, sizeof(ip_src));
213 memcpy(&ip_dst, &ip.ip_dst, sizeof(ip_dst));
215 oldlen = packet->len - ether_size;
217 if(oldlen >= IP_MSS - ip_size - icmp_size)
218 oldlen = IP_MSS - ip_size - icmp_size;
220 /* Copy first part of original contents to ICMP message */
222 memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
224 /* Fill in IPv4 header */
227 ip.ip_hl = ip_size / 4;
229 ip.ip_len = htons(ip_size + icmp_size + oldlen);
233 ip.ip_p = IPPROTO_ICMP;
235 memcpy(&ip.ip_src, &ip_dst, sizeof(ip_src));
236 memcpy(&ip.ip_dst, &ip_src, sizeof(ip_dst));
238 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
240 /* Fill in ICMP header */
242 icmp.icmp_type = ICMP_DEST_UNREACH;
243 icmp.icmp_code = code;
246 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
247 icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
249 /* Copy structs on stack back to packet */
251 memcpy(packet->data + ether_size, &ip, ip_size);
252 memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
254 packet->len = ether_size + ip_size + icmp_size + oldlen;
256 write_packet(packet);
259 static node_t *route_ipv4(vpn_packet_t *packet)
265 if(priorityinheritance)
266 packet->priority = packet->data[15];
268 subnet = lookup_subnet_ipv4((ipv4_t *) &packet->data[30]);
271 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: unknown IPv4 destination address %d.%d.%d.%d"),
272 packet->data[30], packet->data[31], packet->data[32],
275 route_ipv4_unreachable(packet, ICMP_NET_UNKNOWN);
279 if(!subnet->owner->status.reachable)
280 route_ipv4_unreachable(packet, ICMP_NET_UNREACH);
282 return subnet->owner;
287 static void route_ipv6_unreachable(vpn_packet_t *packet, uint8_t code)
290 struct icmp6_hdr icmp6;
294 struct in6_addr ip6_src; /* source address */
295 struct in6_addr ip6_dst; /* destination address */
305 /* Copy headers from packet to structs on the stack */
307 memcpy(&ip6, packet->data + ether_size, ip6_size);
308 memcpy(&icmp6, packet->data + ether_size + ip6_size, icmp6_size);
310 /* Remember original source and destination */
312 memcpy(&pseudo.ip6_src, &ip6.ip6_dst, sizeof(ip6.ip6_src));
313 memcpy(&pseudo.ip6_dst, &ip6.ip6_src, sizeof(ip6.ip6_dst));
315 pseudo.length = ntohs(ip6.ip6_plen) + ip6_size;
317 if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
318 pseudo.length = IP_MSS - ip6_size - icmp6_size;
320 /* Copy first part of original contents to ICMP message */
322 memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
324 /* Fill in IPv6 header */
326 ip6.ip6_flow = htonl(0x60000000UL);
327 ip6.ip6_plen = htons(icmp6_size + pseudo.length);
328 ip6.ip6_nxt = IPPROTO_ICMPV6;
330 memcpy(&ip6.ip6_src, &pseudo.ip6_src, sizeof(ip6.ip6_src));
331 memcpy(&ip6.ip6_dst, &pseudo.ip6_dst, sizeof(ip6.ip6_dst));
333 /* Fill in ICMP header */
335 icmp6.icmp6_type = ICMP6_DST_UNREACH;
336 icmp6.icmp6_code = code;
337 icmp6.icmp6_cksum = 0;
339 /* Create pseudo header */
341 pseudo.length = htonl(icmp6_size + pseudo.length);
342 pseudo.next = htonl(IPPROTO_ICMPV6);
344 /* Generate checksum */
346 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
347 checksum = inet_checksum(&icmp6, icmp6_size, checksum);
348 checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
350 icmp6.icmp6_cksum = checksum;
352 /* Copy structs on stack back to packet */
354 memcpy(packet->data + ether_size, &ip6, ip6_size);
355 memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
357 packet->len = ether_size + ip6_size + ntohl(pseudo.length);
359 write_packet(packet);
362 static node_t *route_ipv6(vpn_packet_t *packet)
368 subnet = lookup_subnet_ipv6((ipv6_t *) &packet->data[38]);
371 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
372 ntohs(*(uint16_t *) &packet->data[38]),
373 ntohs(*(uint16_t *) &packet->data[40]),
374 ntohs(*(uint16_t *) &packet->data[42]),
375 ntohs(*(uint16_t *) &packet->data[44]),
376 ntohs(*(uint16_t *) &packet->data[46]),
377 ntohs(*(uint16_t *) &packet->data[48]),
378 ntohs(*(uint16_t *) &packet->data[50]),
379 ntohs(*(uint16_t *) &packet->data[52]));
380 route_ipv6_unreachable(packet, ICMP6_DST_UNREACH_ADDR);
385 if(!subnet->owner->status.reachable)
386 route_ipv6_unreachable(packet, ICMP6_DST_UNREACH_NOROUTE);
388 return subnet->owner;
393 static void route_neighborsol(vpn_packet_t *packet)
396 struct nd_neighbor_solicit ns;
397 struct nd_opt_hdr opt;
402 struct in6_addr ip6_src; /* source address */
403 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);
413 memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
414 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
416 /* First, snatch the source address from the neighbor solicitation packet */
419 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
421 /* Check if this is a valid neighbor solicitation request */
423 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
424 opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR) {
425 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
429 /* Create pseudo header */
431 memcpy(&pseudo.ip6_src, &ip6.ip6_src, sizeof(ip6.ip6_src));
432 memcpy(&pseudo.ip6_dst, &ip6.ip6_dst, sizeof(ip6.ip6_dst));
433 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
434 pseudo.next = htonl(IPPROTO_ICMPV6);
436 /* Generate checksum */
438 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
439 checksum = inet_checksum(&ns, ns_size, checksum);
440 checksum = inet_checksum(&opt, opt_size, checksum);
443 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
447 /* Check if the IPv6 address exists on the VPN */
449 subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
452 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
453 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
454 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
455 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
456 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
457 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
458 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
459 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
460 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
465 /* Check if it is for our own subnet */
467 if(subnet->owner == myself)
468 return; /* silently ignore */
470 /* Create neighbor advertation reply */
472 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
473 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
475 memcpy(&ip6.ip6_src, &ns.nd_ns_target, sizeof(ip6.ip6_src)); /* swap destination and source protocol address */
476 memcpy(&ip6.ip6_dst, &ip6.ip6_src, sizeof(ip6.ip6_dst)); /* ... */
478 memcpy(&opt + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
481 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
482 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
483 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
485 /* Create pseudo header */
487 memcpy(&pseudo.ip6_src, &ip6.ip6_src, sizeof(ip6.ip6_src));
488 memcpy(&pseudo.ip6_dst, &ip6.ip6_dst, sizeof(ip6.ip6_dst));
489 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
490 pseudo.next = htonl(IPPROTO_ICMPV6);
492 /* Generate checksum */
494 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
495 checksum = inet_checksum(&ns, ns_size, checksum);
496 checksum = inet_checksum(&opt, opt_size, checksum);
498 ns.nd_ns_hdr.icmp6_cksum = checksum;
500 /* Copy structs on stack back to packet */
502 memcpy(packet->data + ether_size, &ip6, ip6_size);
503 memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
504 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
506 write_packet(packet);
511 static void route_arp(vpn_packet_t *packet)
513 struct ether_arp arp;
519 /* First, snatch the source address from the ARP packet */
522 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
524 /* Copy headers from packet to structs on the stack */
526 memcpy(&arp, packet->data + ether_size, arp_size);
528 /* Check if this is a valid ARP request */
530 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
531 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
532 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
536 /* Check if the IPv4 address exists on the VPN */
538 subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
541 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
542 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
547 /* Check if it is for our own subnet */
549 if(subnet->owner == myself)
550 return; /* silently ignore */
552 memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
553 packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
555 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
556 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
557 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
559 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
560 memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
561 arp.arp_op = htons(ARPOP_REPLY);
563 /* Copy structs on stack back to packet */
565 memcpy(packet->data + ether_size, &arp, arp_size);
567 write_packet(packet);
570 void route_outgoing(vpn_packet_t *packet)
577 if(packet->len < ether_size) {
578 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
582 /* FIXME: multicast? */
584 switch (routing_mode) {
586 type = ntohs(*((uint16_t *)(&packet->data[12])));
589 if(packet->len < ether_size + ip_size) {
590 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
594 n = route_ipv4(packet);
598 if(packet->len < ether_size + ip6_size) {
599 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
603 if(packet->data[20] == IPPROTO_ICMPV6 && packet->len >= ether_size + ip6_size + ns_size && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
604 route_neighborsol(packet);
607 n = route_ipv6(packet);
611 if(packet->len < ether_size + arp_size) {
612 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
620 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: unknown type %hx"), type);
624 send_packet(n, packet);
628 n = route_mac(packet);
630 send_packet(n, packet);
632 broadcast_packet(myself, packet);
636 broadcast_packet(myself, packet);
641 void route_incoming(node_t *source, vpn_packet_t *packet)
643 if(packet->len < ether_size) {
644 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
648 switch (routing_mode) {
654 type = ntohs(*((uint16_t *)(&packet->data[12])));
657 if(packet->len < ether_size + ip_size) {
658 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
662 n = route_ipv4(packet);
666 if(packet->len < ether_size + ip6_size) {
667 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
671 n = route_ipv6(packet);
682 memcpy(packet->data, mymac.x, ETH_ALEN);
683 write_packet(packet);
685 send_packet(n, packet);
694 subnet = lookup_subnet_mac((mac_t *)(&packet->data[0]));
697 if(subnet->owner == myself)
698 write_packet(packet);
700 send_packet(subnet->owner, packet);
702 broadcast_packet(source, packet);
703 write_packet(packet);
709 broadcast_packet(source, packet); /* Spread it on */
710 write_packet(packet);