3 Copyright (C) 2000-2004 Ivo Timmermans <ivo@tinc-vpn.org>,
4 2000-2004 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.
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>
41 #include "logger/logger.h"
43 #include "rt/subnet.h"
44 #include "support/avl.h"
45 #include "support/ethernet.h"
46 #include "support/ipv4.h"
47 #include "support/ipv6.h"
49 static mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
51 /* Sizes of various headers */
53 static const size_t ether_size = sizeof(struct ether_header);
54 static const size_t arp_size = sizeof(struct ether_arp);
55 static const size_t ip_size = sizeof(struct ip);
56 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
57 static const size_t ip6_size = sizeof(struct ip6_hdr);
58 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
59 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
60 static const size_t opt_size = sizeof(struct nd_opt_hdr);
62 static struct timeval expires(int seconds) {
65 gettimeofday(&tv, NULL);
73 static __inline__ uint16_t inet_checksum(const void *data, int len, uint16_t prevsum) {
74 const uint16_t *p = data;
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;
94 time_t now = time(NULL);
97 if(++count > frequency)
107 static __inline__ bool checklength(node_t *source, int len, int minlen) {
109 logger(LOG_WARNING, _("Got too short packet from %s"), source->name);
115 static __inline__ void learn_mac(mac_t *address) {
119 subnet = subnet_get_mac(address);
121 /* If we don't know this MAC address yet, store it */
124 logger(LOG_INFO, _("Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx"),
125 address->x[0], address->x[1], address->x[2], address->x[3],
126 address->x[4], address->x[5]);
128 subnet = subnet_new();
129 subnet->type = SUBNET_TYPE_MAC;
130 subnet->expires = expires(rt_macexpire);
131 subnet->net.mac.address = *address;
132 subnet->owner = myself;
135 /* And tell all other tinc daemons it's our MAC */
138 for(node = connection_tree->head; node; node = node->next) {
141 send_add_subnet(c, subnet);
146 if(timerisset(&subnet->expires))
147 subnet->expires = expires(rt_macexpire);
150 void age_subnets(void) {
154 for(node = myself->subnet_tree->head; node; node = next) {
157 if(s->expires && s->expires < now) {
159 char netstr[MAXNETSTR];
160 if(net2str(netstr, sizeof netstr, s))
161 logger(LOG_INFO, _("Subnet %s expired"), netstr);
164 for(node2 = connection_tree->head; node2; node2 = node2->next) {
167 send_del_subnet(c, s);
170 subnet_del(myself, s);
176 static void send_packet(node_t *dest, const uint8_t *packet, int len) {
178 rt_vnd->send(rt_vnd, packet, len);
179 } else if (dest->tnl) {
180 dest->tnl->send_packet(dest->tnl, packet, len);
182 logger(LOG_ERR, _("No tunnel for packet destination %s!"), dest->name);
186 static void broadcast_packet(node_t *source, const uint8_t *packet, int len) {
191 send_packet(myself, packet, len);
193 avl_foreach(rt_tnls, tnl, {
195 if(edge && edge->status.mst && edge->to != source)
196 send_packet(edge->to, packet, len);
200 static __inline__ void route_mac(node_t *source, const uint8_t *packet, int len) {
203 /* Learn source address */
206 learn_mac((mac_t *)(packet + 6));
208 /* Lookup destination address */
210 subnet = subnet_get_mac((mac_t *)(packet));
213 broadcast_packet(source, packet, len);
217 if(subnet->owner == source) {
218 logger(LOG_WARNING, _("Packet looping back to %s!"), source->name);
222 send_packet(subnet->owner, packet, len);
227 static void route_ipv4_unreachable(node_t *source, const uint8_t *packet, int len, uint8_t type, uint8_t code) {
228 uint8_t reply[ether_size + IP_MSS];
231 struct icmp icmp = {0};
233 struct in_addr ip_src;
234 struct in_addr ip_dst;
240 /* Copy headers from packet into properly aligned structs on the stack */
242 memcpy(&ip, packet + ether_size, ip_size);
244 /* Remember original source and destination */
249 oldlen = len - ether_size;
251 if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
252 icmp.icmp_nextmtu = htons(len - ether_size);
254 if(oldlen >= IP_MSS - ip_size - icmp_size)
255 oldlen = IP_MSS - ip_size - icmp_size;
257 /* Copy first part of original contents to ICMP message */
259 memmove(reply + ether_size + ip_size + icmp_size, packet + ether_size, oldlen);
261 /* Fill in IPv4 header */
264 ip.ip_hl = ip_size / 4;
266 ip.ip_len = htons(ip_size + icmp_size + oldlen);
270 ip.ip_p = IPPROTO_ICMP;
275 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
277 /* Fill in ICMP header */
279 icmp.icmp_type = type;
280 icmp.icmp_code = code;
283 icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
284 icmp.icmp_cksum = inet_checksum(packet + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
286 /* Copy structs on stack back to packet */
288 memcpy(reply + ether_size, &ip, ip_size);
289 memcpy(reply + ether_size + ip_size, &icmp, icmp_size);
291 send_packet(source, reply, ether_size + ip_size + icmp_size + oldlen);
296 static __inline__ void fragment_ipv4_packet(node_t *dest, const uint8_t *packet, int len) {
298 char fragment[dest->tnl->mtu];
299 int fraglen, maxlen, todo;
300 const uint8_t *offset;
301 uint16_t ip_off, origf;
303 memcpy(&ip, packet + ether_size, ip_size);
305 if(ip.ip_hl != ip_size / 4)
308 todo = ntohs(ip.ip_len) - ip_size;
310 if(ether_size + ip_size + todo != len) {
311 logger(LOG_WARNING, _("Length of packet (%d) doesn't match length in IPv4 header (%d)"), len, ether_size + ip_size + todo);
315 logger(LOG_INFO, _("Fragmenting packet of %d bytes to %s"), len, dest->name);
317 offset = packet + ether_size + ip_size;
318 maxlen = (dest->tnl->mtu - ether_size - ip_size) & ~0x7;
319 ip_off = ntohs(ip.ip_off);
320 origf = ip_off & ~IP_OFFMASK;
321 ip_off &= IP_OFFMASK;
324 fraglen = todo > maxlen ? maxlen : todo;
325 memcpy(fragment + ether_size + ip_size, offset, fraglen);
329 ip.ip_len = htons(ip_size + fraglen);
330 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
332 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
333 memcpy(fragment, packet, ether_size);
334 memcpy(fragment + ether_size, &ip, ip_size);
336 send_packet(dest, fragment, ether_size + ip_size + fraglen);
338 ip_off += fraglen / 8;
342 static __inline__ void route_ipv4_unicast(node_t *source, const uint8_t *packet, int len) {
346 subnet = subnet_get_ipv4((ipv4_t *)(packet + 30));
349 logger(LOG_WARNING, _("Cannot route packet from %s: unknown IPv4 destination address %d.%d.%d.%d"),
356 route_ipv4_unreachable(source, packet, len, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
360 if(subnet->owner == source) {
361 logger(LOG_WARNING, _("Packet looping back to %s!"), source->name);
365 if(!subnet->owner->status.reachable)
366 route_ipv4_unreachable(source, packet, len, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
368 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
370 if(len > via->tnl->mtu && via != myself) {
371 logger(LOG_INFO, _("Packet for %s length %d larger than MTU %d"), subnet->owner->name, len, via->tnl->mtu);
372 if(packet[20] & 0x40) {
374 route_ipv4_unreachable(source, packet, len, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
376 fragment_ipv4_packet(via, packet, len);
382 send_packet(subnet->owner, packet, len);
385 static __inline__ void route_ipv4(node_t *source, const uint8_t *packet, int len) {
386 if(!checklength(source, len, ether_size + ip_size))
389 route_ipv4_unicast(source, packet, len);
394 static void route_ipv6_unreachable(node_t *source, const uint8_t *packet, int len, uint8_t type, uint8_t code) {
395 uint8_t reply[ether_size + IP_MSS];
397 struct icmp6_hdr icmp6 = {0};
401 struct in6_addr ip6_src; /* source address */
402 struct in6_addr ip6_dst; /* destination address */
410 /* Copy headers from packet to structs on the stack */
412 memcpy(&ip6, packet + 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 = 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 memcpy(reply + ether_size + ip6_size + icmp6_size, packet + 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(reply + 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(reply + ether_size, &ip6, ip6_size);
462 memcpy(reply + ether_size + ip6_size, &icmp6, icmp6_size);
464 send_packet(source, reply, ether_size + ip6_size + ntohl(pseudo.length));
467 static __inline__ void route_ipv6_unicast(node_t *source, const uint8_t *packet, int len) {
471 subnet = subnet_get_ipv6((ipv6_t *)(packet + 38));
474 logger(LOG_WARNING, _("Cannot route packet from %s: unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
476 ntohs(*(uint16_t *)(packet + 38)),
477 ntohs(*(uint16_t *)(packet + 40)),
478 ntohs(*(uint16_t *)(packet + 42)),
479 ntohs(*(uint16_t *)(packet + 44)),
480 ntohs(*(uint16_t *)(packet + 46)),
481 ntohs(*(uint16_t *)(packet + 48)),
482 ntohs(*(uint16_t *)(packet + 50)),
483 ntohs(*(uint16_t *)(packet + 52)));
485 route_ipv6_unreachable(source, packet, len, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
489 if(subnet->owner == source) {
490 logger(LOG_WARNING, _("Packet looping back to %s!"), source->name);
494 if(!subnet->owner->status.reachable)
495 route_ipv6_unreachable(source, packet, len, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
497 via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
499 if(len > via->tnl->mtu && via != myself) {
500 logger(LOG_INFO, _("Packet for %s length %d larger than MTU %d"), subnet->owner->name, len, via->tnl->mtu);
502 route_ipv6_unreachable(source, packet, len, ICMP6_PACKET_TOO_BIG, 0);
506 send_packet(subnet->owner, packet, len);
511 static void route_neighborsol(node_t *source, const uint8_t *packet, int len) {
514 struct nd_neighbor_solicit ns;
515 struct nd_opt_hdr opt;
520 struct in6_addr ip6_src; /* source address */
521 struct in6_addr ip6_dst; /* destination address */
526 if(!checklength(source, len, ether_size + ip6_size + ns_size + opt_size + ETH_ALEN))
529 if(source != myself) {
530 logger(LOG_WARNING, _("Got neighbor solicitation request from %s while in router mode!"), source->name);
534 /* Copy headers from packet to structs on the stack */
536 memcpy(&ip6, packet + ether_size, ip6_size);
537 memcpy(&ns, packet + ether_size + ip6_size, ns_size);
538 memcpy(&opt, packet + ether_size + ip6_size + ns_size, opt_size);
540 /* First, snatch the source address from the neighbor solicitation packet */
543 memcpy(mymac.x, packet + ETH_ALEN, ETH_ALEN);
545 /* Check if this is a valid neighbor solicitation request */
547 if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
548 opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR) {
549 logger(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
553 /* Create pseudo header */
555 pseudo.ip6_src = ip6.ip6_src;
556 pseudo.ip6_dst = ip6.ip6_dst;
557 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
558 pseudo.next = htonl(IPPROTO_ICMPV6);
560 /* Generate checksum */
562 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
563 checksum = inet_checksum(&ns, ns_size, checksum);
564 checksum = inet_checksum(&opt, opt_size, checksum);
565 checksum = inet_checksum(packet + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
568 logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
572 /* Check if the IPv6 address exists on the VPN */
574 subnet = subnet_get_ipv6((ipv6_t *) &ns.nd_ns_target);
577 logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
578 ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
579 ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
580 ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
581 ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
582 ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
583 ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
584 ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
585 ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
590 /* Check if it is for our own subnet */
592 if(subnet->owner == myself)
593 return; /* silently ignore */
595 /* Create neighbor advertation reply */
597 memcpy(reply, packet + ETH_ALEN, ETH_ALEN); /* copy destination address */
598 memcpy(reply + ETH_ALEN, packet + ETH_ALEN, ETH_ALEN); /* copy destination address */
599 reply[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
601 ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocoll address */
602 ip6.ip6_src = ns.nd_ns_target;
604 memcpy(reply + ether_size + ip6_size + ns_size + opt_size, reply + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
607 ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
608 ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
609 opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
611 /* Create pseudo header */
613 pseudo.ip6_src = ip6.ip6_src;
614 pseudo.ip6_dst = ip6.ip6_dst;
615 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
616 pseudo.next = htonl(IPPROTO_ICMPV6);
618 /* Generate checksum */
620 checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
621 checksum = inet_checksum(&ns, ns_size, checksum);
622 checksum = inet_checksum(&opt, opt_size, checksum);
623 checksum = inet_checksum(packet + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
625 ns.nd_ns_hdr.icmp6_cksum = checksum;
627 /* Copy structs on stack back to packet */
629 memcpy(reply + ether_size, &ip6, ip6_size);
630 memcpy(reply + ether_size + ip6_size, &ns, ns_size);
631 memcpy(reply + ether_size + ip6_size + ns_size, &opt, opt_size);
633 send_packet(source, reply, len);
636 static __inline__ void route_ipv6(node_t *source, const uint8_t *packet, int len) {
637 if(!checklength(source, len, ether_size + ip6_size))
640 if(packet[20] == IPPROTO_ICMPV6 && checklength(source, len, ether_size + ip6_size + icmp6_size) && packet[54] == ND_NEIGHBOR_SOLICIT) {
641 route_neighborsol(source, packet, len);
645 route_ipv6_unicast(source, packet, len);
650 static void route_arp(node_t *source, const uint8_t *packet, int len) {
652 struct ether_arp arp;
656 if(!checklength(source, len, ether_size + arp_size))
659 if(source != myself) {
660 logger(LOG_WARNING, _("Got ARP request from %s while in router mode!"), source->name);
664 /* First, snatch the source address from the ARP packet */
667 memcpy(mymac.x, packet + ETH_ALEN, ETH_ALEN);
669 /* Copy headers from packet to structs on the stack */
671 memcpy(&arp, packet + ether_size, arp_size);
673 /* Check if this is a valid ARP request */
675 if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
676 arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
677 logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
681 /* Check if the IPv4 address exists on the VPN */
683 subnet = subnet_get_ipv4((ipv4_t *) &arp.arp_tpa);
686 logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
687 arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
692 /* Check if it is for our own subnet */
694 if(subnet->owner == myself)
695 return; /* silently ignore */
697 memcpy(reply, packet + ETH_ALEN, ETH_ALEN); /* copy destination address */
698 memcpy(reply + ETH_ALEN, packet + ETH_ALEN, ETH_ALEN); /* copy destination address */
699 reply[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
701 memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
702 memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
703 memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
705 memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
706 memcpy(arp.arp_sha, reply + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
707 arp.arp_op = htons(ARPOP_REPLY);
709 /* Copy structs on stack back to packet */
711 memcpy(reply + ether_size, &arp, arp_size);
713 send_packet(source, reply, len);
716 void route(node_t *source, const uint8_t *packet, int len) {
717 if(!checklength(source, len, ether_size))
725 type = ntohs(*((uint16_t *)(packet + 12)));
728 route_arp(source, packet, len);
732 route_ipv4(source, packet, len);
736 route_ipv6(source, packet, len);
740 logger(LOG_WARNING, _("Cannot route packet from %s: unknown type %hx"), source->name, type);
747 route_mac(source, packet, len);
751 broadcast_packet(source, packet, len);