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