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