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