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