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