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