Don't set $INTERFACE automatically, don't quit on EINTR/EAGAIN.
[tinc] / src / route.c
1 /*
2     route.c -- routing
3     Copyright (C) 2000-2004 Ivo Timmermans <ivo@tinc-vpn.org>,
4                   2000-2004 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 #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         node_t *via;
335
336         cp();
337
338         subnet = lookup_subnet_ipv4((ipv4_t *) &packet->data[30]);
339
340         if(!subnet) {
341                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d"),
342                                 source->name, source->hostname,
343                                 packet->data[30],
344                                 packet->data[31],
345                                 packet->data[32],
346                                 packet->data[33]);
347
348                 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
349                 return;
350         }
351         
352         if(subnet->owner == source) {
353                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
354                 return;
355         }
356
357         if(!subnet->owner->status.reachable)
358                 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
359
360         if(priorityinheritance)
361                 packet->priority = packet->data[15];
362
363         via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
364         
365         if(packet->len > via->mtu && via != myself) {
366                 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);
367                 if(packet->data[20] & 0x40) {
368                         packet->len = via->mtu;
369                         route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
370                 } else {
371                         fragment_ipv4_packet(via, packet);
372                 }
373
374                 return;
375         }
376
377         send_packet(subnet->owner, packet);
378 }
379
380 static __inline__ void route_ipv4(node_t *source, vpn_packet_t *packet)
381 {
382         cp();
383
384         if(!checklength(source, packet, ether_size + ip_size))
385                 return;
386
387         route_ipv4_unicast(source, packet);
388 }
389
390 /* RFC 2463 */
391
392 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code)
393 {
394         struct ip6_hdr ip6;
395         struct icmp6_hdr icmp6 = {0};
396         uint16_t checksum;      
397
398         struct {
399                 struct in6_addr ip6_src;        /* source address */
400                 struct in6_addr ip6_dst;        /* destination address */
401                 uint32_t length;
402                 uint32_t next;
403         } pseudo;
404
405         if(ratelimit(3))
406                 return;
407         
408         cp();
409
410         /* Copy headers from packet to structs on the stack */
411
412         memcpy(&ip6, packet->data + ether_size, ip6_size);
413
414         /* Remember original source and destination */
415         
416         pseudo.ip6_src = ip6.ip6_dst;
417         pseudo.ip6_dst = ip6.ip6_src;
418
419         pseudo.length = packet->len - ether_size;
420
421         if(type == ICMP6_PACKET_TOO_BIG)
422                 icmp6.icmp6_mtu = htonl(pseudo.length);
423         
424         if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
425                 pseudo.length = IP_MSS - ip6_size - icmp6_size;
426         
427         /* Copy first part of original contents to ICMP message */
428         
429         memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
430
431         /* Fill in IPv6 header */
432         
433         ip6.ip6_flow = htonl(0x60000000UL);
434         ip6.ip6_plen = htons(icmp6_size + pseudo.length);
435         ip6.ip6_nxt = IPPROTO_ICMPV6;
436         ip6.ip6_hlim = 255;
437         ip6.ip6_src = pseudo.ip6_src;
438         ip6.ip6_dst = pseudo.ip6_dst;
439
440         /* Fill in ICMP header */
441         
442         icmp6.icmp6_type = type;
443         icmp6.icmp6_code = code;
444         icmp6.icmp6_cksum = 0;
445
446         /* Create pseudo header */
447                 
448         pseudo.length = htonl(icmp6_size + pseudo.length);
449         pseudo.next = htonl(IPPROTO_ICMPV6);
450
451         /* Generate checksum */
452         
453         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
454         checksum = inet_checksum(&icmp6, icmp6_size, checksum);
455         checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
456
457         icmp6.icmp6_cksum = checksum;
458
459         /* Copy structs on stack back to packet */
460
461         memcpy(packet->data + ether_size, &ip6, ip6_size);
462         memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
463         
464         packet->len = ether_size + ip6_size + ntohl(pseudo.length);
465         
466         send_packet(source, packet);
467 }
468
469 static __inline__ void route_ipv6_unicast(node_t *source, vpn_packet_t *packet)
470 {
471         subnet_t *subnet;
472         node_t *via;
473
474         cp();
475
476         subnet = lookup_subnet_ipv6((ipv6_t *) &packet->data[38]);
477
478         if(!subnet) {
479                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
480                                 source->name, source->hostname,
481                                 ntohs(*(uint16_t *) &packet->data[38]),
482                                 ntohs(*(uint16_t *) &packet->data[40]),
483                                 ntohs(*(uint16_t *) &packet->data[42]),
484                                 ntohs(*(uint16_t *) &packet->data[44]),
485                                 ntohs(*(uint16_t *) &packet->data[46]),
486                                 ntohs(*(uint16_t *) &packet->data[48]),
487                                 ntohs(*(uint16_t *) &packet->data[50]),
488                                 ntohs(*(uint16_t *) &packet->data[52]));
489
490                 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
491                 return;
492         }
493
494         if(subnet->owner == source) {
495                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
496                 return;
497         }
498
499         if(!subnet->owner->status.reachable)
500                 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
501
502         via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
503         
504         if(packet->len > via->mtu && via != myself) {
505                 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);
506                 packet->len = via->mtu;
507                 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
508                 return;
509         }
510
511         send_packet(subnet->owner, packet);
512 }
513
514 /* RFC 2461 */
515
516 static void route_neighborsol(node_t *source, vpn_packet_t *packet)
517 {
518         struct ip6_hdr ip6;
519         struct nd_neighbor_solicit ns;
520         struct nd_opt_hdr opt;
521         subnet_t *subnet;
522         uint16_t checksum;
523
524         struct {
525                 struct in6_addr ip6_src;        /* source address */
526                 struct in6_addr ip6_dst;        /* destination address */
527                 uint32_t length;
528                 uint32_t next;
529         } pseudo;
530
531         cp();
532
533         if(!checklength(source, packet, ether_size + ip6_size + ns_size + opt_size + ETH_ALEN))
534                 return;
535         
536         if(source != myself) {
537                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got neighbor solicitation request from %s (%s) while in router mode!"), source->name, source->hostname);
538                 return;
539         }
540
541         /* Copy headers from packet to structs on the stack */
542
543         memcpy(&ip6, packet->data + ether_size, ip6_size);
544         memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
545         memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
546
547         /* First, snatch the source address from the neighbor solicitation packet */
548
549         if(overwrite_mac)
550                 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
551
552         /* Check if this is a valid neighbor solicitation request */
553
554         if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
555            opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR) {
556                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
557                 return;
558         }
559
560         /* Create pseudo header */
561
562         pseudo.ip6_src = ip6.ip6_src;
563         pseudo.ip6_dst = ip6.ip6_dst;
564         pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
565         pseudo.next = htonl(IPPROTO_ICMPV6);
566
567         /* Generate checksum */
568
569         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
570         checksum = inet_checksum(&ns, ns_size, checksum);
571         checksum = inet_checksum(&opt, opt_size, checksum);
572         checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
573
574         if(checksum) {
575                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
576                 return;
577         }
578
579         /* Check if the IPv6 address exists on the VPN */
580
581         subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
582
583         if(!subnet) {
584                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
585                                    ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
586                                    ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
587                                    ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
588                                    ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
589                                    ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
590                                    ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
591                                    ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
592                                    ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
593
594                 return;
595         }
596
597         /* Check if it is for our own subnet */
598
599         if(subnet->owner == myself)
600                 return;                                 /* silently ignore */
601
602         /* Create neighbor advertation reply */
603
604         memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN);        /* copy destination address */
605         packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
606
607         ip6.ip6_dst = ip6.ip6_src;                      /* swap destination and source protocoll address */
608         ip6.ip6_src = ns.nd_ns_target;
609
610         memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN);   /* add fake source hard addr */
611
612         ns.nd_ns_cksum = 0;
613         ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
614         ns.nd_ns_reserved = htonl(0x40000000UL);        /* Set solicited flag */
615         opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
616
617         /* Create pseudo header */
618
619         pseudo.ip6_src = ip6.ip6_src;
620         pseudo.ip6_dst = ip6.ip6_dst;
621         pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
622         pseudo.next = htonl(IPPROTO_ICMPV6);
623
624         /* Generate checksum */
625
626         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
627         checksum = inet_checksum(&ns, ns_size, checksum);
628         checksum = inet_checksum(&opt, opt_size, checksum);
629         checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
630
631         ns.nd_ns_hdr.icmp6_cksum = checksum;
632
633         /* Copy structs on stack back to packet */
634
635         memcpy(packet->data + ether_size, &ip6, ip6_size);
636         memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
637         memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
638
639         send_packet(source, packet);
640 }
641
642 static __inline__ void route_ipv6(node_t *source, vpn_packet_t *packet)
643 {
644         cp();
645
646         if(!checklength(source, packet, ether_size + ip6_size))
647                 return;
648
649         if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
650                 route_neighborsol(source, packet);
651                 return;
652         }
653
654         route_ipv6_unicast(source, packet);
655 }
656
657 /* RFC 826 */
658
659 static void route_arp(node_t *source, vpn_packet_t *packet)
660 {
661         struct ether_arp arp;
662         subnet_t *subnet;
663         struct in_addr addr;
664
665         cp();
666
667         if(!checklength(source, packet, ether_size + arp_size))
668                 return;
669
670         if(source != myself) {
671                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got ARP request from %s (%s) while in router mode!"), source->name, source->hostname);
672                 return;
673         }
674
675         /* First, snatch the source address from the ARP packet */
676
677         if(overwrite_mac)
678                 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
679
680         /* Copy headers from packet to structs on the stack */
681
682         memcpy(&arp, packet->data + ether_size, arp_size);
683
684         /* Check if this is a valid ARP request */
685
686         if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
687            arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
688                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
689                 return;
690         }
691
692         /* Check if the IPv4 address exists on the VPN */
693
694         subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
695
696         if(!subnet) {
697                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
698                                    arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
699                                    arp.arp_tpa[3]);
700                 return;
701         }
702
703         /* Check if it is for our own subnet */
704
705         if(subnet->owner == myself)
706                 return;                                 /* silently ignore */
707
708         memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN);        /* copy destination address */
709         packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
710
711         memcpy(&addr, arp.arp_tpa, sizeof(addr));       /* save protocol addr */
712         memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
713         memcpy(arp.arp_spa, &addr, sizeof(addr));       /* ... */
714
715         memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN);     /* set target hard/proto addr */
716         memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
717         arp.arp_op = htons(ARPOP_REPLY);
718
719         /* Copy structs on stack back to packet */
720
721         memcpy(packet->data + ether_size, &arp, arp_size);
722
723         send_packet(source, packet);
724 }
725
726 void route(node_t *source, vpn_packet_t *packet)
727 {
728         cp();
729
730         if(!checklength(source, packet, ether_size))
731                 return;
732
733         switch (routing_mode) {
734                 case RMODE_ROUTER:
735                         {
736                                 uint16_t type;
737
738                                 type = ntohs(*((uint16_t *)(&packet->data[12])));
739                                 switch (type) {
740                                         case ETH_P_ARP:
741                                                 route_arp(source, packet);
742                                                 break;
743
744                                         case ETH_P_IP:
745                                                 route_ipv4(source, packet);
746                                                 break;
747
748                                         case ETH_P_IPV6:
749                                                 route_ipv6(source, packet);
750                                                 break;
751
752                                         default:
753                                                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown type %hx"), source->name, source->hostname, type);
754                                                 break;
755                                 }
756                         }
757                         break;
758
759                 case RMODE_SWITCH:
760                         route_mac(source, packet);
761                         break;
762
763                 case RMODE_HUB:
764                         broadcast_packet(source, packet);
765                         break;
766         }
767 }