Code beautification, start of multicast support.
[tinc] / src / route.c
1 /*
2     route.c -- routing
3     Copyright (C) 2000-2003 Ivo Timmermans <ivo@o2w.nl>,
4                   2000-2003 Guus Sliepen <guus@sliepen.eu.org>
5
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.
10
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.
15
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.
19
20     $Id: route.c,v 1.1.2.70 2003/12/12 19:52:25 guus Exp $
21 */
22
23 #include "system.h"
24
25 #ifdef HAVE_NET_ETHERNET_H
26 #include <net/ethernet.h>
27 #endif
28 #ifdef HAVE_NET_IF_ARP_H
29 #include <net/if_arp.h>
30 #endif
31 #ifdef HAVE_NETINET_IP_ICMP_H
32 #include <netinet/ip_icmp.h>
33 #endif
34 #ifdef HAVE_NETINET_ICMP6_H
35 #include <netinet/icmp6.h>
36 #endif
37 #ifdef HAVE_NETINET_IF_ETHER_H
38 #include <netinet/if_ether.h>
39 #endif
40
41 #include "avl_tree.h"
42 #include "connection.h"
43 #include "ethernet.h"
44 #include "ipv4.h"
45 #include "ipv6.h"
46 #include "logger.h"
47 #include "net.h"
48 #include "protocol.h"
49 #include "route.h"
50 #include "subnet.h"
51 #include "utils.h"
52
53 rmode_t routing_mode = RMODE_ROUTER;
54 bool priorityinheritance = false;
55 int macexpire = 600;
56 int multicastexpire = 375;
57 bool overwrite_mac = false;
58 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
59
60 /* Sizes of various headers */
61
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);
70
71 /* RFC 1071 */
72
73 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum)
74 {
75         uint16_t *p = data;
76         uint32_t checksum = prevsum ^ 0xFFFF;
77
78         while(len >= 2) {
79                 checksum += *p++;
80                 len -= 2;
81         }
82         
83         if(len)
84                 checksum += *(uint8_t *)p;
85
86         while(checksum >> 16)
87                 checksum = (checksum & 0xFFFF) + (checksum >> 16);
88
89         return ~checksum;
90 }
91
92 static bool ratelimit(int frequency) {
93         static time_t lasttime = 0;
94         static int count = 0;
95         
96         if(lasttime == now) {
97                 if(++count > frequency)
98                         return true;
99         } else {
100                 lasttime = now;
101                 count = 0;
102         }
103
104         return false;
105 }
106
107 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
108         if(packet->len < length) {
109                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got too short packet from %s (%s)"), source->name, source->hostname);
110                 return false;
111         } else
112                 return true;
113 }
114         
115 static void learn_mac(mac_t *address)
116 {
117         subnet_t *subnet;
118         avl_node_t *node;
119         connection_t *c;
120
121         cp();
122
123         subnet = lookup_subnet_mac(address);
124
125         /* If we don't know this MAC address yet, store it */
126
127         if(!subnet) {
128                 ifdebug(TRAFFIC) logger(LOG_INFO, _("Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx"),
129                                    address->x[0], address->x[1], address->x[2], address->x[3],
130                                    address->x[4], address->x[5]);
131
132                 subnet = new_subnet();
133                 subnet->type = SUBNET_MAC;
134                 subnet->expires = now + macexpire;
135                 subnet->net.mac.address = *address;
136                 subnet_add(myself, subnet);
137
138                 /* And tell all other tinc daemons it's our MAC */
139
140                 for(node = connection_tree->head; node; node = node->next) {
141                         c = node->data;
142                         if(c->status.active)
143                                 send_add_subnet(c, subnet);
144                 }
145         }
146
147         if(subnet->expires)
148                 subnet->expires = now + macexpire;
149 }
150
151 void age_subnets(void)
152 {
153         subnet_t *s;
154         connection_t *c;
155         avl_node_t *node, *next, *node2;
156
157         cp();
158
159         for(node = myself->subnet_tree->head; node; node = next) {
160                 next = node->next;
161                 s = node->data;
162                 if(s->expires && s->expires < now) {
163                         ifdebug(TRAFFIC) {
164                                 char netstr[MAXNETSTR];
165                                 if(net2str(netstr, sizeof netstr, s))
166                                         logger(LOG_INFO, _("Subnet %s expired"), netstr);
167                         }
168
169                         for(node2 = connection_tree->head; node2; node2 = node2->next) {
170                                 c = node2->data;
171                                 if(c->status.active)
172                                         send_del_subnet(c, s);
173                         }
174
175                         subnet_del(myself, s);
176                 }
177         }
178 }
179
180 static void route_mac(node_t *source, vpn_packet_t *packet)
181 {
182         subnet_t *subnet;
183
184         cp();
185
186         /* Learn source address */
187
188         if(source == myself)
189                 learn_mac((mac_t *)(&packet->data[6]));
190
191         /* Lookup destination address */
192
193         subnet = lookup_subnet_mac((mac_t *)(&packet->data[0]));
194
195         if(!subnet) {
196                 broadcast_packet(source, packet);
197                 return;
198         }
199
200         if(subnet->owner == source) {
201                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
202                 return;
203         }
204
205         send_packet(subnet->owner, packet);
206 }
207
208 /* RFC 792 */
209
210 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t code)
211 {
212         struct ip ip = {0};
213         struct icmp icmp = {0};
214         
215         struct in_addr ip_src;
216         struct in_addr ip_dst;
217         uint32_t oldlen;
218
219         if(ratelimit(3))
220                 return;
221         
222         cp();
223
224         /* Copy headers from packet into properly aligned structs on the stack */
225
226         memcpy(&ip, packet->data + ether_size, ip_size);
227
228         /* Remember original source and destination */
229         
230         ip_src = ip.ip_src;
231         ip_dst = ip.ip_dst;
232
233         oldlen = packet->len - ether_size;
234
235         if(oldlen >= IP_MSS - ip_size - icmp_size)
236                 oldlen = IP_MSS - ip_size - icmp_size;
237         
238         /* Copy first part of original contents to ICMP message */
239         
240         memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
241
242         /* Fill in IPv4 header */
243         
244         ip.ip_v = 4;
245         ip.ip_hl = ip_size / 4;
246         ip.ip_tos = 0;
247         ip.ip_len = htons(ip_size + icmp_size + oldlen);
248         ip.ip_id = 0;
249         ip.ip_off = 0;
250         ip.ip_ttl = 255;
251         ip.ip_p = IPPROTO_ICMP;
252         ip.ip_sum = 0;
253         ip.ip_src = ip_dst;
254         ip.ip_dst = ip_src;
255
256         ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
257         
258         /* Fill in ICMP header */
259         
260         icmp.icmp_type = ICMP_DEST_UNREACH;
261         icmp.icmp_code = code;
262         icmp.icmp_cksum = 0;
263         
264         icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
265         icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
266
267         /* Copy structs on stack back to packet */
268
269         memcpy(packet->data + ether_size, &ip, ip_size);
270         memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
271         
272         packet->len = ether_size + ip_size + icmp_size + oldlen;
273         
274         send_packet(source, packet);
275 }
276
277 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet)
278 {
279         subnet_t *subnet;
280
281         cp();
282
283         subnet = lookup_subnet_ipv4((ipv4_t *) &packet->data[30]);
284
285         if(!subnet) {
286                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d"),
287                                 source->name, source->hostname,
288                                 packet->data[30],
289                                 packet->data[31],
290                                 packet->data[32],
291                                 packet->data[33]);
292
293                 route_ipv4_unreachable(source, packet, ICMP_NET_UNKNOWN);
294                 return;
295         }
296         
297         if(subnet->owner == source) {
298                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
299                 return;
300         }
301
302         if(!subnet->owner->status.reachable)
303                 route_ipv4_unreachable(source, packet, ICMP_NET_UNREACH);
304
305         if(priorityinheritance)
306                 packet->priority = packet->data[15];
307
308         send_packet(subnet->owner, packet);
309 }
310
311 static void route_ipv4(node_t *source, vpn_packet_t *packet)
312 {
313         cp();
314
315         if(!checklength(source, packet, ether_size + ip_size))
316                 return;
317
318 #if 0
319         if(packet->data[30] & 0xf0 == 0xe0) {
320                 route_ipv4_multicast(source, packet);
321                 return;
322         }
323 #endif
324
325         route_ipv4_unicast(source, packet);
326 }
327
328 /* RFC 2463 */
329
330 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t code)
331 {
332         struct ip6_hdr ip6;
333         struct icmp6_hdr icmp6 = {0};
334         uint16_t checksum;      
335
336         struct {
337                 struct in6_addr ip6_src;        /* source address */
338                 struct in6_addr ip6_dst;        /* destination address */
339                 uint32_t length;
340                 uint32_t next;
341         } pseudo;
342
343         if(ratelimit(3))
344                 return;
345         
346         cp();
347
348         /* Copy headers from packet to structs on the stack */
349
350         memcpy(&ip6, packet->data + ether_size, ip6_size);
351
352         /* Remember original source and destination */
353         
354         pseudo.ip6_src = ip6.ip6_dst;
355         pseudo.ip6_dst = ip6.ip6_src;
356
357         pseudo.length = packet->len - ether_size;
358         
359         if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
360                 pseudo.length = IP_MSS - ip6_size - icmp6_size;
361         
362         /* Copy first part of original contents to ICMP message */
363         
364         memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
365
366         /* Fill in IPv6 header */
367         
368         ip6.ip6_flow = htonl(0x60000000UL);
369         ip6.ip6_plen = htons(icmp6_size + pseudo.length);
370         ip6.ip6_nxt = IPPROTO_ICMPV6;
371         ip6.ip6_hlim = 255;
372         ip6.ip6_src = pseudo.ip6_src;
373         ip6.ip6_dst = pseudo.ip6_dst;
374
375         /* Fill in ICMP header */
376         
377         icmp6.icmp6_type = ICMP6_DST_UNREACH;
378         icmp6.icmp6_code = code;
379         icmp6.icmp6_cksum = 0;
380
381         /* Create pseudo header */
382                 
383         pseudo.length = htonl(icmp6_size + pseudo.length);
384         pseudo.next = htonl(IPPROTO_ICMPV6);
385
386         /* Generate checksum */
387         
388         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
389         checksum = inet_checksum(&icmp6, icmp6_size, checksum);
390         checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
391
392         icmp6.icmp6_cksum = checksum;
393
394         /* Copy structs on stack back to packet */
395
396         memcpy(packet->data + ether_size, &ip6, ip6_size);
397         memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
398         
399         packet->len = ether_size + ip6_size + ntohl(pseudo.length);
400         
401         send_packet(source, packet);
402 }
403
404 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet)
405 {
406         subnet_t *subnet;
407
408         cp();
409
410         subnet = lookup_subnet_ipv6((ipv6_t *) &packet->data[38]);
411
412         if(!subnet) {
413                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
414                                 source->name, source->hostname,
415                                 ntohs(*(uint16_t *) &packet->data[38]),
416                                 ntohs(*(uint16_t *) &packet->data[40]),
417                                 ntohs(*(uint16_t *) &packet->data[42]),
418                                 ntohs(*(uint16_t *) &packet->data[44]),
419                                 ntohs(*(uint16_t *) &packet->data[46]),
420                                 ntohs(*(uint16_t *) &packet->data[48]),
421                                 ntohs(*(uint16_t *) &packet->data[50]),
422                                 ntohs(*(uint16_t *) &packet->data[52]));
423
424                 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH_ADDR);
425                 return;
426         }
427
428         if(subnet->owner == source) {
429                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
430                 return;
431         }
432
433         if(!subnet->owner->status.reachable)
434                 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH_NOROUTE);
435         
436         send_packet(subnet->owner, packet);
437 }
438
439 #ifdef ENABLE_MULTICAST
440 static void route_ipv6_multicast(node_t *source, vpn_packet_t *packet)
441 {
442         avl_node_t *node;
443         subnet_t *subnet, search = {0};
444
445         cp();
446
447         search.type = SUBNET_IPV6;
448         search.net.ipv6.address = *(ipv6_t *)(packet->data + ether_size + ip6_size + icmp6_size);
449         search.net.ipv6.prefixlength = 128;
450         search.owner = NULL;
451
452         ifdebug(TRAFFIC) logger(LOG_INFO, _("Multicasting packet of %d bytes from %s (%s)"), packet->len, source->name, source->hostname);
453
454         for(node = avl_search_closest_smaller_node(myself->subnet_tree, &search); node; node = node->next) {
455                 subnet = node->data;
456                 
457                 if(subnet->type != SUBNET_IPV6 || memcmp(&subnet->net.ipv6.address, packet->data + ether_size + ip6_size + icmp6_size, sizeof(ipv6_t)))
458                         break;
459                 
460                 if(subnet->owner != source)
461                         send_packet(subnet->owner, packet);
462         }
463 }
464 #endif
465
466 /* RFC 2461 */
467
468 static void route_neighborsol(node_t *source, vpn_packet_t *packet)
469 {
470         struct ip6_hdr ip6;
471         struct nd_neighbor_solicit ns;
472         struct nd_opt_hdr opt;
473         subnet_t *subnet;
474         uint16_t checksum;
475
476         struct {
477                 struct in6_addr ip6_src;        /* source address */
478                 struct in6_addr ip6_dst;        /* destination address */
479                 uint32_t length;
480                 uint32_t next;
481         } pseudo;
482
483         cp();
484
485         if(!checklength(source, packet, ether_size + ip6_size + ns_size + opt_size + ETH_ALEN))
486                 return;
487         
488         if(source != myself) {
489                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got neighbor solicitation request from %s (%s) while in router mode!"), source->name, source->hostname);
490                 return;
491         }
492
493         /* Copy headers from packet to structs on the stack */
494
495         memcpy(&ip6, packet->data + ether_size, ip6_size);
496         memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
497         memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
498
499         /* First, snatch the source address from the neighbor solicitation packet */
500
501         if(overwrite_mac)
502                 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
503
504         /* Check if this is a valid neighbor solicitation request */
505
506         if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
507            opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR) {
508                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
509                 return;
510         }
511
512         /* Create pseudo header */
513
514         pseudo.ip6_src = ip6.ip6_src;
515         pseudo.ip6_dst = ip6.ip6_dst;
516         pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
517         pseudo.next = htonl(IPPROTO_ICMPV6);
518
519         /* Generate checksum */
520
521         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
522         checksum = inet_checksum(&ns, ns_size, checksum);
523         checksum = inet_checksum(&opt, opt_size, checksum);
524         checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
525
526         if(checksum) {
527                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
528                 return;
529         }
530
531         /* Check if the IPv6 address exists on the VPN */
532
533         subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
534
535         if(!subnet) {
536                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
537                                    ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
538                                    ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
539                                    ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
540                                    ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
541                                    ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
542                                    ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
543                                    ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
544                                    ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
545
546                 return;
547         }
548
549         /* Check if it is for our own subnet */
550
551         if(subnet->owner == myself)
552                 return;                                 /* silently ignore */
553
554         /* Create neighbor advertation reply */
555
556         memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN);        /* copy destination address */
557         packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
558
559         ip6.ip6_dst = ip6.ip6_src;                      /* swap destination and source protocoll address */
560         ip6.ip6_src = ns.nd_ns_target;
561
562         memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN);   /* add fake source hard addr */
563
564         ns.nd_ns_cksum = 0;
565         ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
566         ns.nd_ns_reserved = htonl(0x40000000UL);        /* Set solicited flag */
567         opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
568
569         /* Create pseudo header */
570
571         pseudo.ip6_src = ip6.ip6_src;
572         pseudo.ip6_dst = ip6.ip6_dst;
573         pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
574         pseudo.next = htonl(IPPROTO_ICMPV6);
575
576         /* Generate checksum */
577
578         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
579         checksum = inet_checksum(&ns, ns_size, checksum);
580         checksum = inet_checksum(&opt, opt_size, checksum);
581         checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
582
583         ns.nd_ns_hdr.icmp6_cksum = checksum;
584
585         /* Copy structs on stack back to packet */
586
587         memcpy(packet->data + ether_size, &ip6, ip6_size);
588         memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
589         memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
590
591         send_packet(source, packet);
592 }
593
594 /* RFC 2710 */
595
596 #ifdef ENABLE_MULTICAST
597 static void route_membershipreport(node_t *source, vpn_packet_t *packet)
598 {
599         struct ip6_hdr ip6;
600         struct icmp6_hdr icmp6;
601         subnet_t *subnet, search = {0};
602         uint16_t checksum;
603
604         struct {
605                 struct in6_addr ip6_src;        /* source address */
606                 struct in6_addr ip6_dst;        /* destination address */
607                 uint32_t length;
608                 uint32_t next;
609         } pseudo;
610
611         cp();
612
613         if(!checklength(source, packet, ether_size + ip6_size + icmp6_size + sizeof(ipv6_t)))
614                 return;
615         
616         if(source != myself) {
617                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got membership report from %s (%s) while in router mode!"), source->name, source->hostname);
618                 return;
619         }
620
621         /* Copy headers from packet to structs on the stack */
622
623         memcpy(&ip6, packet->data + ether_size, ip6_size);
624         memcpy(&icmp6, packet->data + ether_size + ip6_size + 8, icmp6_size);
625
626         /* Create pseudo header */
627
628         pseudo.ip6_src = ip6.ip6_src;
629         pseudo.ip6_dst = ip6.ip6_dst;
630         pseudo.length = htonl(icmp6_size + sizeof(ipv6_t));
631         pseudo.next = htonl(IPPROTO_ICMPV6);
632
633         /* Generate checksum */
634
635         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
636         checksum = inet_checksum(&icmp6, icmp6_size, checksum);
637         checksum = inet_checksum(packet->data + ether_size + ip6_size + 8 + icmp6_size, sizeof(ipv6_t), checksum);
638
639         if(checksum) {
640                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for membership report"));
641                 return;
642         }
643
644         /* Check if the IPv6 address exists on the VPN */
645
646         search.type = SUBNET_IPV6;
647         search.net.ipv6.address = *(ipv6_t *)(packet->data + ether_size + ip6_size + 8 + icmp6_size);
648         search.net.ipv6.prefixlength = 128;
649         search.owner = myself;
650
651         subnet = avl_search(myself->subnet_tree, &search);
652
653         if(!subnet) {
654                 avl_node_t *node;
655                 connection_t *c;
656
657                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Learned new IPv6 multicast address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
658                                 ntohs(*(uint16_t *) &packet->data[70]),
659                                 ntohs(*(uint16_t *) &packet->data[72]),
660                                 ntohs(*(uint16_t *) &packet->data[74]),
661                                 ntohs(*(uint16_t *) &packet->data[76]),
662                                 ntohs(*(uint16_t *) &packet->data[78]),
663                                 ntohs(*(uint16_t *) &packet->data[80]),
664                                 ntohs(*(uint16_t *) &packet->data[82]),
665                                 ntohs(*(uint16_t *) &packet->data[84]));
666
667                 subnet = new_subnet();
668                 subnet->type = SUBNET_IPV6;
669                 subnet->net.ipv6.address = *(ipv6_t *)(packet->data + ether_size + ip6_size + 8 + icmp6_size);
670                 subnet->net.ipv6.prefixlength = 128;
671                 subnet->expires = now + multicastexpire;
672                 subnet_add(myself, subnet);
673
674                 /* And tell all other tinc daemons it's ours */
675
676                 for(node = connection_tree->head; node; node = node->next) {
677                         c = node->data;
678                         if(c->status.active)
679                                 send_add_subnet(c, subnet);
680                 }
681         }
682
683         if(subnet->expires)
684                 subnet->expires = now + multicastexpire;
685 }
686 #endif
687
688 static void route_ipv6(node_t *source, vpn_packet_t *packet)
689 {
690         cp();
691
692         if(!checklength(source, packet, ether_size + ip6_size))
693                 return;
694
695         if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
696                 route_neighborsol(source, packet);
697                 return;
698         }
699
700 #ifdef ENABLE_MULTICAST 
701         if(packet->data[20] == IPPROTO_HOPOPTS && checklength(source, packet, ether_size + ip6_size + 8)
702                         && packet->data[54] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + 8 + icmp6_size)
703                         && packet->data[62] == ICMP6_MEMBERSHIP_REPORT) {
704                 route_membershipreport(source, packet);
705                 return;
706         }
707         
708         if(packet->data[38] == 0xff && packet->data[39] & 0x0c) {
709                 route_ipv6_multicast(source, packet);
710                 return;
711         }
712 #endif
713
714         route_ipv6_unicast(source, packet);
715 }
716
717 /* RFC 826 */
718
719 static void route_arp(node_t *source, vpn_packet_t *packet)
720 {
721         struct ether_arp arp;
722         subnet_t *subnet;
723         struct in_addr addr;
724
725         cp();
726
727         if(!checklength(source, packet, ether_size + arp_size))
728                 return;
729
730         if(source != myself) {
731                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got ARP request from %s (%s) while in router mode!"), source->name, source->hostname);
732                 return;
733         }
734
735         /* First, snatch the source address from the ARP packet */
736
737         if(overwrite_mac)
738                 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
739
740         /* Copy headers from packet to structs on the stack */
741
742         memcpy(&arp, packet->data + ether_size, arp_size);
743
744         /* Check if this is a valid ARP request */
745
746         if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
747            arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
748                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
749                 return;
750         }
751
752         /* Check if the IPv4 address exists on the VPN */
753
754         subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
755
756         if(!subnet) {
757                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
758                                    arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
759                                    arp.arp_tpa[3]);
760                 return;
761         }
762
763         /* Check if it is for our own subnet */
764
765         if(subnet->owner == myself)
766                 return;                                 /* silently ignore */
767
768         memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN);        /* copy destination address */
769         packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
770
771         memcpy(&addr, arp.arp_tpa, sizeof(addr));       /* save protocol addr */
772         memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
773         memcpy(arp.arp_spa, &addr, sizeof(addr));       /* ... */
774
775         memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN);     /* set target hard/proto addr */
776         memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
777         arp.arp_op = htons(ARPOP_REPLY);
778
779         /* Copy structs on stack back to packet */
780
781         memcpy(packet->data + ether_size, &arp, arp_size);
782
783         send_packet(source, packet);
784 }
785
786 void route(node_t *source, vpn_packet_t *packet)
787 {
788         cp();
789
790         if(!checklength(source, packet, ether_size))
791                 return;
792
793         switch (routing_mode) {
794                 case RMODE_ROUTER:
795                         {
796                                 uint16_t type;
797
798                                 type = ntohs(*((uint16_t *)(&packet->data[12])));
799                                 switch (type) {
800                                         case ETH_P_ARP:
801                                                 route_arp(source, packet);
802                                                 break;
803
804                                         case ETH_P_IP:
805                                                 route_ipv4(source, packet);
806                                                 break;
807
808                                         case ETH_P_IPV6:
809                                                 route_ipv6(source, packet);
810                                                 break;
811
812                                         default:
813                                                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown type %hx"), source->name, source->hostname, type);
814                                                 break;
815                                 }
816                         }
817                         break;
818
819                 case RMODE_SWITCH:
820                         route_mac(source, packet);
821                         break;
822
823                 case RMODE_HUB:
824                         broadcast_packet(source, packet);
825                         break;
826         }
827 }