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