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