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