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