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