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