Fix all warnings when compiling with -Wall -W -pedantic.
[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 = {0};
120         struct icmp icmp = {0};
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 = {0};
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                 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
636                 return;
637         }
638
639         if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
640                 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
641                 return;
642         }
643
644         if(decrement_ttl && source != myself && subnet->owner != myself)
645                 if(!do_decrement_ttl(source, packet)) {
646                         return;
647                 }
648
649         if(priorityinheritance) {
650                 packet->priority = packet->data[15];
651         }
652
653         via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
654
655         if(via == source) {
656                 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
657                 return;
658         }
659
660         if(directonly && subnet->owner != via) {
661                 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
662                 return;
663         }
664
665         if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
666                 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);
667
668                 if(packet->data[20] & 0x40) {
669                         packet->len = MAX(via->mtu, 590);
670                         route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
671                 } else {
672                         fragment_ipv4_packet(via, packet, ether_size);
673                 }
674
675                 return;
676         }
677
678         clamp_mss(source, via, packet);
679
680         send_packet(subnet->owner, packet);
681 }
682
683 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
684         if(!checklength(source, packet, ether_size + ip_size)) {
685                 return;
686         }
687
688         if(broadcast_mode && (((packet->data[30] & 0xf0) == 0xe0) || (
689                                       packet->data[30] == 255 &&
690                                       packet->data[31] == 255 &&
691                                       packet->data[32] == 255 &&
692                                       packet->data[33] == 255))) {
693                 route_broadcast(source, packet);
694         } else {
695                 route_ipv4_unicast(source, packet);
696         }
697 }
698
699 static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
700         subnet_t *subnet;
701         node_t *via;
702         ipv6_t dest;
703
704         memcpy(&dest, &packet->data[38], sizeof(dest));
705         subnet = lookup_subnet_ipv6(&dest);
706
707         if(!subnet) {
708                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
709                                         source->name, source->hostname,
710                                         ntohs(dest.x[0]),
711                                         ntohs(dest.x[1]),
712                                         ntohs(dest.x[2]),
713                                         ntohs(dest.x[3]),
714                                         ntohs(dest.x[4]),
715                                         ntohs(dest.x[5]),
716                                         ntohs(dest.x[6]),
717                                         ntohs(dest.x[7]));
718
719                 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
720                 return;
721         }
722
723         if(subnet->owner == source) {
724                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
725                 return;
726         }
727
728         if(!subnet->owner->status.reachable) {
729                 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
730                 return;
731         }
732
733         if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
734                 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
735                 return;
736         }
737
738         if(decrement_ttl && source != myself && subnet->owner != myself) {
739                 if(!do_decrement_ttl(source, packet)) {
740                         return;
741                 }
742         }
743
744         if(priorityinheritance) {
745                 packet->priority = ((packet->data[14] & 0x0f) << 4) | (packet->data[15] >> 4);
746         }
747
748         via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
749
750         if(via == source) {
751                 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
752                 return;
753         }
754
755         if(directonly && subnet->owner != via) {
756                 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
757                 return;
758         }
759
760         if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
761                 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);
762                 packet->len = MAX(via->mtu, 1294);
763                 route_ipv6_unreachable(source, packet, ether_size, ICMP6_PACKET_TOO_BIG, 0);
764                 return;
765         }
766
767         clamp_mss(source, via, packet);
768
769         send_packet(subnet->owner, packet);
770 }
771
772 /* RFC 2461 */
773
774 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
775         struct ip6_hdr ip6;
776         struct nd_neighbor_solicit ns;
777         struct nd_opt_hdr opt;
778         subnet_t *subnet;
779         uint16_t checksum;
780         bool has_opt;
781
782         struct {
783                 struct in6_addr ip6_src;        /* source address */
784                 struct in6_addr ip6_dst;        /* destination address */
785                 uint32_t length;
786                 uint32_t next;
787         } pseudo;
788
789         if(!checklength(source, packet, ether_size + ip6_size + ns_size)) {
790                 return;
791         }
792
793         has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
794
795         if(source != myself) {
796                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
797                 return;
798         }
799
800         /* Copy headers from packet to structs on the stack */
801
802         memcpy(&ip6, packet->data + ether_size, ip6_size);
803         memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
804
805         if(has_opt) {
806                 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
807         }
808
809         /* First, snatch the source address from the neighbor solicitation packet */
810
811         if(overwrite_mac) {
812                 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
813         }
814
815         /* Check if this is a valid neighbor solicitation request */
816
817         if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
818                         (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
819                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
820                 return;
821         }
822
823         /* Create pseudo header */
824
825         pseudo.ip6_src = ip6.ip6_src;
826         pseudo.ip6_dst = ip6.ip6_dst;
827
828         if(has_opt) {
829                 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
830         } else {
831                 pseudo.length = htonl(ns_size);
832         }
833
834         pseudo.next = htonl(IPPROTO_ICMPV6);
835
836         /* Generate checksum */
837
838         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
839         checksum = inet_checksum(&ns, ns_size, checksum);
840
841         if(has_opt) {
842                 checksum = inet_checksum(&opt, opt_size, checksum);
843                 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
844         }
845
846         if(checksum) {
847                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
848                 return;
849         }
850
851         /* Check if the IPv6 address exists on the VPN */
852
853         subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
854
855         if(!subnet) {
856                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
857                                         ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
858                                         ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
859                                         ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
860                                         ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
861                                         ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
862                                         ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
863                                         ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
864                                         ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
865
866                 return;
867         }
868
869         /* Check if it is for our own subnet */
870
871         if(subnet->owner == myself) {
872                 return;        /* silently ignore */
873         }
874
875         if(decrement_ttl)
876                 if(!do_decrement_ttl(source, packet)) {
877                         return;
878                 }
879
880         /* Create neighbor advertation reply */
881
882         memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN);        /* copy destination address */
883         packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
884
885         ip6.ip6_dst = ip6.ip6_src;                      /* swap destination and source protocoll address */
886         ip6.ip6_src = ns.nd_ns_target;
887
888         if(has_opt) {
889                 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN);        /* add fake source hard addr */
890         }
891
892         ns.nd_ns_cksum = 0;
893         ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
894         ns.nd_ns_reserved = htonl(0x40000000UL);        /* Set solicited flag */
895         opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
896
897         /* Create pseudo header */
898
899         pseudo.ip6_src = ip6.ip6_src;
900         pseudo.ip6_dst = ip6.ip6_dst;
901
902         if(has_opt) {
903                 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
904         } else {
905                 pseudo.length = htonl(ns_size);
906         }
907
908         pseudo.next = htonl(IPPROTO_ICMPV6);
909
910         /* Generate checksum */
911
912         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
913         checksum = inet_checksum(&ns, ns_size, checksum);
914
915         if(has_opt) {
916                 checksum = inet_checksum(&opt, opt_size, checksum);
917                 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
918         }
919
920         ns.nd_ns_hdr.icmp6_cksum = checksum;
921
922         /* Copy structs on stack back to packet */
923
924         memcpy(packet->data + ether_size, &ip6, ip6_size);
925         memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
926
927         if(has_opt) {
928                 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
929         }
930
931         send_packet(source, packet);
932 }
933
934 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
935         if(!checklength(source, packet, ether_size + ip6_size)) {
936                 return;
937         }
938
939         if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
940                 route_neighborsol(source, packet);
941                 return;
942         }
943
944         if(broadcast_mode && packet->data[38] == 255) {
945                 route_broadcast(source, packet);
946         } else {
947                 route_ipv6_unicast(source, packet);
948         }
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                 ifdebug(TRAFFIC) logger(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, packet->data + ETH_ALEN, ETH_ALEN);
971         }
972
973         /* Copy headers from packet to structs on the stack */
974
975         memcpy(&arp, packet->data + 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                 ifdebug(TRAFFIC) logger(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                 ifdebug(TRAFFIC) logger(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(packet->data, packet->data + ETH_ALEN, ETH_ALEN);        /* copy destination address */
1008         packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
1009
1010         memcpy(&addr, arp.arp_tpa, sizeof(addr));       /* save protocol addr */
1011         memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
1012         memcpy(arp.arp_spa, &addr, sizeof(addr));       /* ... */
1013
1014         memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN);     /* set target hard/proto addr */
1015         memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
1016         arp.arp_op = htons(ARPOP_REPLY);
1017
1018         /* Copy structs on stack back to packet */
1019
1020         memcpy(packet->data + ether_size, &arp, arp_size);
1021
1022         send_packet(source, packet);
1023 }
1024
1025 static void route_mac(node_t *source, vpn_packet_t *packet) {
1026         subnet_t *subnet;
1027         mac_t dest;
1028
1029         /* Learn source address */
1030
1031         if(source == myself) {
1032                 mac_t src;
1033                 memcpy(&src, &packet->data[6], sizeof(src));
1034                 learn_mac(&src);
1035         }
1036
1037         /* Lookup destination address */
1038
1039         memcpy(&dest, &packet->data[0], sizeof(dest));
1040         subnet = lookup_subnet_mac(NULL, &dest);
1041
1042         if(!subnet) {
1043                 route_broadcast(source, packet);
1044                 return;
1045         }
1046
1047         if(subnet->owner == source) {
1048                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
1049                 return;
1050         }
1051
1052         if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
1053                 return;
1054         }
1055
1056         if(decrement_ttl && source != myself && subnet->owner != myself)
1057                 if(!do_decrement_ttl(source, packet)) {
1058                         return;
1059                 }
1060
1061         uint16_t type = packet->data[12] << 8 | packet->data[13];
1062
1063         if(priorityinheritance) {
1064                 if(type == ETH_P_IP && packet->len >= ether_size + ip_size) {
1065                         packet->priority = packet->data[15];
1066                 } else if(type == ETH_P_IPV6 && packet->len >= ether_size + ip6_size) {
1067                         packet->priority = ((packet->data[14] & 0x0f) << 4) | (packet->data[15] >> 4);
1068                 }
1069         }
1070
1071         // Handle packets larger than PMTU
1072
1073         node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
1074
1075         if(directonly && subnet->owner != via) {
1076                 return;
1077         }
1078
1079         if(via && packet->len > via->mtu && via != myself) {
1080                 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);
1081                 length_t ethlen = 14;
1082
1083                 if(type == ETH_P_8021Q) {
1084                         type = packet->data[16] << 8 | packet->data[17];
1085                         ethlen += 4;
1086                 }
1087
1088                 if(type == ETH_P_IP && packet->len > 576 + ethlen) {
1089                         if(packet->data[6 + ethlen] & 0x40) {
1090                                 packet->len = via->mtu;
1091                                 route_ipv4_unreachable(source, packet, ethlen, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
1092                         } else {
1093                                 fragment_ipv4_packet(via, packet, ethlen);
1094                         }
1095
1096                         return;
1097                 } else if(type == ETH_P_IPV6 && packet->len > 1280 + ethlen) {
1098                         packet->len = via->mtu;
1099                         route_ipv6_unreachable(source, packet, ethlen, ICMP6_PACKET_TOO_BIG, 0);
1100                         return;
1101                 }
1102         }
1103
1104         clamp_mss(source, via, packet);
1105
1106         send_packet(subnet->owner, packet);
1107 }
1108
1109 void route(node_t *source, vpn_packet_t *packet) {
1110         if(forwarding_mode == FMODE_KERNEL && source != myself) {
1111                 send_packet(myself, packet);
1112                 return;
1113         }
1114
1115         if(!checklength(source, packet, ether_size)) {
1116                 return;
1117         }
1118
1119         switch(routing_mode) {
1120         case RMODE_ROUTER: {
1121                 uint16_t type = packet->data[12] << 8 | packet->data[13];
1122
1123                 switch(type) {
1124                 case ETH_P_ARP:
1125                         route_arp(source, packet);
1126                         break;
1127
1128                 case ETH_P_IP:
1129                         route_ipv4(source, packet);
1130                         break;
1131
1132                 case ETH_P_IPV6:
1133                         route_ipv6(source, packet);
1134                         break;
1135
1136                 default:
1137                         ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
1138                         break;
1139                 }
1140         }
1141         break;
1142
1143         case RMODE_SWITCH:
1144                 route_mac(source, packet);
1145                 break;
1146
1147         case RMODE_HUB:
1148                 route_broadcast(source, packet);
1149                 break;
1150         }
1151 }