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