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