- Let user choose whether keys are in the config files or separate
[tinc] / src / route.c
1 /*
2     route.c -- routing
3     Copyright (C) 2000 Ivo Timmermans <itimmermans@bigfoot.com>,
4                   2000 Guus Sliepen <guus@sliepen.warande.net>
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.4 2001/01/05 23:53:53 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <utils.h>
26 #include <xalloc.h>
27 #include <syslog.h>
28
29 #include "net.h"
30 #include "connection.h"
31 #include "subnet.h"
32 #include "route.h"
33
34 #include "system.h"
35
36 int routing_mode = RMODE_ROUTER;        /* Will be used to determine if we route by MAC or by payload's protocol */
37
38 connection_t *route_mac(connection_t *source, vpn_packet_t *packet)
39 {
40   connection_t *oldsrc, *dst;
41   subnet_t *subnet;
42 cp
43   /* Learn source address */
44
45   oldsrc = lookup_subnet_mac((mac_t *)(&packet->data[0]))->owner;
46   
47   if(!oldsrc)
48     {
49       subnet = new_subnet();
50       subnet->type = SUBNET_MAC;
51       memcpy(&subnet->net.mac.address, (mac_t *)(&packet->data[0]), sizeof(mac_t));
52       subnet_add(source, subnet);
53     }
54
55   /* FIXME: do ageing and roaming */
56   
57   /* Lookup destination address */
58     
59   dst = lookup_subnet_mac((mac_t *)(&packet->data[6]))->owner;
60
61   if(!dst)
62     if(debug_lvl >= DEBUG_TRAFFIC)
63       {
64         syslog(LOG_WARNING, _("Cannot route packet: unknown destination address %x:%x:%x:%x:%x:%x"),
65                packet->data[6],
66                packet->data[7],
67                packet->data[8],
68                packet->data[9],
69                packet->data[10],
70                packet->data[11]);
71       } 
72 cp  
73   return dst;  
74 }
75
76 connection_t *route_ipv4(vpn_packet_t *packet)
77 {
78   ipv4_t dest;
79   connection_t *cl;
80 cp
81   dest = ntohl(*((unsigned long*)(&packet->data[30])));
82   
83   cl = lookup_subnet_ipv4(&dest)->owner;
84   if(!cl)
85     if(debug_lvl >= DEBUG_TRAFFIC)
86       {
87         syslog(LOG_WARNING, _("Cannot route packet: unknown destination address %d.%d.%d.%d"),
88                packet->data[30], packet->data[31], packet->data[32], packet->data[33]);
89       } 
90 cp
91   return cl;  
92 }
93
94 connection_t *route_ipv6(vpn_packet_t *packet)
95 {
96 cp
97   if(debug_lvl > DEBUG_NOTHING)
98     {
99       syslog(LOG_WARNING, _("Cannot route packet: IPv6 routing not implemented yet"));
100     } 
101 cp
102   return NULL;
103 }
104
105 connection_t *route_packet(connection_t *source, vpn_packet_t *packet)
106 {
107   unsigned short int type;
108 cp
109   /* FIXME: multicast? */
110
111   switch(routing_mode)
112     {
113       case RMODE_HUB:
114         return broadcast;
115         
116       case RMODE_SWITCH:
117         return route_mac(source, packet);
118         
119       case RMODE_ROUTER:
120         type = ntohs(*((unsigned short*)(&packet->data[12])));
121         switch(type)
122           {
123             case 0x0800:
124               return route_ipv4(packet);
125             case 0x86DD:
126               return route_ipv6(packet);
127       /*
128             case 0x8137:
129               return route_ipx(packet);
130             case 0x0806:
131               return route_arp(packet);
132       */
133             default:
134               if(debug_lvl >= DEBUG_TRAFFIC)
135                 {
136                   syslog(LOG_WARNING, _("Cannot route packet: unknown type %hx"), type);
137                 }
138               return NULL;
139            }
140     }
141 }