b586157d760de0d965cce1be211a9baae53f50d4
[oweals/tinc.git] / 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.69 2003/12/08 12:00:40 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         checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
442
443         if(checksum) {
444                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: checksum error for neighbor solicitation request"));
445                 return;
446         }
447
448         /* Check if the IPv6 address exists on the VPN */
449
450         subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
451
452         if(!subnet) {
453                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
454                                    ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
455                                    ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
456                                    ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
457                                    ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
458                                    ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
459                                    ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
460                                    ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
461                                    ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
462
463                 return;
464         }
465
466         /* Check if it is for our own subnet */
467
468         if(subnet->owner == myself)
469                 return;                                 /* silently ignore */
470
471         /* Create neighbor advertation reply */
472
473         memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN);        /* copy destination address */
474         packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
475
476         memcpy(&ip6.ip6_dst, &ip6.ip6_src, sizeof(ip6.ip6_dst));        /* ... */
477         memcpy(&ip6.ip6_src, &ns.nd_ns_target, sizeof(ip6.ip6_src));    /* swap destination and source protocol address */
478
479         memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN);   /* add fake source hard addr */
480
481         ns.nd_ns_cksum = 0;
482         ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
483         ns.nd_ns_reserved = htonl(0x40000000UL);        /* Set solicited flag */
484         opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
485
486         /* Create pseudo header */
487
488         memcpy(&pseudo.ip6_src, &ip6.ip6_src, sizeof(ip6.ip6_src));
489         memcpy(&pseudo.ip6_dst, &ip6.ip6_dst, sizeof(ip6.ip6_dst));
490         pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
491         pseudo.next = htonl(IPPROTO_ICMPV6);
492
493         /* Generate checksum */
494
495         checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
496         checksum = inet_checksum(&ns, ns_size, checksum);
497         checksum = inet_checksum(&opt, opt_size, checksum);
498         checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
499
500         ns.nd_ns_hdr.icmp6_cksum = checksum;
501
502         /* Copy structs on stack back to packet */
503
504         memcpy(packet->data + ether_size, &ip6, ip6_size);
505         memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
506         memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
507
508         write_packet(packet);
509 }
510
511 /* RFC 826 */
512
513 static void route_arp(vpn_packet_t *packet)
514 {
515         struct ether_arp arp;
516         subnet_t *subnet;
517         struct in_addr addr;
518
519         cp();
520
521         /* First, snatch the source address from the ARP packet */
522
523         if(overwrite_mac)
524                 memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
525
526         /* Copy headers from packet to structs on the stack */
527
528         memcpy(&arp, packet->data + ether_size, arp_size);
529
530         /* Check if this is a valid ARP request */
531
532         if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
533            arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
534                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
535                 return;
536         }
537
538         /* Check if the IPv4 address exists on the VPN */
539
540         subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
541
542         if(!subnet) {
543                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
544                                    arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
545                                    arp.arp_tpa[3]);
546                 return;
547         }
548
549         /* Check if it is for our own subnet */
550
551         if(subnet->owner == myself)
552                 return;                                 /* silently ignore */
553
554         memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN);        /* copy destination address */
555         packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
556
557         memcpy(&addr, arp.arp_tpa, sizeof(addr));       /* save protocol addr */
558         memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
559         memcpy(arp.arp_spa, &addr, sizeof(addr));       /* ... */
560
561         memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN);     /* set target hard/proto addr */
562         memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
563         arp.arp_op = htons(ARPOP_REPLY);
564
565         /* Copy structs on stack back to packet */
566
567         memcpy(packet->data + ether_size, &arp, arp_size);
568
569         write_packet(packet);
570 }
571
572 void route_outgoing(vpn_packet_t *packet)
573 {
574         uint16_t type;
575         node_t *n = NULL;
576
577         cp();
578
579         if(packet->len < ether_size) {
580                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
581                 return;
582         }
583
584         /* FIXME: multicast? */
585
586         switch (routing_mode) {
587                 case RMODE_ROUTER:
588                         type = ntohs(*((uint16_t *)(&packet->data[12])));
589                         switch (type) {
590                                 case ETH_P_IP:
591                                         if(packet->len < ether_size + ip_size) {
592                                                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
593                                                 return;
594                                         }
595
596                                         n = route_ipv4(packet);
597                                         break;
598
599                                 case ETH_P_IPV6:
600                                         if(packet->len < ether_size + ip6_size) {
601                                                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
602                                                 return;
603                                         }
604
605                                         if(packet->data[20] == IPPROTO_ICMPV6 && packet->len >= ether_size + ip6_size + ns_size && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
606                                                 route_neighborsol(packet);
607                                                 return;
608                                         }
609                                         n = route_ipv6(packet);
610                                         break;
611
612                                 case ETH_P_ARP:
613                                         if(packet->len < ether_size + arp_size) {
614                                                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
615                                                 return;
616                                         }
617
618                                         route_arp(packet);
619                                         return;
620
621                                 default:
622                                         ifdebug(TRAFFIC) logger(LOG_WARNING, _("Cannot route packet: unknown type %hx"), type);
623                                         return;
624                         }
625                         if(n)
626                                 send_packet(n, packet);
627                         break;
628
629                 case RMODE_SWITCH:
630                         n = route_mac(packet);
631                         if(n)
632                                 send_packet(n, packet);
633                         else
634                                 broadcast_packet(myself, packet);
635                         break;
636
637                 case RMODE_HUB:
638                         broadcast_packet(myself, packet);
639                         break;
640         }
641 }
642
643 void route_incoming(node_t *source, vpn_packet_t *packet)
644 {
645         if(packet->len < ether_size) {
646                 ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
647                 return;
648         }
649
650         switch (routing_mode) {
651                 case RMODE_ROUTER:
652                         {
653                                 node_t *n = NULL;
654                                 uint16_t type;
655
656                                 type = ntohs(*((uint16_t *)(&packet->data[12])));
657                                 switch (type) {
658                                         case ETH_P_IP:
659                                                 if(packet->len < ether_size + ip_size) {
660                                                         ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
661                                                         return;
662                                                 }
663
664                                                 n = route_ipv4(packet);
665                                                 break;
666
667                                         case ETH_P_IPV6:
668                                                 if(packet->len < ether_size + ip6_size) {
669                                                         ifdebug(TRAFFIC) logger(LOG_WARNING, _("Read too short packet"));
670                                                         return;
671                                                 }
672
673                                                 n = route_ipv6(packet);
674                                                 break;
675
676                                         default:
677                                                 n = myself;
678                                                 break;
679                                 }
680
681                                 if(n) {
682                                         if(n == myself) {
683                                                 if(overwrite_mac)
684                                                         memcpy(packet->data, mymac.x, ETH_ALEN);
685                                                 write_packet(packet);
686                                         } else
687                                                 send_packet(n, packet);
688                                 }
689                         }
690                         break;
691
692                 case RMODE_SWITCH:
693                         {
694                                 subnet_t *subnet;
695
696                                 subnet = lookup_subnet_mac((mac_t *)(&packet->data[0]));
697
698                                 if(subnet) {
699                                         if(subnet->owner == myself)
700                                                 write_packet(packet);
701                                         else
702                                                 send_packet(subnet->owner, packet);
703                                 } else {
704                                         broadcast_packet(source, packet);
705                                         write_packet(packet);
706                                 }
707                         }
708                         break;
709
710                 case RMODE_HUB:
711                         broadcast_packet(source, packet);       /* Spread it on */
712                         write_packet(packet);
713                         break;
714         }
715 }