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