4e7f7e585db410c41f733a582aae7c2540064e43
[tinc] / src / route.c
1 /*
2     route.c -- routing
3     Copyright (C) 2000-2005 Ivo Timmermans,
4                   2000-2010 Guus Sliepen <guus@tinc-vpn.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 along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "avl_tree.h"
24 #include "connection.h"
25 #include "ethernet.h"
26 #include "ipv4.h"
27 #include "ipv6.h"
28 #include "logger.h"
29 #include "net.h"
30 #include "protocol.h"
31 #include "route.h"
32 #include "subnet.h"
33 #include "utils.h"
34
35 rmode_t routing_mode = RMODE_ROUTER;
36 fmode_t forwarding_mode = FMODE_INTERNAL;
37 bool directonly = false;
38 bool priorityinheritance = false;
39 int macexpire = 600;
40 bool overwrite_mac = false;
41 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
42
43 /* Sizes of various headers */
44
45 static const size_t ether_size = sizeof(struct ether_header);
46 static const size_t arp_size = sizeof(struct ether_arp);
47 static const size_t ip_size = sizeof(struct ip);
48 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
49 static const size_t ip6_size = sizeof(struct ip6_hdr);
50 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
51 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
52 static const size_t opt_size = sizeof(struct nd_opt_hdr);
53 #define max(a, b) ((a) > (b) ? (a) : (b))
54
55 /* RFC 1071 */
56
57 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
58         uint16_t *p = data;
59         uint32_t checksum = prevsum ^ 0xFFFF;
60
61         while(len >= 2) {
62                 checksum += *p++;
63                 len -= 2;
64         }
65         
66         if(len)
67                 checksum += *(uint8_t *)p;
68
69         while(checksum >> 16)
70                 checksum = (checksum & 0xFFFF) + (checksum >> 16);
71
72         return ~checksum;
73 }
74
75 static bool ratelimit(int frequency) {
76         static time_t lasttime = 0;
77         static int count = 0;
78         
79         if(lasttime == now) {
80                 if(++count > frequency)
81                         return true;
82         } else {
83                 lasttime = now;
84                 count = 0;
85         }
86
87         return false;
88 }
89
90 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
91         if(packet->len < length) {
92                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
93                 return false;
94         } else
95                 return true;
96 }
97
98 static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
99         if(!source || !via || !(via->options & OPTION_CLAMP_MSS))
100                 return;
101
102         uint16_t mtu = source->mtu;
103         if(via != myself && via->mtu < mtu)
104                 mtu = via->mtu;
105
106         /* Find TCP header */
107         int start = 0;
108         uint16_t type = packet->data[12] << 8 | packet->data[13];
109
110         if(type == ETH_P_IP && packet->data[23] == 6)
111                 start = 14 + (packet->data[14] & 0xf) * 4;
112         else if(type == ETH_P_IPV6 && packet->data[20] == 6)
113                 start = 14 + 40;
114
115         if(!start || packet->len <= start + 20)
116                 return;
117
118         /* Use data offset field to calculate length of options field */
119         int len = ((packet->data[start + 12] >> 4) - 5) * 4;
120
121         if(packet->len < start + 20 + len)
122                 return;
123
124         /* Search for MSS option header */
125         for(int i = 0; i < len;) {
126                 if(packet->data[start + 20 + i] == 0)
127                         break;
128
129                 if(packet->data[start + 20 + i] == 1) {
130                         i++;
131                         continue;
132                 }
133
134                 if(i > len - 2 || i > len - packet->data[start + 21 + i])
135                         break;
136
137                 if(packet->data[start + 20 + i] != 2) {
138                         if(packet->data[start + 21 + i] < 2)
139                                 break;
140                         i += packet->data[start + 21 + i];
141                         continue;
142                 }
143
144                 if(packet->data[start + 21] != 4)
145                         break;
146
147                 /* Found it */
148                 uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
149                 uint16_t newmss = mtu - start - 20;
150                 uint16_t csum = packet->data[start + 16] << 8 | packet->data[start + 17];
151
152                 if(oldmss <= newmss)
153                         break;
154                 
155                 ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
156
157                 /* Update the MSS value and the checksum */
158                 packet->data[start + 22 + i] = newmss >> 8;
159                 packet->data[start + 23 + i] = newmss & 0xff;
160                 csum ^= 0xffff;
161                 csum -= oldmss;
162                 csum += newmss;
163                 csum ^= 0xffff;
164                 packet->data[start + 16] = csum >> 8;
165                 packet->data[start + 17] = csum & 0xff;
166                 break;
167         }
168 }
169
170 static void swap_mac_addresses(vpn_packet_t *packet) {
171         mac_t tmp;
172         memcpy(&tmp, &packet->data[0], sizeof tmp);
173         memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
174         memcpy(&packet->data[6], &tmp, sizeof tmp);
175 }
176         
177 static void learn_mac(mac_t *address) {
178         subnet_t *subnet;
179         avl_node_t *node;
180         connection_t *c;
181
182         subnet = lookup_subnet_mac(myself, address);
183
184         /* If we don't know this MAC address yet, store it */
185
186         if(!subnet) {
187                 ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx",
188                                    address->x[0], address->x[1], address->x[2], address->x[3],
189                                    address->x[4], address->x[5]);
190
191                 subnet = new_subnet();
192                 subnet->type = SUBNET_MAC;
193                 subnet->expires = now + macexpire;
194                 subnet->net.mac.address = *address;
195                 subnet->weight = 10;
196                 subnet_add(myself, subnet);
197                 subnet_update(myself, subnet, true);
198
199                 /* And tell all other tinc daemons it's our MAC */
200
201                 for(node = connection_tree->head; node; node = node->next) {
202                         c = node->data;
203                         if(c->status.active)
204                                 send_add_subnet(c, subnet);
205                 }
206         }
207
208         if(subnet->expires)
209                 subnet->expires = now + macexpire;
210 }
211
212 void age_subnets(void) {
213         subnet_t *s;
214         connection_t *c;
215         avl_node_t *node, *next, *node2;
216
217         for(node = myself->subnet_tree->head; node; node = next) {
218                 next = node->next;
219                 s = node->data;
220                 if(s->expires && s->expires < now) {
221                         ifdebug(TRAFFIC) {
222                                 char netstr[MAXNETSTR];
223                                 if(net2str(netstr, sizeof netstr, s))
224                                         logger(LOG_INFO, "Subnet %s expired", netstr);
225                         }
226
227                         for(node2 = connection_tree->head; node2; node2 = node2->next) {
228                                 c = node2->data;
229                                 if(c->status.active)
230                                         send_del_subnet(c, s);
231                         }
232
233                         subnet_update(myself, s, false);
234                         subnet_del(myself, s);
235                 }
236         }
237 }
238
239 /* RFC 792 */
240
241 static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
242         struct ip ip = {0};
243         struct icmp icmp = {0};
244         
245         struct in_addr ip_src;
246         struct in_addr ip_dst;
247         uint32_t oldlen;
248
249         if(ratelimit(3))
250                 return;
251         
252         /* Swap Ethernet source and destination addresses */
253
254         swap_mac_addresses(packet);
255
256         /* Copy headers from packet into properly aligned structs on the stack */
257
258         memcpy(&ip, packet->data + ether_size, ip_size);
259
260         /* Remember original source and destination */
261         
262         ip_src = ip.ip_src;
263         ip_dst = ip.ip_dst;
264
265         oldlen = packet->len - ether_size;
266
267         if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
268                 icmp.icmp_nextmtu = htons(packet->len - ether_size);
269
270         if(oldlen >= IP_MSS - ip_size - icmp_size)
271                 oldlen = IP_MSS - ip_size - icmp_size;
272         
273         /* Copy first part of original contents to ICMP message */
274         
275         memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
276
277         /* Fill in IPv4 header */
278         
279         ip.ip_v = 4;
280         ip.ip_hl = ip_size / 4;
281         ip.ip_tos = 0;
282         ip.ip_len = htons(ip_size + icmp_size + oldlen);
283         ip.ip_id = 0;
284         ip.ip_off = 0;
285         ip.ip_ttl = 255;
286         ip.ip_p = IPPROTO_ICMP;
287         ip.ip_sum = 0;
288         ip.ip_src = ip_dst;
289         ip.ip_dst = ip_src;
290
291         ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
292         
293         /* Fill in ICMP header */
294         
295         icmp.icmp_type = type;
296         icmp.icmp_code = code;
297         icmp.icmp_cksum = 0;
298         
299         icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
300         icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
301
302         /* Copy structs on stack back to packet */
303
304         memcpy(packet->data + ether_size, &ip, ip_size);
305         memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
306         
307         packet->len = ether_size + ip_size + icmp_size + oldlen;
308
309         send_packet(source, packet);
310 }
311
312 /* RFC 791 */
313
314 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet) {
315         struct ip ip;
316         vpn_packet_t fragment;
317         int len, maxlen, todo;
318         uint8_t *offset;
319         uint16_t ip_off, origf;
320         
321         memcpy(&ip, packet->data + ether_size, ip_size);
322         fragment.priority = packet->priority;
323
324         if(ip.ip_hl != ip_size / 4)
325                 return;
326         
327         todo = ntohs(ip.ip_len) - ip_size;
328
329         if(ether_size + ip_size + todo != packet->len) {
330                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%zd)", packet->len, ether_size + ip_size + todo);
331                 return;
332         }
333
334         ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
335
336         offset = packet->data + ether_size + ip_size;
337         maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
338         ip_off = ntohs(ip.ip_off);
339         origf = ip_off & ~IP_OFFMASK;
340         ip_off &= IP_OFFMASK;
341         
342         while(todo) {
343                 len = todo > maxlen ? maxlen : todo;
344                 memcpy(fragment.data + ether_size + ip_size, offset, len);
345                 todo -= len;
346                 offset += len;
347
348                 ip.ip_len = htons(ip_size + len);
349                 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
350                 ip.ip_sum = 0;
351                 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
352                 memcpy(fragment.data, packet->data, ether_size);
353                 memcpy(fragment.data + ether_size, &ip, ip_size);
354                 fragment.len = ether_size + ip_size + len;
355
356                 send_packet(dest, &fragment);
357
358                 ip_off += len / 8;
359         }       
360 }
361
362 static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
363         subnet_t *subnet;
364         node_t *via;
365         ipv4_t dest;
366
367         memcpy(&dest, &packet->data[30], sizeof dest);
368         subnet = lookup_subnet_ipv4(&dest);
369
370         if(!subnet) {
371                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
372                                 source->name, source->hostname,
373                                 dest.x[0],
374                                 dest.x[1],
375                                 dest.x[2],
376                                 dest.x[3]);
377
378                 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
379                 return;
380         }
381         
382         if(subnet->owner == source) {
383                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
384                 return;
385         }
386
387         if(!subnet->owner->status.reachable)
388                 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
389
390         if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
391                 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
392
393         if(priorityinheritance)
394                 packet->priority = packet->data[15];
395
396         via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
397         
398         if(directonly && subnet->owner != via)
399                 return route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_ANO);
400
401         if(via && packet->len > max(via->mtu, 590) && via != myself) {
402                 ifdebug(TRAFFIC) logger(LOG_INFO, "Packet for %s (%s) length %d larger than MTU %d", subnet->owner->name, subnet->owner->hostname, packet->len, via->mtu);
403                 if(packet->data[20] & 0x40) {
404                         packet->len = max(via->mtu, 590);
405                         route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
406                 } else {
407                         fragment_ipv4_packet(via, packet);
408                 }
409
410                 return;
411         }
412
413         clamp_mss(source, via, packet);
414  
415         send_packet(subnet->owner, packet);
416 }
417
418 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
419         if(!checklength(source, packet, ether_size + ip_size))
420                 return;
421
422         if(((packet->data[30] & 0xf0) == 0xe0) || (
423                         packet->data[30] == 255 &&
424                         packet->data[31] == 255 &&
425                         packet->data[32] == 255 &&
426                         packet->data[33] == 255))
427                 broadcast_packet(source, packet);
428         else
429                 route_ipv4_unicast(source, packet);
430 }
431
432 /* RFC 2463 */
433
434 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code) {
435         struct ip6_hdr ip6;
436         struct icmp6_hdr icmp6 = {0};
437         uint16_t checksum;      
438
439         struct {
440                 struct in6_addr ip6_src;        /* source address */
441                 struct in6_addr ip6_dst;        /* destination address */
442                 uint32_t length;
443                 uint32_t next;
444         } pseudo;
445
446         if(ratelimit(3))
447                 return;
448         
449         /* Swap Ethernet source and destination addresses */
450
451         swap_mac_addresses(packet);
452
453         /* Copy headers from packet to structs on the stack */
454
455         memcpy(&ip6, packet->data + ether_size, ip6_size);
456
457         /* Remember original source and destination */
458         
459         pseudo.ip6_src = ip6.ip6_dst;
460         pseudo.ip6_dst = ip6.ip6_src;
461
462         pseudo.length = packet->len - ether_size;
463
464         if(type == ICMP6_PACKET_TOO_BIG)
465                 icmp6.icmp6_mtu = htonl(pseudo.length);
466         
467         if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
468                 pseudo.length = IP_MSS - ip6_size - icmp6_size;
469         
470         /* Copy first part of original contents to ICMP message */
471         
472         memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
473
474         /* Fill in IPv6 header */
475         
476         ip6.ip6_flow = htonl(0x60000000UL);
477         ip6.ip6_plen = htons(icmp6_size + pseudo.length);
478         ip6.ip6_nxt = IPPROTO_ICMPV6;
479         ip6.ip6_hlim = 255;
480         ip6.ip6_src = pseudo.ip6_src;
481         ip6.ip6_dst = pseudo.ip6_dst;
482
483         /* Fill in ICMP header */
484         
485         icmp6.icmp6_type = type;
486         icmp6.icmp6_code = code;
487         icmp6.icmp6_cksum = 0;
488
489         /* Create pseudo header */
490                 
491         pseudo.length = htonl(icmp6_size + pseudo.length);
492         pseudo.next = htonl(IPPROTO_ICMPV6);
493
494         /* Generate checksum */
495         
496         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
497         checksum = inet_checksum(&icmp6, icmp6_size, checksum);
498         checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
499
500         icmp6.icmp6_cksum = checksum;
501
502         /* Copy structs on stack back to packet */
503
504         memcpy(packet->data + ether_size, &ip6, ip6_size);
505         memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
506         
507         packet->len = ether_size + ip6_size + ntohl(pseudo.length);
508         
509         send_packet(source, packet);
510 }
511
512 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
513         subnet_t *subnet;
514         node_t *via;
515         ipv6_t dest;
516
517         memcpy(&dest, &packet->data[38], sizeof dest);
518         subnet = lookup_subnet_ipv6(&dest);
519
520         if(!subnet) {
521                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
522                                 source->name, source->hostname,
523                                 ntohs(dest.x[0]),
524                                 ntohs(dest.x[1]),
525                                 ntohs(dest.x[2]),
526                                 ntohs(dest.x[3]),
527                                 ntohs(dest.x[4]),
528                                 ntohs(dest.x[5]),
529                                 ntohs(dest.x[6]),
530                                 ntohs(dest.x[7]));
531
532                 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
533                 return;
534         }
535
536         if(subnet->owner == source) {
537                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
538                 return;
539         }
540
541         if(!subnet->owner->status.reachable)
542                 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
543
544         if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
545                 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
546
547         via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
548         
549         if(directonly && subnet->owner != via)
550                 return route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
551
552         if(via && packet->len > max(via->mtu, 1294) && via != myself) {
553                 ifdebug(TRAFFIC) logger(LOG_INFO, "Packet for %s (%s) length %d larger than MTU %d", subnet->owner->name, subnet->owner->hostname, packet->len, via->mtu);
554                 packet->len = max(via->mtu, 1294);
555                 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
556                 return;
557         }
558
559         clamp_mss(source, via, packet);
560  
561         send_packet(subnet->owner, packet);
562 }
563
564 /* RFC 2461 */
565
566 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
567         struct ip6_hdr ip6;
568         struct nd_neighbor_solicit ns;
569         struct nd_opt_hdr opt;
570         subnet_t *subnet;
571         uint16_t checksum;
572         bool has_opt;
573
574         struct {
575                 struct in6_addr ip6_src;        /* source address */
576                 struct in6_addr ip6_dst;        /* destination address */
577                 uint32_t length;
578                 uint32_t next;
579         } pseudo;
580
581         if(!checklength(source, packet, ether_size + ip6_size + ns_size))
582                 return;
583         
584         has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
585         
586         if(source != myself) {
587                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
588                 return;
589         }
590
591         /* Copy headers from packet to structs on the stack */
592
593         memcpy(&ip6, packet->data + ether_size, ip6_size);
594         memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
595         if(has_opt)
596                 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
597
598         /* First, snatch the source address from the neighbor solicitation packet */
599
600         if(overwrite_mac)
601                 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
602
603         /* Check if this is a valid neighbor solicitation request */
604
605         if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
606            (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
607                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
608                 return;
609         }
610
611         /* Create pseudo header */
612
613         pseudo.ip6_src = ip6.ip6_src;
614         pseudo.ip6_dst = ip6.ip6_dst;
615         if(has_opt)
616                 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
617         else
618                 pseudo.length = htonl(ns_size);
619         pseudo.next = htonl(IPPROTO_ICMPV6);
620
621         /* Generate checksum */
622
623         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
624         checksum = inet_checksum(&ns, ns_size, checksum);
625         if(has_opt) {
626                 checksum = inet_checksum(&opt, opt_size, checksum);
627                 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
628         }
629
630         if(checksum) {
631                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
632                 return;
633         }
634
635         /* Check if the IPv6 address exists on the VPN */
636
637         subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
638
639         if(!subnet) {
640                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
641                                    ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
642                                    ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
643                                    ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
644                                    ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
645                                    ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
646                                    ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
647                                    ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
648                                    ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
649
650                 return;
651         }
652
653         /* Check if it is for our own subnet */
654
655         if(subnet->owner == myself)
656                 return;                                 /* silently ignore */
657
658         /* Create neighbor advertation reply */
659
660         memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN);        /* copy destination address */
661         packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
662
663         ip6.ip6_dst = ip6.ip6_src;                      /* swap destination and source protocoll address */
664         ip6.ip6_src = ns.nd_ns_target;
665
666         if(has_opt)
667                 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN);   /* add fake source hard addr */
668
669         ns.nd_ns_cksum = 0;
670         ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
671         ns.nd_ns_reserved = htonl(0x40000000UL);        /* Set solicited flag */
672         opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
673
674         /* Create pseudo header */
675
676         pseudo.ip6_src = ip6.ip6_src;
677         pseudo.ip6_dst = ip6.ip6_dst;
678         if(has_opt)
679                 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
680         else
681                 pseudo.length = htonl(ns_size);
682         pseudo.next = htonl(IPPROTO_ICMPV6);
683
684         /* Generate checksum */
685
686         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
687         checksum = inet_checksum(&ns, ns_size, checksum);
688         if(has_opt) {
689                 checksum = inet_checksum(&opt, opt_size, checksum);
690                 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
691         }
692
693         ns.nd_ns_hdr.icmp6_cksum = checksum;
694
695         /* Copy structs on stack back to packet */
696
697         memcpy(packet->data + ether_size, &ip6, ip6_size);
698         memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
699         if(has_opt)
700                 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
701
702         send_packet(source, packet);
703 }
704
705 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
706         if(!checklength(source, packet, ether_size + ip6_size))
707                 return;
708
709         if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
710                 route_neighborsol(source, packet);
711                 return;
712         }
713
714         if(packet->data[38] == 255)
715                 broadcast_packet(source, packet);
716         else
717                 route_ipv6_unicast(source, packet);
718 }
719
720 /* RFC 826 */
721
722 static void route_arp(node_t *source, vpn_packet_t *packet) {
723         struct ether_arp arp;
724         subnet_t *subnet;
725         struct in_addr addr;
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 static void route_mac(node_t *source, vpn_packet_t *packet) {
787         subnet_t *subnet;
788         mac_t dest;
789
790         /* Learn source address */
791
792         if(source == myself) {
793                 mac_t src;
794                 memcpy(&src, &packet->data[6], sizeof src);
795                 learn_mac(&src);
796         }
797
798         /* Lookup destination address */
799
800         memcpy(&dest, &packet->data[0], sizeof dest);
801         subnet = lookup_subnet_mac(NULL, &dest);
802
803         if(!subnet) {
804                 broadcast_packet(source, packet);
805                 return;
806         }
807
808         if(subnet->owner == source) {
809                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
810                 return;
811         }
812
813         if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
814                 return;
815
816         // Handle packets larger than PMTU
817
818         node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
819
820         if(directonly && subnet->owner != via)
821                 return;
822         
823         if(via && packet->len > via->mtu && via != myself) {
824                 ifdebug(TRAFFIC) logger(LOG_INFO, "Packet for %s (%s) length %d larger than MTU %d", subnet->owner->name, subnet->owner->hostname, packet->len, via->mtu);
825                 uint16_t type = packet->data[12] << 8 | packet->data[13];
826                 if(type == ETH_P_IP && packet->len > 590) {
827                         if(packet->data[20] & 0x40) {
828                                 packet->len = via->mtu;
829                                 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
830                         } else {
831                                 fragment_ipv4_packet(via, packet);
832                         }
833                         return;
834                 } else if(type == ETH_P_IPV6 && packet->len > 1294) {
835                         packet->len = via->mtu;
836                         route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
837                         return;
838                 }
839         }
840
841         clamp_mss(source, via, packet);
842  
843         send_packet(subnet->owner, packet);
844 }
845
846 void route(node_t *source, vpn_packet_t *packet) {
847         if(forwarding_mode == FMODE_KERNEL) {
848                 send_packet(myself, packet);
849                 return;
850         }
851
852         if(!checklength(source, packet, ether_size))
853                 return;
854
855         switch (routing_mode) {
856                 case RMODE_ROUTER:
857                         {
858                                 uint16_t type = packet->data[12] << 8 | packet->data[13];
859
860                                 switch (type) {
861                                         case ETH_P_ARP:
862                                                 route_arp(source, packet);
863                                                 break;
864
865                                         case ETH_P_IP:
866                                                 route_ipv4(source, packet);
867                                                 break;
868
869                                         case ETH_P_IPV6:
870                                                 route_ipv6(source, packet);
871                                                 break;
872
873                                         default:
874                                                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
875                                                 break;
876                                 }
877                         }
878                         break;
879
880                 case RMODE_SWITCH:
881                         route_mac(source, packet);
882                         break;
883
884                 case RMODE_HUB:
885                         broadcast_packet(source, packet);
886                         break;
887         }
888 }