]> git.meshlink.io Git - meshlink/blob - src/route.c
Replace Opaque and Strict options with a TunnelServer option.
[meshlink] / src / route.c
1 /*
2     route.c -- routing
3     Copyright (C) 2000-2003 Ivo Timmermans <ivo@o2w.nl>,
4                   2000-2003 Guus Sliepen <guus@sliepen.eu.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
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: route.c,v 1.1.2.68 2003/10/06 13:57:12 guus Exp $
21 */
22
23 #include "system.h"
24
25 #ifdef HAVE_NET_ETHERNET_H
26 #include <net/ethernet.h>
27 #endif
28 #ifdef HAVE_NET_IF_ARP_H
29 #include <net/if_arp.h>
30 #endif
31 #ifdef HAVE_NETINET_IP_ICMP_H
32 #include <netinet/ip_icmp.h>
33 #endif
34 #ifdef HAVE_NETINET_ICMP6_H
35 #include <netinet/icmp6.h>
36 #endif
37 #ifdef HAVE_NETINET_IF_ETHER_H
38 #include <netinet/if_ether.h>
39 #endif
40
41 #include "avl_tree.h"
42 #include "connection.h"
43 #include "device.h"
44 #include "ethernet.h"
45 #include "ipv4.h"
46 #include "ipv6.h"
47 #include "logger.h"
48 #include "net.h"
49 #include "protocol.h"
50 #include "route.h"
51 #include "subnet.h"
52 #include "utils.h"
53
54 rmode_t routing_mode = RMODE_ROUTER;
55 bool priorityinheritance = false;
56 int macexpire = 600;
57 bool overwrite_mac = false;
58 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
59
60 /* Sizes of various headers */
61
62 static const size_t ether_size = sizeof(struct ether_header);
63 static const size_t arp_size = sizeof(struct ether_arp);
64 static const size_t ip_size = sizeof(struct ip);
65 static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
66 static const size_t ip6_size = sizeof(struct ip6_hdr);
67 static const size_t icmp6_size = sizeof(struct icmp6_hdr);
68 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
69 static const size_t opt_size = sizeof(struct nd_opt_hdr);
70
71 /* RFC 1071 */
72
73 static uint16_t inet_checksum(void *data, int len, uint16_t prevsum)
74 {
75         uint16_t *p = data;
76         uint32_t checksum = prevsum ^ 0xFFFF;
77
78         while(len >= 2) {
79                 checksum += *p++;
80                 len -= 2;
81         }
82         
83         if(len)
84                 checksum += *(unsigned char *)p;
85
86         while(checksum >> 16)
87                 checksum = (checksum & 0xFFFF) + (checksum >> 16);
88
89         return ~checksum;
90 }
91
92 static bool ratelimit(int frequency) {
93         static time_t lasttime = 0;
94         static int count = 0;
95         
96         if(lasttime == now) {
97                 if(++count > frequency)
98                         return true;
99         } else {
100                 lasttime = now;
101                 count = 0;
102         }
103
104         return false;
105 }
106         
107 static void learn_mac(mac_t *address)
108 {
109         subnet_t *subnet;
110         avl_node_t *node;
111         connection_t *c;
112
113         cp();
114
115         subnet = lookup_subnet_mac(address);
116
117         /* If we don't know this MAC address yet, store it */
118
119         if(!subnet || subnet->owner != myself) {
120                 ifdebug(TRAFFIC) logger(LOG_INFO, _("Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx"),
121                                    address->x[0], address->x[1], address->x[2], address->x[3],
122                                    address->x[4], address->x[5]);
123
124                 subnet = new_subnet();
125                 subnet->type = SUBNET_MAC;
126                 memcpy(&subnet->net.mac.address, address, sizeof(mac_t));
127                 subnet_add(myself, subnet);
128
129                 /* And tell all other tinc daemons it's our MAC */
130
131                 for(node = connection_tree->head; node; node = node->next) {
132                         c = node->data;
133                         if(c->status.active)
134                                 send_add_subnet(c, subnet);
135                 }
136         }
137
138         subnet->net.mac.lastseen = now;
139 }
140
141 void age_mac(void)
142 {
143         subnet_t *s;
144         connection_t *c;
145         avl_node_t *node, *next, *node2;
146
147         cp();
148
149         for(node = myself->subnet_tree->head; node; node = next) {
150                 next = node->next;
151                 s = node->data;
152                 if(s->type == SUBNET_MAC && s->net.mac.lastseen && s->net.mac.lastseen + macexpire < now) {
153                         ifdebug(TRAFFIC) logger(LOG_INFO, _("MAC address %hx:%hx:%hx:%hx:%hx:%hx expired"),
154                                            s->net.mac.address.x[0], s->net.mac.address.x[1],
155                                            s->net.mac.address.x[2], s->net.mac.address.x[3],
156                                            s->net.mac.address.x[4], s->net.mac.address.x[5]);
157
158                         for(node2 = connection_tree->head; node2; node2 = node2->next) {
159                                 c = node2->data;
160                                 if(c->status.active)
161                                         send_del_subnet(c, s);
162                         }
163
164                         subnet_del(myself, s);
165                 }
166         }
167 }
168
169 static node_t *route_mac(vpn_packet_t *packet)
170 {
171         subnet_t *subnet;
172
173         cp();
174
175         /* Learn source address */
176
177         learn_mac((mac_t *)(&packet->data[6]));
178
179         /* Lookup destination address */
180
181         subnet = lookup_subnet_mac((mac_t *)(&packet->data[0]));
182
183         if(subnet)
184                 return subnet->owner;
185         else
186                 return NULL;
187 }
188
189 /* RFC 792 */
190
191 static void route_ipv4_unreachable(vpn_packet_t *packet, uint8_t code)
192 {
193         struct ip ip;
194         struct icmp icmp;
195         
196         struct in_addr ip_src;
197         struct in_addr ip_dst;
198         uint32_t oldlen;
199
200         if(ratelimit(3))
201                 return;
202         
203         cp();
204
205         /* Copy headers from packet into properly aligned structs on the stack */
206
207         memcpy(&ip, packet->data + ether_size, ip_size);
208         memcpy(&icmp, packet->data + ether_size + ip_size, icmp_size);
209
210         /* Remember original source and destination */
211                 
212         memcpy(&ip_src, &ip.ip_src, sizeof(ip_src));
213         memcpy(&ip_dst, &ip.ip_dst, sizeof(ip_dst));
214
215         oldlen = packet->len - ether_size;
216         
217         if(oldlen >= IP_MSS - ip_size - icmp_size)
218                 oldlen = IP_MSS - ip_size - icmp_size;
219         
220         /* Copy first part of original contents to ICMP message */
221         
222         memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
223
224         /* Fill in IPv4 header */
225         
226         ip.ip_v = 4;
227         ip.ip_hl = ip_size / 4;
228         ip.ip_tos = 0;
229         ip.ip_len = htons(ip_size + icmp_size + oldlen);
230         ip.ip_id = 0;
231         ip.ip_off = 0;
232         ip.ip_ttl = 255;
233         ip.ip_p = IPPROTO_ICMP;
234         ip.ip_sum = 0;
235         memcpy(&ip.ip_src, &ip_dst, sizeof(ip_src));
236         memcpy(&ip.ip_dst, &ip_src, sizeof(ip_dst));
237
238         ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
239         
240         /* Fill in ICMP header */
241         
242         icmp.icmp_type = ICMP_DEST_UNREACH;
243         icmp.icmp_code = code;
244         icmp.icmp_cksum = 0;
245         
246         icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
247         icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
248
249         /* Copy structs on stack back to packet */
250
251         memcpy(packet->data + ether_size, &ip, ip_size);
252         memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
253         
254         packet->len = ether_size + ip_size + icmp_size + oldlen;
255         
256         write_packet(packet);
257 }
258
259 static node_t *route_ipv4(vpn_packet_t *packet)
260 {
261         subnet_t *subnet;
262
263         cp();
264
265         if(priorityinheritance)
266                 packet->priority = packet->data[15];
267
268         subnet = lookup_subnet_ipv4((ipv4_t *) &packet->data[30]);
269
270         if(!subnet) {
271                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: unknown IPv4 destination address %d.%d.%d.%d"),
272                                    packet->data[30], packet->data[31], packet->data[32],
273                                    packet->data[33]);
274
275                 route_ipv4_unreachable(packet, ICMP_NET_UNKNOWN);
276                 return NULL;
277         }
278         
279         if(!subnet->owner->status.reachable)
280                 route_ipv4_unreachable(packet, ICMP_NET_UNREACH);
281
282         return subnet->owner;
283 }
284
285 /* RFC 2463 */
286
287 static void route_ipv6_unreachable(vpn_packet_t *packet, uint8_t code)
288 {
289         struct ip6_hdr ip6;
290         struct icmp6_hdr icmp6;
291         uint16_t checksum;      
292
293         struct {
294                 struct in6_addr ip6_src;        /* source address */
295                 struct in6_addr ip6_dst;        /* destination address */
296                 uint32_t length;
297                 uint32_t next;
298         } pseudo;
299
300         if(ratelimit(3))
301                 return;
302         
303         cp();
304
305         /* Copy headers from packet to structs on the stack */
306
307         memcpy(&ip6, packet->data + ether_size, ip6_size);
308         memcpy(&icmp6, packet->data + ether_size + ip6_size, icmp6_size);
309
310         /* Remember original source and destination */
311                 
312         memcpy(&pseudo.ip6_src, &ip6.ip6_dst, sizeof(ip6.ip6_src));
313         memcpy(&pseudo.ip6_dst, &ip6.ip6_src, sizeof(ip6.ip6_dst));
314
315         pseudo.length = ntohs(ip6.ip6_plen) + ip6_size;
316         
317         if(pseudo.length >= IP_MSS - ip6_size - icmp6_size)
318                 pseudo.length = IP_MSS - ip6_size - icmp6_size;
319         
320         /* Copy first part of original contents to ICMP message */
321         
322         memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
323
324         /* Fill in IPv6 header */
325         
326         ip6.ip6_flow = htonl(0x60000000UL);
327         ip6.ip6_plen = htons(icmp6_size + pseudo.length);
328         ip6.ip6_nxt = IPPROTO_ICMPV6;
329         ip6.ip6_hlim = 255;
330         memcpy(&ip6.ip6_src, &pseudo.ip6_src, sizeof(ip6.ip6_src));
331         memcpy(&ip6.ip6_dst, &pseudo.ip6_dst, sizeof(ip6.ip6_dst));
332
333         /* Fill in ICMP header */
334         
335         icmp6.icmp6_type = ICMP6_DST_UNREACH;
336         icmp6.icmp6_code = code;
337         icmp6.icmp6_cksum = 0;
338
339         /* Create pseudo header */
340                 
341         pseudo.length = htonl(icmp6_size + pseudo.length);
342         pseudo.next = htonl(IPPROTO_ICMPV6);
343
344         /* Generate checksum */
345         
346         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
347         checksum = inet_checksum(&icmp6, icmp6_size, checksum);
348         checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
349
350         icmp6.icmp6_cksum = checksum;
351
352         /* Copy structs on stack back to packet */
353
354         memcpy(packet->data + ether_size, &ip6, ip6_size);
355         memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
356         
357         packet->len = ether_size + ip6_size + ntohl(pseudo.length);
358         
359         write_packet(packet);
360 }
361
362 static node_t *route_ipv6(vpn_packet_t *packet)
363 {
364         subnet_t *subnet;
365
366         cp();
367
368         subnet = lookup_subnet_ipv6((ipv6_t *) &packet->data[38]);
369
370         if(!subnet) {
371                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
372                                    ntohs(*(uint16_t *) &packet->data[38]),
373                                    ntohs(*(uint16_t *) &packet->data[40]),
374                                    ntohs(*(uint16_t *) &packet->data[42]),
375                                    ntohs(*(uint16_t *) &packet->data[44]),
376                                    ntohs(*(uint16_t *) &packet->data[46]),
377                                    ntohs(*(uint16_t *) &packet->data[48]),
378                                    ntohs(*(uint16_t *) &packet->data[50]),
379                                    ntohs(*(uint16_t *) &packet->data[52]));
380                 route_ipv6_unreachable(packet, ICMP6_DST_UNREACH_ADDR);
381
382                 return NULL;
383         }
384
385         if(!subnet->owner->status.reachable)
386                 route_ipv6_unreachable(packet, ICMP6_DST_UNREACH_NOROUTE);
387         
388         return subnet->owner;
389 }
390
391 /* RFC 2461 */
392
393 static void route_neighborsol(vpn_packet_t *packet)
394 {
395         struct ip6_hdr ip6;
396         struct nd_neighbor_solicit ns;
397         struct nd_opt_hdr opt;
398         subnet_t *subnet;
399         uint16_t checksum;
400
401         struct {
402                 struct in6_addr ip6_src;        /* source address */
403                 struct in6_addr ip6_dst;        /* destination address */
404                 uint32_t length;
405                 uint32_t next;
406         } pseudo;
407
408         cp();
409
410         /* Copy headers from packet to structs on the stack */
411
412         memcpy(&ip6, packet->data + ether_size, ip6_size);
413         memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
414         memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
415
416         /* First, snatch the source address from the neighbor solicitation packet */
417
418         if(overwrite_mac)
419                 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
420
421         /* Check if this is a valid neighbor solicitation request */
422
423         if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
424            opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR) {
425                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
426                 return;
427         }
428
429         /* Create pseudo header */
430
431         memcpy(&pseudo.ip6_src, &ip6.ip6_src, sizeof(ip6.ip6_src));
432         memcpy(&pseudo.ip6_dst, &ip6.ip6_dst, sizeof(ip6.ip6_dst));
433         pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
434         pseudo.next = htonl(IPPROTO_ICMPV6);
435
436         /* Generate checksum */
437
438         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
439         checksum = inet_checksum(&ns, ns_size, checksum);
440         checksum = inet_checksum(&opt, opt_size, checksum);
441
442         if(checksum) {
443                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
444                 return;
445         }
446
447         /* Check if the IPv6 address exists on the VPN */
448
449         subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
450
451         if(!subnet) {
452                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
453                                    ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
454                                    ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
455                                    ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
456                                    ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
457                                    ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
458                                    ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
459                                    ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
460                                    ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
461
462                 return;
463         }
464
465         /* Check if it is for our own subnet */
466
467         if(subnet->owner == myself)
468                 return;                                 /* silently ignore */
469
470         /* Create neighbor advertation reply */
471
472         memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN);        /* copy destination address */
473         packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
474
475         memcpy(&ip6.ip6_src, &ns.nd_ns_target, sizeof(ip6.ip6_src));    /* swap destination and source protocol address */
476         memcpy(&ip6.ip6_dst, &ip6.ip6_src, sizeof(ip6.ip6_dst));        /* ... */
477
478         memcpy(&opt + opt_size, packet->data + ETH_ALEN, ETH_ALEN);     /* add fake source hard addr */
479
480         ns.nd_ns_cksum = 0;
481         ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
482         ns.nd_ns_reserved = htonl(0x40000000UL);        /* Set solicited flag */
483         opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
484
485         /* Create pseudo header */
486
487         memcpy(&pseudo.ip6_src, &ip6.ip6_src, sizeof(ip6.ip6_src));
488         memcpy(&pseudo.ip6_dst, &ip6.ip6_dst, sizeof(ip6.ip6_dst));
489         pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
490         pseudo.next = htonl(IPPROTO_ICMPV6);
491
492         /* Generate checksum */
493
494         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
495         checksum = inet_checksum(&ns, ns_size, checksum);
496         checksum = inet_checksum(&opt, opt_size, checksum);
497
498         ns.nd_ns_hdr.icmp6_cksum = checksum;
499
500         /* Copy structs on stack back to packet */
501
502         memcpy(packet->data + ether_size, &ip6, ip6_size);
503         memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
504         memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
505
506         write_packet(packet);
507 }
508
509 /* RFC 826 */
510
511 static void route_arp(vpn_packet_t *packet)
512 {
513         struct ether_arp arp;
514         subnet_t *subnet;
515         struct in_addr addr;
516
517         cp();
518
519         /* First, snatch the source address from the ARP packet */
520
521         if(overwrite_mac)
522                 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
523
524         /* Copy headers from packet to structs on the stack */
525
526         memcpy(&arp, packet->data + ether_size, arp_size);
527
528         /* Check if this is a valid ARP request */
529
530         if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
531            arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
532                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
533                 return;
534         }
535
536         /* Check if the IPv4 address exists on the VPN */
537
538         subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
539
540         if(!subnet) {
541                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
542                                    arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
543                                    arp.arp_tpa[3]);
544                 return;
545         }
546
547         /* Check if it is for our own subnet */
548
549         if(subnet->owner == myself)
550                 return;                                 /* silently ignore */
551
552         memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN);        /* copy destination address */
553         packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
554
555         memcpy(&addr, arp.arp_tpa, sizeof(addr));       /* save protocol addr */
556         memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
557         memcpy(arp.arp_spa, &addr, sizeof(addr));       /* ... */
558
559         memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN);     /* set target hard/proto addr */
560         memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
561         arp.arp_op = htons(ARPOP_REPLY);
562
563         /* Copy structs on stack back to packet */
564
565         memcpy(packet->data + ether_size, &arp, arp_size);
566
567         write_packet(packet);
568 }
569
570 void route_outgoing(vpn_packet_t *packet)
571 {
572         uint16_t type;
573         node_t *n = NULL;
574
575         cp();
576
577         if(packet->len < ether_size) {
578                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
579                 return;
580         }
581
582         /* FIXME: multicast? */
583
584         switch (routing_mode) {
585                 case RMODE_ROUTER:
586                         type = ntohs(*((uint16_t *)(&packet->data[12])));
587                         switch (type) {
588                                 case ETH_P_IP:
589                                         if(packet->len < ether_size + ip_size) {
590                                                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
591                                                 return;
592                                         }
593
594                                         n = route_ipv4(packet);
595                                         break;
596
597                                 case ETH_P_IPV6:
598                                         if(packet->len < ether_size + ip6_size) {
599                                                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
600                                                 return;
601                                         }
602
603                                         if(packet->data[20] == IPPROTO_ICMPV6 && packet->len >= ether_size + ip6_size + ns_size && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
604                                                 route_neighborsol(packet);
605                                                 return;
606                                         }
607                                         n = route_ipv6(packet);
608                                         break;
609
610                                 case ETH_P_ARP:
611                                         if(packet->len < ether_size + arp_size) {
612                                                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
613                                                 return;
614                                         }
615
616                                         route_arp(packet);
617                                         return;
618
619                                 default:
620                                         ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: unknown type %hx"), type);
621                                         return;
622                         }
623                         if(n)
624                                 send_packet(n, packet);
625                         break;
626
627                 case RMODE_SWITCH:
628                         n = route_mac(packet);
629                         if(n)
630                                 send_packet(n, packet);
631                         else
632                                 broadcast_packet(myself, packet);
633                         break;
634
635                 case RMODE_HUB:
636                         broadcast_packet(myself, packet);
637                         break;
638         }
639 }
640
641 void route_incoming(node_t *source, vpn_packet_t *packet)
642 {
643         if(packet->len < ether_size) {
644                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
645                 return;
646         }
647
648         switch (routing_mode) {
649                 case RMODE_ROUTER:
650                         {
651                                 node_t *n = NULL;
652                                 uint16_t type;
653
654                                 type = ntohs(*((uint16_t *)(&packet->data[12])));
655                                 switch (type) {
656                                         case ETH_P_IP:
657                                                 if(packet->len < ether_size + ip_size) {
658                                                         ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
659                                                         return;
660                                                 }
661
662                                                 n = route_ipv4(packet);
663                                                 break;
664
665                                         case ETH_P_IPV6:
666                                                 if(packet->len < ether_size + ip6_size) {
667                                                         ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
668                                                         return;
669                                                 }
670
671                                                 n = route_ipv6(packet);
672                                                 break;
673
674                                         default:
675                                                 n = myself;
676                                                 break;
677                                 }
678
679                                 if(n) {
680                                         if(n == myself) {
681                                                 if(overwrite_mac)
682                                                         memcpy(packet->data, mymac.x, ETH_ALEN);
683                                                 write_packet(packet);
684                                         } else
685                                                 send_packet(n, packet);
686                                 }
687                         }
688                         break;
689
690                 case RMODE_SWITCH:
691                         {
692                                 subnet_t *subnet;
693
694                                 subnet = lookup_subnet_mac((mac_t *)(&packet->data[0]));
695
696                                 if(subnet) {
697                                         if(subnet->owner == myself)
698                                                 write_packet(packet);
699                                         else
700                                                 send_packet(subnet->owner, packet);
701                                 } else {
702                                         broadcast_packet(source, packet);
703                                         write_packet(packet);
704                                 }
705                         }
706                         break;
707
708                 case RMODE_HUB:
709                         broadcast_packet(source, packet);       /* Spread it on */
710                         write_packet(packet);
711                         break;
712         }
713 }