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