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