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