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