route: Support ToS/DiffServ priority inheritance when routing IPv6 packets.
[oweals/tinc.git] / 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         if(priorityinheritance)
679                 packet->priority = ((packet->data[14] & 0x0f) << 4) | (packet->data[15] >> 4);
680
681         via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
682         
683         if(via == source) {
684                 ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
685                 return;
686         }
687         
688         if(directonly && subnet->owner != via)
689                 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
690
691         if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
692                 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);
693                 packet->len = MAX(via->mtu, 1294);
694                 route_ipv6_unreachable(source, packet, ether_size, ICMP6_PACKET_TOO_BIG, 0);
695                 return;
696         }
697
698         clamp_mss(source, via, packet);
699  
700         send_packet(subnet->owner, packet);
701 }
702
703 /* RFC 2461 */
704
705 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
706         struct ip6_hdr ip6;
707         struct nd_neighbor_solicit ns;
708         struct nd_opt_hdr opt;
709         subnet_t *subnet;
710         uint16_t checksum;
711         bool has_opt;
712
713         struct {
714                 struct in6_addr ip6_src;        /* source address */
715                 struct in6_addr ip6_dst;        /* destination address */
716                 uint32_t length;
717                 uint32_t next;
718         } pseudo;
719
720         if(!checklength(source, packet, ether_size + ip6_size + ns_size))
721                 return;
722         
723         has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
724         
725         if(source != myself) {
726                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
727                 return;
728         }
729
730         /* Copy headers from packet to structs on the stack */
731
732         memcpy(&ip6, packet->data + ether_size, ip6_size);
733         memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
734         if(has_opt)
735                 memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
736
737         /* First, snatch the source address from the neighbor solicitation packet */
738
739         if(overwrite_mac)
740                 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
741
742         /* Check if this is a valid neighbor solicitation request */
743
744         if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
745            (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
746                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
747                 return;
748         }
749
750         /* Create pseudo header */
751
752         pseudo.ip6_src = ip6.ip6_src;
753         pseudo.ip6_dst = ip6.ip6_dst;
754         if(has_opt)
755                 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
756         else
757                 pseudo.length = htonl(ns_size);
758         pseudo.next = htonl(IPPROTO_ICMPV6);
759
760         /* Generate checksum */
761
762         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
763         checksum = inet_checksum(&ns, ns_size, checksum);
764         if(has_opt) {
765                 checksum = inet_checksum(&opt, opt_size, checksum);
766                 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
767         }
768
769         if(checksum) {
770                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
771                 return;
772         }
773
774         /* Check if the IPv6 address exists on the VPN */
775
776         subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
777
778         if(!subnet) {
779                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
780                                    ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
781                                    ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
782                                    ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
783                                    ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
784                                    ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
785                                    ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
786                                    ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
787                                    ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
788
789                 return;
790         }
791
792         /* Check if it is for our own subnet */
793
794         if(subnet->owner == myself)
795                 return;                                 /* silently ignore */
796
797         if(decrement_ttl)
798                 if(!do_decrement_ttl(source, packet))
799                         return;
800
801         /* Create neighbor advertation reply */
802
803         memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN);        /* copy destination address */
804         packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
805
806         ip6.ip6_dst = ip6.ip6_src;                      /* swap destination and source protocoll address */
807         ip6.ip6_src = ns.nd_ns_target;
808
809         if(has_opt)
810                 memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN);   /* add fake source hard addr */
811
812         ns.nd_ns_cksum = 0;
813         ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
814         ns.nd_ns_reserved = htonl(0x40000000UL);        /* Set solicited flag */
815         opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
816
817         /* Create pseudo header */
818
819         pseudo.ip6_src = ip6.ip6_src;
820         pseudo.ip6_dst = ip6.ip6_dst;
821         if(has_opt)
822                 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
823         else
824                 pseudo.length = htonl(ns_size);
825         pseudo.next = htonl(IPPROTO_ICMPV6);
826
827         /* Generate checksum */
828
829         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
830         checksum = inet_checksum(&ns, ns_size, checksum);
831         if(has_opt) {
832                 checksum = inet_checksum(&opt, opt_size, checksum);
833                 checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
834         }
835
836         ns.nd_ns_hdr.icmp6_cksum = checksum;
837
838         /* Copy structs on stack back to packet */
839
840         memcpy(packet->data + ether_size, &ip6, ip6_size);
841         memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
842         if(has_opt)
843                 memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
844
845         send_packet(source, packet);
846 }
847
848 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
849         if(!checklength(source, packet, ether_size + ip6_size))
850                 return;
851
852         if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
853                 route_neighborsol(source, packet);
854                 return;
855         }
856
857         if(broadcast_mode && packet->data[38] == 255)
858                 route_broadcast(source, packet);
859         else
860                 route_ipv6_unicast(source, packet);
861 }
862
863 /* RFC 826 */
864
865 static void route_arp(node_t *source, vpn_packet_t *packet) {
866         struct ether_arp arp;
867         subnet_t *subnet;
868         struct in_addr addr;
869
870         if(!checklength(source, packet, ether_size + arp_size))
871                 return;
872
873         if(source != myself) {
874                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
875                 return;
876         }
877
878         /* First, snatch the source address from the ARP packet */
879
880         if(overwrite_mac)
881                 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
882
883         /* Copy headers from packet to structs on the stack */
884
885         memcpy(&arp, packet->data + ether_size, arp_size);
886
887         /* Check if this is a valid ARP request */
888
889         if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
890            arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
891                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
892                 return;
893         }
894
895         /* Check if the IPv4 address exists on the VPN */
896
897         subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
898
899         if(!subnet) {
900                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
901                                    arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
902                                    arp.arp_tpa[3]);
903                 return;
904         }
905
906         /* Check if it is for our own subnet */
907
908         if(subnet->owner == myself)
909                 return;                                 /* silently ignore */
910
911         if(decrement_ttl)
912                 if(!do_decrement_ttl(source, packet))
913                         return;
914
915         memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN);        /* copy destination address */
916         packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
917
918         memcpy(&addr, arp.arp_tpa, sizeof(addr));       /* save protocol addr */
919         memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
920         memcpy(arp.arp_spa, &addr, sizeof(addr));       /* ... */
921
922         memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN);     /* set target hard/proto addr */
923         memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
924         arp.arp_op = htons(ARPOP_REPLY);
925
926         /* Copy structs on stack back to packet */
927
928         memcpy(packet->data + ether_size, &arp, arp_size);
929
930         send_packet(source, packet);
931 }
932
933 static void route_mac(node_t *source, vpn_packet_t *packet) {
934         subnet_t *subnet;
935         mac_t dest;
936
937         /* Learn source address */
938
939         if(source == myself) {
940                 mac_t src;
941                 memcpy(&src, &packet->data[6], sizeof src);
942                 learn_mac(&src);
943         }
944
945         /* Lookup destination address */
946
947         memcpy(&dest, &packet->data[0], sizeof dest);
948         subnet = lookup_subnet_mac(NULL, &dest);
949
950         if(!subnet) {
951                 route_broadcast(source, packet);
952                 return;
953         }
954
955         if(subnet->owner == source) {
956                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
957                 return;
958         }
959
960         if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
961                 return;
962
963         if(decrement_ttl && source != myself && subnet->owner != myself)
964                 if(!do_decrement_ttl(source, packet))
965                         return;
966
967         uint16_t type = packet->data[12] << 8 | packet->data[13];
968
969         if(priorityinheritance) {
970                 if(type == ETH_P_IP && packet->len >= ether_size + ip_size)
971                         packet->priority = packet->data[15];
972                 else if(type == ETH_P_IPV6 && packet->len >= ether_size + ip6_size)
973                         packet->priority = ((packet->data[14] & 0x0f) << 4) | (packet->data[15] >> 4);
974         }
975
976         // Handle packets larger than PMTU
977
978         node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
979
980         if(directonly && subnet->owner != via)
981                 return;
982         
983         if(via && packet->len > via->mtu && via != myself) {
984                 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);
985                 length_t ethlen = 14;
986
987                 if(type == ETH_P_8021Q) {
988                         type = packet->data[16] << 8 | packet->data[17];
989                         ethlen += 4;
990                 }
991
992                 if(type == ETH_P_IP && packet->len > 576 + ethlen) {
993                         if(packet->data[6 + ethlen] & 0x40) {
994                                 packet->len = via->mtu;
995                                 route_ipv4_unreachable(source, packet, ethlen, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
996                         } else {
997                                 fragment_ipv4_packet(via, packet, ethlen);
998                         }
999                         return;
1000                 } else if(type == ETH_P_IPV6 && packet->len > 1280 + ethlen) {
1001                         packet->len = via->mtu;
1002                         route_ipv6_unreachable(source, packet, ethlen, ICMP6_PACKET_TOO_BIG, 0);
1003                         return;
1004                 }
1005         }
1006
1007         clamp_mss(source, via, packet);
1008  
1009         send_packet(subnet->owner, packet);
1010 }
1011
1012 void route(node_t *source, vpn_packet_t *packet) {
1013         if(forwarding_mode == FMODE_KERNEL && source != myself) {
1014                 send_packet(myself, packet);
1015                 return;
1016         }
1017
1018         if(!checklength(source, packet, ether_size))
1019                 return;
1020
1021         switch (routing_mode) {
1022                 case RMODE_ROUTER:
1023                         {
1024                                 uint16_t type = packet->data[12] << 8 | packet->data[13];
1025
1026                                 switch (type) {
1027                                         case ETH_P_ARP:
1028                                                 route_arp(source, packet);
1029                                                 break;
1030
1031                                         case ETH_P_IP:
1032                                                 route_ipv4(source, packet);
1033                                                 break;
1034
1035                                         case ETH_P_IPV6:
1036                                                 route_ipv6(source, packet);
1037                                                 break;
1038
1039                                         default:
1040                                                 ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
1041                                                 break;
1042                                 }
1043                         }
1044                         break;
1045
1046                 case RMODE_SWITCH:
1047                         route_mac(source, packet);
1048                         break;
1049
1050                 case RMODE_HUB:
1051                         route_broadcast(source, packet);
1052                         break;
1053         }
1054 }