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