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