a11c55aefeee1ca3906cc17b13a4afff8d55b5a1
[oweals/tinc.git] / 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.5 2001/01/07 15:25:49 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;
37
38 void learn_mac(connection_t *source, mac_t *address)
39 {
40   connection_t *old;
41   subnet_t *subnet;
42 cp
43   old = lookup_subnet_mac(address)->owner;
44   
45   if(!old)
46     {
47       subnet = new_subnet();
48       subnet->type = SUBNET_MAC;
49 //      subnet->lasttime = gettimeofday();
50       memcpy(&subnet->net.mac.address, address, sizeof(mac_t));
51       subnet_add(source, subnet);
52
53       if(DEBUG_LVL >= DEBUG_TRAFFIC)
54         {
55           syslog(LOG_DEBUG, _("Learned new MAC address %x:%x:%x:%x:%x:%x from %s (%s)"),
56                address->address.x[0],
57                address->address.x[1],
58                address->address.x[2],
59                address->address.x[3],
60                address->address.x[4],
61                address->address.x[5],
62                cl->name, cl->hostname);
63         }
64     }
65 }
66
67 connection_t *route_mac(connection_t *source, vpn_packet_t *packet)
68 {
69   connection_t *oldsrc, *dst;
70   subnet_t *subnet;
71 cp
72   /* Learn source address */
73
74   learn_mac(source, (mac_t *)(&packet->data[0]));
75   
76   /* Lookup destination address */
77     
78   dst = lookup_subnet_mac((mac_t *)(&packet->data[6]))->owner;
79
80   if(!dst)
81     if(debug_lvl >= DEBUG_TRAFFIC)
82       {
83         syslog(LOG_WARNING, _("Cannot route packet: unknown destination address %x:%x:%x:%x:%x:%x"),
84                packet->data[6],
85                packet->data[7],
86                packet->data[8],
87                packet->data[9],
88                packet->data[10],
89                packet->data[11]);
90       } 
91 cp  
92   return dst;  
93 }
94
95 connection_t *route_ipv4(vpn_packet_t *packet)
96 {
97   ipv4_t dest;
98   connection_t *cl;
99 cp
100   dest = ntohl(*((unsigned long*)(&packet->data[30])));
101   
102   cl = lookup_subnet_ipv4(&dest)->owner;
103   if(!cl)
104     if(debug_lvl >= DEBUG_TRAFFIC)
105       {
106         syslog(LOG_WARNING, _("Cannot route packet: unknown destination address %d.%d.%d.%d"),
107                packet->data[30], packet->data[31], packet->data[32], packet->data[33]);
108       } 
109 cp
110   return cl;  
111 }
112
113 connection_t *route_ipv6(vpn_packet_t *packet)
114 {
115 cp
116   if(debug_lvl > DEBUG_NOTHING)
117     {
118       syslog(LOG_WARNING, _("Cannot route packet: IPv6 routing not yet implemented"));
119     } 
120 cp
121   return NULL;
122 }
123
124 void route_outgoing(vpn_packet_t *packet)
125 {
126   unsigned short int type;
127   avl_tree_t *node;
128   connection_t *cl;
129 cp
130   /* FIXME: multicast? */
131
132   switch(routing_mode)
133     {
134       case RMODE_ROUTER:
135         type = ntohs(*((unsigned short*)(&packet->data[12])));
136         switch(type)
137           {
138             case 0x0800:
139               cl = route_ipv4(packet);
140               break;
141             case 0x86DD:
142               cl = route_ipv6(packet);
143               break;
144             default:
145               if(debug_lvl >= DEBUG_TRAFFIC)
146                 {
147                   syslog(LOG_WARNING, _("Cannot route packet: unknown type %hx"), type);
148                 }
149               return;
150            }
151          send_packet(cl, packet);
152          break;
153         
154       case RMODE_SWITCH:
155         cl = route_mac(packet);
156         if(cl)
157           send_packet(cl, packet);
158         break;
159         
160       case RMODE_HUB:
161         for(node = connection_tree->head; node; node = node->next)
162           {
163             cl = (connection_t *)node->data;
164             if(cl->status.active)
165               send_packet(cl, packet);
166           }
167         break;
168     }
169 }
170
171 void route_incoming(connection_t *source, vpn_packet_t *packet)
172 {
173   switch(routing_mode)
174     {
175       case RMODE_SWITCH:
176         learn_mac(source, &packet->data[0]);
177         break;
178     }
179   
180   accept_packet(packet);
181 }