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