2000-10-23 13:52:54 +00:00
|
|
|
/*
|
|
|
|
route.c -- routing
|
2002-02-10 21:57:54 +00:00
|
|
|
Copyright (C) 2000-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
|
|
|
|
2000-2002 Guus Sliepen <guus@sliepen.warande.net>
|
2000-10-23 13:52:54 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
2002-02-20 16:04:59 +00:00
|
|
|
$Id: route.c,v 1.1.2.24 2002/02/20 16:04:59 guus Exp $
|
2000-10-23 13:52:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2002-02-10 21:57:54 +00:00
|
|
|
#if defined(HAVE_FREEBSD) || defined(HAVE_OPENBSD)
|
2001-05-28 08:21:43 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
2001-06-04 11:14:35 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
2002-02-10 21:57:54 +00:00
|
|
|
#if defined(HAVE_SOLARIS) || defined(HAVE_OPENBSD)
|
2001-07-21 20:21:25 +00:00
|
|
|
#include <net/if.h>
|
2001-06-21 16:16:32 +00:00
|
|
|
#define ETHER_ADDR_LEN 6
|
|
|
|
#else
|
|
|
|
#include <net/ethernet.h>
|
|
|
|
#endif
|
2001-06-04 11:14:35 +00:00
|
|
|
#include <netinet/if_ether.h>
|
2000-10-23 13:52:54 +00:00
|
|
|
#include <utils.h>
|
|
|
|
#include <xalloc.h>
|
2001-01-05 23:53:53 +00:00
|
|
|
#include <syslog.h>
|
2001-07-01 09:21:14 +00:00
|
|
|
#include <string.h>
|
2000-10-23 13:52:54 +00:00
|
|
|
|
2001-06-06 19:12:38 +00:00
|
|
|
#include <avl_tree.h>
|
|
|
|
|
2000-10-23 13:52:54 +00:00
|
|
|
#include "net.h"
|
2000-11-20 19:12:17 +00:00
|
|
|
#include "connection.h"
|
2001-01-05 23:53:53 +00:00
|
|
|
#include "subnet.h"
|
|
|
|
#include "route.h"
|
2001-06-06 19:12:38 +00:00
|
|
|
#include "protocol.h"
|
2001-10-27 13:13:35 +00:00
|
|
|
#include "device.h"
|
2000-10-23 13:52:54 +00:00
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
|
2001-01-07 15:25:49 +00:00
|
|
|
int routing_mode = RMODE_ROUTER;
|
2001-03-04 13:59:32 +00:00
|
|
|
subnet_t mymac;
|
2000-10-23 13:52:54 +00:00
|
|
|
|
2001-06-06 19:12:38 +00:00
|
|
|
void learn_mac(mac_t *address)
|
2000-10-23 13:52:54 +00:00
|
|
|
{
|
2001-01-05 23:53:53 +00:00
|
|
|
subnet_t *subnet;
|
2001-06-06 19:12:38 +00:00
|
|
|
avl_node_t *node;
|
2001-10-27 12:13:17 +00:00
|
|
|
connection_t *c;
|
2000-10-23 13:52:54 +00:00
|
|
|
cp
|
2001-03-04 13:59:32 +00:00
|
|
|
subnet = lookup_subnet_mac(address);
|
2001-06-06 19:12:38 +00:00
|
|
|
|
|
|
|
/* If we don't know this MAC address yet, store it */
|
2001-01-05 23:53:53 +00:00
|
|
|
|
2001-06-06 19:12:38 +00:00
|
|
|
if(!subnet || subnet->owner!=myself)
|
2000-11-04 22:57:33 +00:00
|
|
|
{
|
2001-06-06 19:12:38 +00:00
|
|
|
if(debug_lvl >= DEBUG_TRAFFIC)
|
2002-02-10 21:57:54 +00:00
|
|
|
syslog(LOG_INFO, _("Learned new MAC address %hx:%hx:%hx:%hx:%hx:%hx"),
|
2001-06-06 19:12:38 +00:00
|
|
|
address->x[0], address->x[1], address->x[2], address->x[3], address->x[4], address->x[5]);
|
|
|
|
|
2001-01-05 23:53:53 +00:00
|
|
|
subnet = new_subnet();
|
|
|
|
subnet->type = SUBNET_MAC;
|
2001-01-07 15:25:49 +00:00
|
|
|
memcpy(&subnet->net.mac.address, address, sizeof(mac_t));
|
2001-06-06 19:12:38 +00:00
|
|
|
subnet_add(myself, subnet);
|
2001-01-07 15:25:49 +00:00
|
|
|
|
2001-06-06 19:12:38 +00:00
|
|
|
/* And tell all other tinc daemons it's our MAC */
|
|
|
|
|
|
|
|
for(node = connection_tree->head; node; node = node->next)
|
2001-01-07 15:25:49 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
c = (connection_t *)node->data;
|
|
|
|
if(c->status.active)
|
|
|
|
send_add_subnet(c, subnet);
|
2001-01-07 15:25:49 +00:00
|
|
|
}
|
2000-11-04 22:57:33 +00:00
|
|
|
}
|
2001-01-07 15:25:49 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
node_t *route_mac(vpn_packet_t *packet)
|
2001-01-07 15:25:49 +00:00
|
|
|
{
|
|
|
|
subnet_t *subnet;
|
|
|
|
cp
|
|
|
|
/* Learn source address */
|
2000-11-04 22:57:33 +00:00
|
|
|
|
2001-06-06 19:12:38 +00:00
|
|
|
learn_mac((mac_t *)(&packet->data[6]));
|
2001-01-05 23:53:53 +00:00
|
|
|
|
|
|
|
/* Lookup destination address */
|
|
|
|
|
2001-06-05 16:09:55 +00:00
|
|
|
subnet = lookup_subnet_mac((mac_t *)(&packet->data[0]));
|
2001-03-04 13:59:32 +00:00
|
|
|
|
2001-06-05 16:09:55 +00:00
|
|
|
if(subnet)
|
|
|
|
return subnet->owner;
|
|
|
|
else
|
|
|
|
return NULL;
|
2000-11-04 22:57:33 +00:00
|
|
|
}
|
|
|
|
|
2002-02-10 21:57:54 +00:00
|
|
|
node_t *route_ipv4(vpn_packet_t *packet)
|
|
|
|
{
|
|
|
|
subnet_t *subnet;
|
|
|
|
cp
|
2002-02-18 16:25:19 +00:00
|
|
|
subnet = lookup_subnet_ipv4((ipv4_t *)&packet->data[30]);
|
2002-02-10 21:57:54 +00:00
|
|
|
cp
|
2001-03-04 13:59:32 +00:00
|
|
|
if(!subnet)
|
|
|
|
{
|
|
|
|
if(debug_lvl >= DEBUG_TRAFFIC)
|
|
|
|
{
|
2002-02-18 16:25:19 +00:00
|
|
|
syslog(LOG_WARNING, _("Cannot route packet: unknown IPv4 destination address %d.%d.%d.%d"),
|
2002-02-10 21:57:54 +00:00
|
|
|
packet->data[30], packet->data[31], packet->data[32], packet->data[33]);
|
2001-03-04 13:59:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-10-23 13:52:54 +00:00
|
|
|
cp
|
2001-03-04 13:59:32 +00:00
|
|
|
return subnet->owner;
|
2000-10-23 13:52:54 +00:00
|
|
|
}
|
|
|
|
|
2002-02-10 21:57:54 +00:00
|
|
|
node_t *route_ipv6(vpn_packet_t *packet)
|
2000-10-23 13:52:54 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
subnet_t *subnet;
|
|
|
|
cp
|
2002-02-10 21:57:54 +00:00
|
|
|
subnet = lookup_subnet_ipv6((ipv6_t *)&packet->data[38]);
|
2000-10-23 13:52:54 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(!subnet)
|
2001-01-05 23:53:53 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
if(debug_lvl >= DEBUG_TRAFFIC)
|
|
|
|
{
|
2002-02-10 21:57:54 +00:00
|
|
|
syslog(LOG_WARNING, _("Cannot route packet: unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
|
|
|
|
ntohs(*(short unsigned int *)&packet->data[38]),
|
|
|
|
ntohs(*(short unsigned int *)&packet->data[40]),
|
|
|
|
ntohs(*(short unsigned int *)&packet->data[42]),
|
|
|
|
ntohs(*(short unsigned int *)&packet->data[44]),
|
|
|
|
ntohs(*(short unsigned int *)&packet->data[46]),
|
|
|
|
ntohs(*(short unsigned int *)&packet->data[48]),
|
|
|
|
ntohs(*(short unsigned int *)&packet->data[50]),
|
|
|
|
ntohs(*(short unsigned int *)&packet->data[52]));
|
2001-10-27 12:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-10-23 13:52:54 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return subnet->owner;
|
2000-10-23 13:52:54 +00:00
|
|
|
}
|
2001-01-05 23:53:53 +00:00
|
|
|
|
2001-06-04 11:14:35 +00:00
|
|
|
void route_arp(vpn_packet_t *packet)
|
|
|
|
{
|
|
|
|
struct ether_arp *arp;
|
|
|
|
subnet_t *subnet;
|
|
|
|
unsigned char ipbuf[4];
|
|
|
|
cp
|
2001-06-05 16:31:59 +00:00
|
|
|
/* First, snatch the source address from the ARP packet */
|
|
|
|
|
|
|
|
memcpy(mymac.net.mac.address.x, packet->data + 6, 6);
|
|
|
|
|
2001-06-04 11:14:35 +00:00
|
|
|
/* This routine generates replies to ARP requests.
|
|
|
|
You don't need to set NOARP flag on the interface anymore (which is broken on FreeBSD).
|
|
|
|
Most of the code here is taken from choparp.c by Takamichi Tateoka (tree@mma.club.uec.ac.jp)
|
|
|
|
*/
|
|
|
|
|
|
|
|
arp = (struct ether_arp *)(packet->data + 14);
|
|
|
|
|
|
|
|
/* Check if this is a valid ARP request */
|
|
|
|
|
|
|
|
if(ntohs(arp->arp_hrd) != ARPHRD_ETHER ||
|
|
|
|
ntohs(arp->arp_pro) != ETHERTYPE_IP ||
|
|
|
|
(int) (arp->arp_hln) != ETHER_ADDR_LEN ||
|
|
|
|
(int) (arp->arp_pln) != 4 ||
|
|
|
|
ntohs(arp->arp_op) != ARPOP_REQUEST )
|
|
|
|
{
|
|
|
|
if(debug_lvl > DEBUG_TRAFFIC)
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, _("Cannot route packet: received unknown type ARP request"));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the IP address exists on the VPN */
|
2002-02-10 21:57:54 +00:00
|
|
|
|
2002-02-18 16:25:19 +00:00
|
|
|
subnet = lookup_subnet_ipv4((ipv4_t *)arp->arp_tpa);
|
2001-06-04 11:14:35 +00:00
|
|
|
|
|
|
|
if(!subnet)
|
|
|
|
{
|
|
|
|
if(debug_lvl >= DEBUG_TRAFFIC)
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, _("Cannot route packet: ARP request for unknown address %d.%d.%d.%d"),
|
|
|
|
arp->arp_tpa[0], arp->arp_tpa[1], arp->arp_tpa[2], arp->arp_tpa[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if it is for our own subnet */
|
|
|
|
|
|
|
|
if(subnet->owner == myself)
|
|
|
|
return; /* silently ignore */
|
|
|
|
|
|
|
|
memcpy(packet->data, packet->data + ETHER_ADDR_LEN, ETHER_ADDR_LEN); /* copy destination address */
|
|
|
|
packet->data[ETHER_ADDR_LEN*2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
|
|
|
|
|
|
|
|
memcpy(ipbuf, arp->arp_tpa, 4); /* save protocol addr */
|
|
|
|
memcpy(arp->arp_tpa, arp->arp_spa, 4); /* swap destination and source protocol address */
|
|
|
|
memcpy(arp->arp_spa, ipbuf, 4); /* ... */
|
|
|
|
|
|
|
|
memcpy(arp->arp_tha, arp->arp_sha, 10); /* set target hard/proto addr */
|
|
|
|
memcpy(arp->arp_sha, packet->data + ETHER_ADDR_LEN, ETHER_ADDR_LEN); /* add fake source hard addr */
|
|
|
|
arp->arp_op = htons(ARPOP_REPLY);
|
|
|
|
|
2001-10-27 13:13:35 +00:00
|
|
|
write_packet(packet);
|
2001-06-04 11:14:35 +00:00
|
|
|
cp
|
|
|
|
}
|
|
|
|
|
2001-01-07 15:25:49 +00:00
|
|
|
void route_outgoing(vpn_packet_t *packet)
|
2001-01-05 23:53:53 +00:00
|
|
|
{
|
|
|
|
unsigned short int type;
|
2001-10-27 12:13:17 +00:00
|
|
|
node_t *n;
|
2001-01-05 23:53:53 +00:00
|
|
|
cp
|
|
|
|
/* FIXME: multicast? */
|
|
|
|
|
|
|
|
switch(routing_mode)
|
|
|
|
{
|
|
|
|
case RMODE_ROUTER:
|
|
|
|
type = ntohs(*((unsigned short*)(&packet->data[12])));
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case 0x0800:
|
2001-10-27 12:13:17 +00:00
|
|
|
n = route_ipv4(packet);
|
2001-01-07 15:25:49 +00:00
|
|
|
break;
|
2001-01-05 23:53:53 +00:00
|
|
|
case 0x86DD:
|
2001-10-27 12:13:17 +00:00
|
|
|
n = route_ipv6(packet);
|
2001-01-07 15:25:49 +00:00
|
|
|
break;
|
2001-06-04 11:14:35 +00:00
|
|
|
case 0x0806:
|
|
|
|
route_arp(packet);
|
|
|
|
return;
|
2001-01-05 23:53:53 +00:00
|
|
|
default:
|
|
|
|
if(debug_lvl >= DEBUG_TRAFFIC)
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, _("Cannot route packet: unknown type %hx"), type);
|
|
|
|
}
|
2001-01-07 15:25:49 +00:00
|
|
|
return;
|
2001-01-05 23:53:53 +00:00
|
|
|
}
|
2001-10-27 12:13:17 +00:00
|
|
|
if(n)
|
|
|
|
send_packet(n, packet);
|
2001-01-07 15:25:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RMODE_SWITCH:
|
2001-10-27 12:13:17 +00:00
|
|
|
n = route_mac(packet);
|
|
|
|
if(n)
|
|
|
|
send_packet(n, packet);
|
2001-06-05 16:09:55 +00:00
|
|
|
else
|
|
|
|
broadcast_packet(myself, packet);
|
2001-01-07 15:25:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RMODE_HUB:
|
2001-06-05 16:09:55 +00:00
|
|
|
broadcast_packet(myself, packet);
|
2001-01-07 15:25:49 +00:00
|
|
|
break;
|
2001-01-05 23:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
2001-01-07 15:25:49 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
void route_incoming(node_t *source, vpn_packet_t *packet)
|
2001-01-07 15:25:49 +00:00
|
|
|
{
|
|
|
|
switch(routing_mode)
|
|
|
|
{
|
2001-05-25 11:54:28 +00:00
|
|
|
case RMODE_ROUTER:
|
2002-02-20 16:04:59 +00:00
|
|
|
{
|
|
|
|
node_t *n;
|
|
|
|
|
|
|
|
n = route_ipv4(packet);
|
|
|
|
|
|
|
|
if(n)
|
|
|
|
{
|
|
|
|
if(n == myself)
|
|
|
|
{
|
|
|
|
memcpy(packet->data, mymac.net.mac.address.x, 6);
|
|
|
|
write_packet(packet);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
send_packet(n, packet);
|
|
|
|
}
|
|
|
|
}
|
2001-05-25 11:54:28 +00:00
|
|
|
break;
|
2001-01-07 15:25:49 +00:00
|
|
|
case RMODE_SWITCH:
|
2001-06-21 16:16:32 +00:00
|
|
|
{
|
|
|
|
subnet_t *subnet;
|
|
|
|
|
|
|
|
subnet = lookup_subnet_mac((mac_t *)(&packet->data[0]));
|
|
|
|
|
|
|
|
if(subnet)
|
|
|
|
{
|
|
|
|
if(subnet->owner == myself)
|
2001-10-27 13:13:35 +00:00
|
|
|
write_packet(packet);
|
2001-06-21 16:16:32 +00:00
|
|
|
else
|
|
|
|
send_packet(subnet->owner, packet);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
broadcast_packet(source, packet);
|
2001-10-27 13:13:35 +00:00
|
|
|
write_packet(packet);
|
2001-06-21 16:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
2001-06-05 16:09:55 +00:00
|
|
|
break;
|
|
|
|
case RMODE_HUB:
|
2001-10-27 12:13:17 +00:00
|
|
|
broadcast_packet(source, packet); /* Spread it on */
|
2001-10-27 13:13:35 +00:00
|
|
|
write_packet(packet);
|
2001-01-07 15:25:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|