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