Fix a few memory leaks in the CLI found by AddressSanitizer.
[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         oldlen = packet->len - ether_size;
280
281         if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
282                 icmp.icmp_nextmtu = htons(packet->len - ether_size);
283
284         if(oldlen >= IP_MSS - ip_size - icmp_size)
285                 oldlen = IP_MSS - ip_size - icmp_size;
286
287         /* Copy first part of original contents to ICMP message */
288
289         memmove(DATA(packet) + ether_size + ip_size + icmp_size, DATA(packet) + ether_size, oldlen);
290
291         /* Fill in IPv4 header */
292
293         ip.ip_v = 4;
294         ip.ip_hl = ip_size / 4;
295         ip.ip_tos = 0;
296         ip.ip_len = htons(ip_size + icmp_size + oldlen);
297         ip.ip_id = 0;
298         ip.ip_off = 0;
299         ip.ip_ttl = 255;
300         ip.ip_p = IPPROTO_ICMP;
301         ip.ip_sum = 0;
302         ip.ip_src = ip_dst;
303         ip.ip_dst = ip_src;
304
305         ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
306
307         /* Fill in ICMP header */
308
309         icmp.icmp_type = type;
310         icmp.icmp_code = code;
311         icmp.icmp_cksum = 0;
312
313         icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
314         icmp.icmp_cksum = inet_checksum(DATA(packet) + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
315
316         /* Copy structs on stack back to packet */
317
318         memcpy(DATA(packet) + ether_size, &ip, ip_size);
319         memcpy(DATA(packet) + ether_size + ip_size, &icmp, icmp_size);
320
321         packet->len = ether_size + ip_size + icmp_size + oldlen;
322
323         send_packet(source, packet);
324 }
325
326 /* RFC 791 */
327
328 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t ether_size) {
329         struct ip ip;
330         vpn_packet_t fragment;
331         int len, maxlen, todo;
332         uint8_t *offset;
333         uint16_t ip_off, origf;
334
335         memcpy(&ip, DATA(packet) + ether_size, ip_size);
336         fragment.priority = packet->priority;
337         fragment.offset = DEFAULT_PACKET_OFFSET;
338
339         if(ip.ip_hl != ip_size / 4)
340                 return;
341
342         todo = ntohs(ip.ip_len) - ip_size;
343
344         if(ether_size + ip_size + todo != packet->len) {
345                 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));
346                 return;
347         }
348
349         logger(DEBUG_TRAFFIC, LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
350
351         offset = DATA(packet) + ether_size + ip_size;
352         maxlen = (dest->mtu - ether_size - ip_size) & ~0x7;
353         ip_off = ntohs(ip.ip_off);
354         origf = ip_off & ~IP_OFFMASK;
355         ip_off &= IP_OFFMASK;
356
357         while(todo) {
358                 len = todo > maxlen ? maxlen : todo;
359                 memcpy(DATA(&fragment) + ether_size + ip_size, offset, len);
360                 todo -= len;
361                 offset += len;
362
363                 ip.ip_len = htons(ip_size + len);
364                 ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
365                 ip.ip_sum = 0;
366                 ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
367                 memcpy(DATA(&fragment), DATA(packet), ether_size);
368                 memcpy(DATA(&fragment) + ether_size, &ip, ip_size);
369                 fragment.len = ether_size + ip_size + len;
370
371                 send_packet(dest, &fragment);
372
373                 ip_off += len / 8;
374         }
375 }
376
377 static void route_ipv4(node_t *source, vpn_packet_t *packet) {
378         if(!checklength(source, packet, ether_size + ip_size))
379                 return;
380
381         subnet_t *subnet;
382         node_t *via;
383         ipv4_t dest;
384
385         memcpy(&dest, &DATA(packet)[30], sizeof dest);
386         subnet = lookup_subnet_ipv4(&dest);
387
388         if(!subnet) {
389                 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
390                                 source->name, source->hostname,
391                                 dest.x[0],
392                                 dest.x[1],
393                                 dest.x[2],
394                                 dest.x[3]);
395
396                 route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
397                 return;
398         }
399
400         if (!subnet->owner) {
401                 broadcast_packet(source, packet);
402                 return;
403         }
404
405         if(subnet->owner == source) {
406                 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
407                 return;
408         }
409
410         if(!subnet->owner->status.reachable)
411                 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
412
413         if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
414                 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
415
416         if(priorityinheritance)
417                 packet->priority = DATA(packet)[15];
418
419         via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
420
421         if(via == source) {
422                 logger(DEBUG_TRAFFIC, LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
423                 return;
424         }
425
426         if(directonly && subnet->owner != via)
427                 return route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
428
429         if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
430                 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);
431                 if(DATA(packet)[20] & 0x40) {
432                         packet->len = MAX(via->mtu, 590);
433                         route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
434                 } else {
435                         fragment_ipv4_packet(via, packet, ether_size);
436                 }
437
438                 return;
439         }
440
441         clamp_mss(source, via, packet);
442
443         send_packet(subnet->owner, packet);
444 }
445
446 /* RFC 2463 */
447
448 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, 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, DATA(packet) + 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(DATA(packet) + ether_size + ip6_size + icmp6_size, DATA(packet) + 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(DATA(packet) + 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(DATA(packet) + ether_size, &ip6, ip6_size);
519         memcpy(DATA(packet) + 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_neighborsol(node_t *source, vpn_packet_t *packet);
527
528 static void route_ipv6(node_t *source, vpn_packet_t *packet) {
529         if(!checklength(source, packet, ether_size + ip6_size))
530                 return;
531
532         if(DATA(packet)[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && DATA(packet)[54] == ND_NEIGHBOR_SOLICIT) {
533                 route_neighborsol(source, packet);
534                 return;
535         }
536
537         subnet_t *subnet;
538         node_t *via;
539         ipv6_t dest;
540
541         memcpy(&dest, &DATA(packet)[38], sizeof dest);
542         subnet = lookup_subnet_ipv6(&dest);
543
544         if(!subnet) {
545                 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
546                                 source->name, source->hostname,
547                                 ntohs(dest.x[0]),
548                                 ntohs(dest.x[1]),
549                                 ntohs(dest.x[2]),
550                                 ntohs(dest.x[3]),
551                                 ntohs(dest.x[4]),
552                                 ntohs(dest.x[5]),
553                                 ntohs(dest.x[6]),
554                                 ntohs(dest.x[7]));
555
556                 route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
557                 return;
558         }
559
560         if (!subnet->owner) {
561                 broadcast_packet(source, packet);
562                 return;
563         }
564
565         if(subnet->owner == source) {
566                 logger(DEBUG_TRAFFIC, LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
567                 return;
568         }
569
570         if(!subnet->owner->status.reachable)
571                 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
572
573         if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself)
574                 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
575
576         via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
577
578         if(via == source) {
579                 logger(DEBUG_TRAFFIC, LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
580                 return;
581         }
582
583         if(directonly && subnet->owner != via)
584                 return route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
585
586         if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
587                 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);
588                 packet->len = MAX(via->mtu, 1294);
589                 route_ipv6_unreachable(source, packet, ether_size, ICMP6_PACKET_TOO_BIG, 0);
590                 return;
591         }
592
593         clamp_mss(source, via, packet);
594
595         send_packet(subnet->owner, packet);
596 }
597
598 /* RFC 2461 */
599
600 static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
601         struct ip6_hdr ip6;
602         struct nd_neighbor_solicit ns;
603         struct nd_opt_hdr opt;
604         subnet_t *subnet;
605         uint16_t checksum;
606         bool has_opt;
607
608         struct {
609                 struct in6_addr ip6_src;
610                 struct in6_addr ip6_dst;
611                 uint32_t length;
612                 uint32_t next;
613         } pseudo;
614
615         if(!checklength(source, packet, ether_size + ip6_size + ns_size))
616                 return;
617
618         has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
619
620         if(source != myself) {
621                 logger(DEBUG_TRAFFIC, LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
622                 return;
623         }
624
625         /* Copy headers from packet to structs on the stack */
626
627         memcpy(&ip6, DATA(packet) + ether_size, ip6_size);
628         memcpy(&ns, DATA(packet) + ether_size + ip6_size, ns_size);
629         if(has_opt)
630                 memcpy(&opt, DATA(packet) + ether_size + ip6_size + ns_size, opt_size);
631
632         /* First, snatch the source address from the neighbor solicitation packet */
633
634         if(overwrite_mac)
635                 memcpy(mymac.x, DATA(packet) + ETH_ALEN, ETH_ALEN);
636
637         /* Check if this is a valid neighbor solicitation request */
638
639         if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
640            (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
641                 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
642                 return;
643         }
644
645         /* Create pseudo header */
646
647         pseudo.ip6_src = ip6.ip6_src;
648         pseudo.ip6_dst = ip6.ip6_dst;
649         if(has_opt)
650                 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
651         else
652                 pseudo.length = htonl(ns_size);
653         pseudo.next = htonl(IPPROTO_ICMPV6);
654
655         /* Generate checksum */
656
657         checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
658         checksum = inet_checksum(&ns, ns_size, checksum);
659         if(has_opt) {
660                 checksum = inet_checksum(&opt, opt_size, checksum);
661                 checksum = inet_checksum(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
662         }
663
664         if(checksum) {
665                 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
666                 return;
667         }
668
669         /* Check if the IPv6 address exists on the VPN */
670
671         subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
672
673         if(!subnet) {
674                 logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
675                                    ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
676                                    ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
677                                    ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
678                                    ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
679                                    ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
680                                    ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
681                                    ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
682                                    ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
683
684                 return;
685         }
686
687         /* Check if it is for our own subnet */
688
689         if(subnet->owner == myself)
690                 return;                                          /* silently ignore */
691
692         /* Create neighbor advertation reply */
693
694         memcpy(DATA(packet), DATA(packet) + ETH_ALEN, ETH_ALEN); /* copy destination address */
695         DATA(packet)[ETH_ALEN * 2 - 1] ^= 0xFF;                  /* mangle source address so it looks like it's not from us */
696
697         ip6.ip6_dst = ip6.ip6_src;                               /* swap destination and source protocoll address */
698         ip6.ip6_src = ns.nd_ns_target;
699
700         if(has_opt)
701                 memcpy(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, DATA(packet) + ETH_ALEN, ETH_ALEN);   /* add fake source hard addr */
702
703         ns.nd_ns_cksum = 0;
704         ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
705         ns.nd_ns_reserved = htonl(0x40000000UL);                 /* Set solicited flag */
706         opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
707
708         /* Create pseudo header */
709
710         pseudo.ip6_src = ip6.ip6_src;
711         pseudo.ip6_dst = ip6.ip6_dst;
712         if(has_opt)
713                 pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
714         else
715                 pseudo.length = htonl(ns_size);
716         pseudo.next = htonl(IPPROTO_ICMPV6);
717
718         /* Generate checksum */
719
720         checksum = inet_checksum(&pseudo, sizeof pseudo, ~0);
721         checksum = inet_checksum(&ns, ns_size, checksum);
722         if(has_opt) {
723                 checksum = inet_checksum(&opt, opt_size, checksum);
724                 checksum = inet_checksum(DATA(packet) + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
725         }
726
727         ns.nd_ns_hdr.icmp6_cksum = checksum;
728
729         /* Copy structs on stack back to packet */
730
731         memcpy(DATA(packet) + ether_size, &ip6, ip6_size);
732         memcpy(DATA(packet) + ether_size + ip6_size, &ns, ns_size);
733         if(has_opt)
734                 memcpy(DATA(packet) + ether_size + ip6_size + ns_size, &opt, opt_size);
735
736         send_packet(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, DATA(packet) + ETH_ALEN, ETH_ALEN);
758
759         /* Copy headers from packet to structs on the stack */
760
761         memcpy(&arp, DATA(packet) + 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(DATA(packet), DATA(packet) + ETH_ALEN, ETH_ALEN); /* copy destination address */
788         DATA(packet)[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, DATA(packet) + 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(DATA(packet) + 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, &DATA(packet)[6], sizeof src);
814                 learn_mac(&src);
815         }
816
817         /* Lookup destination address */
818
819         memcpy(&dest, &DATA(packet)[0], sizeof dest);
820         subnet = lookup_subnet_mac(NULL, &dest);
821
822         if(!subnet || !subnet->owner) {
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         uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
836
837         if(priorityinheritance && type == ETH_P_IP && packet->len >= ether_size + ip_size)
838                 packet->priority = DATA(packet)[15];
839
840         // Handle packets larger than PMTU
841
842         node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
843
844         if(directonly && subnet->owner != via)
845                 return;
846
847         if(via && packet->len > via->mtu && via != myself) {
848                 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);
849                 length_t ethlen = 14;
850
851                 if(type == ETH_P_8021Q) {
852                         type = DATA(packet)[16] << 8 | DATA(packet)[17];
853                         ethlen += 4;
854                 }
855
856                 if(type == ETH_P_IP && packet->len > 576 + ethlen) {
857                         if(DATA(packet)[6 + ethlen] & 0x40) {
858                                 packet->len = via->mtu;
859                                 route_ipv4_unreachable(source, packet, ethlen, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
860                         } else {
861                                 fragment_ipv4_packet(via, packet, ethlen);
862                         }
863                         return;
864                 } else if(type == ETH_P_IPV6 && packet->len > 1280 + ethlen) {
865                         packet->len = via->mtu;
866                         route_ipv6_unreachable(source, packet, ethlen, ICMP6_PACKET_TOO_BIG, 0);
867                         return;
868                 }
869         }
870
871         clamp_mss(source, via, packet);
872
873         send_packet(subnet->owner, packet);
874 }
875
876 static void send_pcap(vpn_packet_t *packet) {
877         pcap = false;
878
879         for list_each(connection_t, c, connection_list) {
880                 if(!c->status.pcap)
881                         continue;
882
883                 pcap = true;
884                 int len = packet->len;
885                 if(c->outmaclength && c->outmaclength < len)
886                         len = c->outmaclength;
887
888                 if(send_request(c, "%d %d %d", CONTROL, REQ_PCAP, len))
889                         send_meta(c, (char *)DATA(packet), len);
890         }
891 }
892
893 static bool do_decrement_ttl(node_t *source, vpn_packet_t *packet) {
894         uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
895         length_t ethlen = ether_size;
896
897         if(type == ETH_P_8021Q) {
898                 type = DATA(packet)[16] << 8 | DATA(packet)[17];
899                 ethlen += 4;
900         }
901
902         switch (type) {
903                 case ETH_P_IP:
904                         if(!checklength(source, packet, ethlen + ip_size))
905                                 return false;
906
907                         if(DATA(packet)[ethlen + 8] < 1) {
908                                 if(DATA(packet)[ethlen + 11] != IPPROTO_ICMP || DATA(packet)[ethlen + 32] != ICMP_TIME_EXCEEDED)
909                                         route_ipv4_unreachable(source, packet, ethlen, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL);
910                                 return false;
911                         }
912
913                         uint16_t old = DATA(packet)[ethlen + 8] << 8 | DATA(packet)[ethlen + 9];
914                         DATA(packet)[ethlen + 8]--;
915                         uint16_t new = DATA(packet)[ethlen + 8] << 8 | DATA(packet)[ethlen + 9];
916
917                         uint32_t checksum = DATA(packet)[ethlen + 10] << 8 | DATA(packet)[ethlen + 11];
918                         checksum += old + (~new & 0xFFFF);
919                         while(checksum >> 16)
920                                 checksum = (checksum & 0xFFFF) + (checksum >> 16);
921                         DATA(packet)[ethlen + 10] = checksum >> 8;
922                         DATA(packet)[ethlen + 11] = checksum & 0xff;
923
924                         return true;
925
926                 case ETH_P_IPV6:
927                         if(!checklength(source, packet, ethlen + ip6_size))
928                                 return false;
929
930                         if(DATA(packet)[ethlen + 7] < 1) {
931                                 if(DATA(packet)[ethlen + 6] != IPPROTO_ICMPV6 || DATA(packet)[ethlen + 40] != ICMP6_TIME_EXCEEDED)
932                                         route_ipv6_unreachable(source, packet, ethlen, ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT);
933                                 return false;
934                         }
935
936                         DATA(packet)[ethlen + 7]--;
937
938                         return true;
939
940                 default:
941                         return true;
942         }
943 }
944
945 void route(node_t *source, vpn_packet_t *packet) {
946         if(pcap)
947                 send_pcap(packet);
948
949         if(forwarding_mode == FMODE_KERNEL && source != myself) {
950                 send_packet(myself, packet);
951                 return;
952         }
953
954         if(!checklength(source, packet, ether_size))
955                 return;
956
957         if(decrement_ttl && source != myself)
958                 if(!do_decrement_ttl(source, packet))
959                         return;
960
961         uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
962
963         switch (routing_mode) {
964                 case RMODE_ROUTER:
965                         switch (type) {
966                                 case ETH_P_ARP:
967                                         route_arp(source, packet);
968                                         break;
969
970                                 case ETH_P_IP:
971                                         route_ipv4(source, packet);
972                                         break;
973
974                                 case ETH_P_IPV6:
975                                         route_ipv6(source, packet);
976                                         break;
977
978                                 default:
979                                         logger(DEBUG_TRAFFIC, LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
980                                         break;
981                         }
982                         break;
983
984                 case RMODE_SWITCH:
985                         route_mac(source, packet);
986                         break;
987
988                 case RMODE_HUB:
989                         broadcast_packet(source, packet);
990                         break;
991         }
992 }