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