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