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