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