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