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