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