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