367926ea035fd0b102cd4b83baf59c85201438df
[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.73 2003/12/20 21:25: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 static __inline__ void route_ipv4_unicast(node_t *source, vpn_packet_t *packet)
280 {
281         subnet_t *subnet;
282
283         cp();
284
285         subnet = lookup_subnet_ipv4((ipv4_t *) &packet->data[30]);
286
287         if(!subnet) {
288                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d"),
289                                 source->name, source->hostname,
290                                 packet->data[30],
291                                 packet->data[31],
292                                 packet->data[32],
293                                 packet->data[33]);
294
295                 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
296                 return;
297         }
298         
299         if(subnet->owner == source) {
300                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
301                 return;
302         }
303
304         if(!subnet->owner->status.reachable)
305                 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
306
307         if(subnet->owner->options & OPTION_PMTU_DISCOVERY && packet->len > subnet->owner->mtu && subnet->owner != myself) {
308                 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);
309                 packet->len = subnet->owner->mtu;
310                 route_ipv4_unreachable(source, packet, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
311                 return;
312         }
313
314         if(priorityinheritance)
315                 packet->priority = packet->data[15];
316
317         send_packet(subnet->owner, packet);
318 }
319
320 static __inline__ void route_ipv4(node_t *source, vpn_packet_t *packet)
321 {
322         cp();
323
324         if(!checklength(source, packet, ether_size + ip_size))
325                 return;
326
327         route_ipv4_unicast(source, packet);
328 }
329
330 /* RFC 2463 */
331
332 static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, uint8_t type, uint8_t code)
333 {
334         struct ip6_hdr ip6;
335         struct icmp6_hdr icmp6 = {0};
336         uint16_t checksum;      
337
338         struct {
339                 struct in6_addr ip6_src;        /* source address */
340                 struct in6_addr ip6_dst;        /* destination address */
341                 uint32_t length;
342                 uint32_t next;
343         } pseudo;
344
345         if(ratelimit(3))
346                 return;
347         
348         cp();
349
350         /* Copy headers from packet to structs on the stack */
351
352         memcpy(&ip6, packet->data + ether_size, ip6_size);
353
354         /* Remember original source and destination */
355         
356         pseudo.ip6_src = ip6.ip6_dst;
357         pseudo.ip6_dst = ip6.ip6_src;
358
359         pseudo.length = packet->len - ether_size;
360
361         if(type == ICMP6_PACKET_TOO_BIG)
362                 icmp6.icmp6_mtu = htonl(pseudo.length);
363         
364         if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
365                 pseudo.length = IP_MSS - ip6_size - icmp6_size;
366         
367         /* Copy first part of original contents to ICMP message */
368         
369         memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
370
371         /* Fill in IPv6 header */
372         
373         ip6.ip6_flow = htonl(0x60000000UL);
374         ip6.ip6_plen = htons(icmp6_size + pseudo.length);
375         ip6.ip6_nxt = IPPROTO_ICMPV6;
376         ip6.ip6_hlim = 255;
377         ip6.ip6_src = pseudo.ip6_src;
378         ip6.ip6_dst = pseudo.ip6_dst;
379
380         /* Fill in ICMP header */
381         
382         icmp6.icmp6_type = type;
383         icmp6.icmp6_code = code;
384         icmp6.icmp6_cksum = 0;
385
386         /* Create pseudo header */
387                 
388         pseudo.length = htonl(icmp6_size + pseudo.length);
389         pseudo.next = htonl(IPPROTO_ICMPV6);
390
391         /* Generate checksum */
392         
393         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
394         checksum = inet_checksum(&icmp6, icmp6_size, checksum);
395         checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
396
397         icmp6.icmp6_cksum = checksum;
398
399         /* Copy structs on stack back to packet */
400
401         memcpy(packet->data + ether_size, &ip6, ip6_size);
402         memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
403         
404         packet->len = ether_size + ip6_size + ntohl(pseudo.length);
405         
406         send_packet(source, packet);
407 }
408
409 static __inline__ void route_ipv6_unicast(node_t *source, vpn_packet_t *packet)
410 {
411         subnet_t *subnet;
412
413         cp();
414
415         subnet = lookup_subnet_ipv6((ipv6_t *) &packet->data[38]);
416
417         if(!subnet) {
418                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
419                                 source->name, source->hostname,
420                                 ntohs(*(uint16_t *) &packet->data[38]),
421                                 ntohs(*(uint16_t *) &packet->data[40]),
422                                 ntohs(*(uint16_t *) &packet->data[42]),
423                                 ntohs(*(uint16_t *) &packet->data[44]),
424                                 ntohs(*(uint16_t *) &packet->data[46]),
425                                 ntohs(*(uint16_t *) &packet->data[48]),
426                                 ntohs(*(uint16_t *) &packet->data[50]),
427                                 ntohs(*(uint16_t *) &packet->data[52]));
428
429                 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
430                 return;
431         }
432
433         if(subnet->owner == source) {
434                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Packet looping back to %s (%s)!"), source->name, source->hostname);
435                 return;
436         }
437
438         if(!subnet->owner->status.reachable)
439                 route_ipv6_unreachable(source, packet, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
440         
441         if(subnet->owner->options & OPTION_PMTU_DISCOVERY && packet->len > subnet->owner->mtu && subnet->owner != myself) {
442                 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);
443                 packet->len = subnet->owner->mtu;
444                 route_ipv6_unreachable(source, packet, ICMP6_PACKET_TOO_BIG, 0);
445                 return;
446         }
447
448         send_packet(subnet->owner, packet);
449 }
450
451 /* RFC 2461 */
452
453 static void route_neighborsol(node_t *source, vpn_packet_t *packet)
454 {
455         struct ip6_hdr ip6;
456         struct nd_neighbor_solicit ns;
457         struct nd_opt_hdr opt;
458         subnet_t *subnet;
459         uint16_t checksum;
460
461         struct {
462                 struct in6_addr ip6_src;        /* source address */
463                 struct in6_addr ip6_dst;        /* destination address */
464                 uint32_t length;
465                 uint32_t next;
466         } pseudo;
467
468         cp();
469
470         if(!checklength(source, packet, ether_size + ip6_size + ns_size + opt_size + ETH_ALEN))
471                 return;
472         
473         if(source != myself) {
474                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got neighbor solicitation request from %s (%s) while in router mode!"), source->name, source->hostname);
475                 return;
476         }
477
478         /* Copy headers from packet to structs on the stack */
479
480         memcpy(&ip6, packet->data + ether_size, ip6_size);
481         memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
482         memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
483
484         /* First, snatch the source address from the neighbor solicitation packet */
485
486         if(overwrite_mac)
487                 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
488
489         /* Check if this is a valid neighbor solicitation request */
490
491         if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
492            opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR) {
493                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
494                 return;
495         }
496
497         /* Create pseudo header */
498
499         pseudo.ip6_src = ip6.ip6_src;
500         pseudo.ip6_dst = ip6.ip6_dst;
501         pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
502         pseudo.next = htonl(IPPROTO_ICMPV6);
503
504         /* Generate checksum */
505
506         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
507         checksum = inet_checksum(&ns, ns_size, checksum);
508         checksum = inet_checksum(&opt, opt_size, checksum);
509         checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
510
511         if(checksum) {
512                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
513                 return;
514         }
515
516         /* Check if the IPv6 address exists on the VPN */
517
518         subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
519
520         if(!subnet) {
521                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
522                                    ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
523                                    ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
524                                    ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
525                                    ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
526                                    ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
527                                    ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
528                                    ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
529                                    ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
530
531                 return;
532         }
533
534         /* Check if it is for our own subnet */
535
536         if(subnet->owner == myself)
537                 return;                                 /* silently ignore */
538
539         /* Create neighbor advertation reply */
540
541         memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN);        /* copy destination address */
542         packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
543
544         ip6.ip6_dst = ip6.ip6_src;                      /* swap destination and source protocoll address */
545         ip6.ip6_src = ns.nd_ns_target;
546
547         memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN);   /* add fake source hard addr */
548
549         ns.nd_ns_cksum = 0;
550         ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
551         ns.nd_ns_reserved = htonl(0x40000000UL);        /* Set solicited flag */
552         opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
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         ns.nd_ns_hdr.icmp6_cksum = checksum;
569
570         /* Copy structs on stack back to packet */
571
572         memcpy(packet->data + ether_size, &ip6, ip6_size);
573         memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
574         memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
575
576         send_packet(source, packet);
577 }
578
579 static __inline__ void route_ipv6(node_t *source, vpn_packet_t *packet)
580 {
581         cp();
582
583         if(!checklength(source, packet, ether_size + ip6_size))
584                 return;
585
586         if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
587                 route_neighborsol(source, packet);
588                 return;
589         }
590
591         route_ipv6_unicast(source, packet);
592 }
593
594 /* RFC 826 */
595
596 static void route_arp(node_t *source, vpn_packet_t *packet)
597 {
598         struct ether_arp arp;
599         subnet_t *subnet;
600         struct in_addr addr;
601
602         cp();
603
604         if(!checklength(source, packet, ether_size + arp_size))
605                 return;
606
607         if(source != myself) {
608                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Got ARP request from %s (%s) while in router mode!"), source->name, source->hostname);
609                 return;
610         }
611
612         /* First, snatch the source address from the ARP packet */
613
614         if(overwrite_mac)
615                 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
616
617         /* Copy headers from packet to structs on the stack */
618
619         memcpy(&arp, packet->data + ether_size, arp_size);
620
621         /* Check if this is a valid ARP request */
622
623         if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
624            arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
625                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
626                 return;
627         }
628
629         /* Check if the IPv4 address exists on the VPN */
630
631         subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
632
633         if(!subnet) {
634                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
635                                    arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
636                                    arp.arp_tpa[3]);
637                 return;
638         }
639
640         /* Check if it is for our own subnet */
641
642         if(subnet->owner == myself)
643                 return;                                 /* silently ignore */
644
645         memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN);        /* copy destination address */
646         packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
647
648         memcpy(&addr, arp.arp_tpa, sizeof(addr));       /* save protocol addr */
649         memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
650         memcpy(arp.arp_spa, &addr, sizeof(addr));       /* ... */
651
652         memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN);     /* set target hard/proto addr */
653         memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
654         arp.arp_op = htons(ARPOP_REPLY);
655
656         /* Copy structs on stack back to packet */
657
658         memcpy(packet->data + ether_size, &arp, arp_size);
659
660         send_packet(source, packet);
661 }
662
663 void route(node_t *source, vpn_packet_t *packet)
664 {
665         cp();
666
667         if(!checklength(source, packet, ether_size))
668                 return;
669
670         switch (routing_mode) {
671                 case RMODE_ROUTER:
672                         {
673                                 uint16_t type;
674
675                                 type = ntohs(*((uint16_t *)(&packet->data[12])));
676                                 switch (type) {
677                                         case ETH_P_ARP:
678                                                 route_arp(source, packet);
679                                                 break;
680
681                                         case ETH_P_IP:
682                                                 route_ipv4(source, packet);
683                                                 break;
684
685                                         case ETH_P_IPV6:
686                                                 route_ipv6(source, packet);
687                                                 break;
688
689                                         default:
690                                                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet from %s (%s): unknown type %hx"), source->name, source->hostname, type);
691                                                 break;
692                                 }
693                         }
694                         break;
695
696                 case RMODE_SWITCH:
697                         route_mac(source, packet);
698                         break;
699
700                 case RMODE_HUB:
701                         broadcast_packet(source, packet);
702                         break;
703         }
704 }