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