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