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