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