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