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