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