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