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-03-12 14:25:04 +00:00
|
|
|
$Id: route.c,v 1.1.2.32 2002/03/12 14:25:04 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
|
2002-03-12 14:25:04 +00:00
|
|
|
#ifdef NEIGHBORSOL
|
2002-03-11 13:14:53 +00:00
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#include <netinet/icmp6.h>
|
2002-03-12 14:25:04 +00:00
|
|
|
#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;
|
2002-03-01 12:26:56 +00:00
|
|
|
int priorityinheritance = 0;
|
2002-03-01 14:09:31 +00:00
|
|
|
int macexpire = 600;
|
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
|
|
|
}
|
2002-03-01 14:09:31 +00:00
|
|
|
|
|
|
|
subnet->net.mac.lastseen = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
void age_mac(void)
|
|
|
|
{
|
|
|
|
subnet_t *s;
|
|
|
|
connection_t *c;
|
|
|
|
avl_node_t *node, *next, *node2;
|
|
|
|
cp
|
|
|
|
for(node = myself->subnet_tree->head; node; node = next)
|
|
|
|
{
|
2002-03-01 14:33:48 +00:00
|
|
|
next = node->next;
|
2002-03-01 14:09:31 +00:00
|
|
|
s = (subnet_t *)node->data;
|
|
|
|
if(s->type == SUBNET_MAC && s->net.mac.lastseen && s->net.mac.lastseen + macexpire < now)
|
|
|
|
{
|
2002-03-01 14:25:10 +00:00
|
|
|
if(debug_lvl >= DEBUG_TRAFFIC)
|
|
|
|
syslog(LOG_INFO, _("MAC address %hx:%hx:%hx:%hx:%hx:%hx expired"),
|
|
|
|
s->net.mac.address.x[0], s->net.mac.address.x[1], s->net.mac.address.x[2], s->net.mac.address.x[3], s->net.mac.address.x[4], s->net.mac.address.x[5]);
|
2002-03-01 14:09:31 +00:00
|
|
|
for(node2 = connection_tree->head; node2; node2 = node2->next)
|
|
|
|
{
|
|
|
|
c = (connection_t *)node2->data;
|
|
|
|
if(c->status.active)
|
|
|
|
send_del_subnet(c, s);
|
|
|
|
}
|
|
|
|
subnet_del(myself, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cp
|
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-03-01 12:26:56 +00:00
|
|
|
if(priorityinheritance)
|
|
|
|
packet->priority = packet->data[15];
|
|
|
|
|
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
|
|
|
|
2002-03-12 14:25:04 +00:00
|
|
|
#ifdef NEIGHBORSOL
|
|
|
|
unsigned short int ipv6_cksum(short int *data, int len, unsigned short int cksum)
|
|
|
|
{
|
|
|
|
while(len--)
|
|
|
|
{
|
|
|
|
cksum += ntohs(*data++);
|
|
|
|
}
|
|
|
|
return cksum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void route_neighborsol(vpn_packet_t *packet)
|
2002-03-11 13:14:53 +00:00
|
|
|
{
|
|
|
|
struct ip6_hdr *hdr;
|
|
|
|
struct nd_neighbor_solicit *ns;
|
2002-03-12 14:25:04 +00:00
|
|
|
struct nd_opt_hdr *opt;
|
2002-03-11 13:14:53 +00:00
|
|
|
subnet_t *subnet;
|
2002-03-12 14:25:04 +00:00
|
|
|
short int cksum;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
struct in6_addr ip6_src; /* source address */
|
|
|
|
struct in6_addr ip6_dst; /* destination address */
|
|
|
|
uint32_t length;
|
|
|
|
uint8_t junk[4];
|
|
|
|
} pseudo;
|
|
|
|
|
2002-03-11 13:14:53 +00:00
|
|
|
cp
|
|
|
|
hdr = (struct ip6_hdr *)(packet->data + 14);
|
|
|
|
ns = (struct nd_neighbor_solicit *)(packet->data + 14 + sizeof(struct ip6_hdr));
|
2002-03-12 14:25:04 +00:00
|
|
|
opt = (struct nd_opt_hdr *)(packet->data + 14 + sizeof(struct ip6_hdr) + sizeof(struct nd_neighbor_solicit));
|
2002-03-11 13:14:53 +00:00
|
|
|
|
|
|
|
/* First, snatch the source address from the neighbor solicitation packet */
|
|
|
|
|
|
|
|
memcpy(mymac.net.mac.address.x, packet->data + 6, 6);
|
|
|
|
|
|
|
|
/* Check if this is a valid neighbor solicitation request */
|
|
|
|
|
2002-03-12 14:25:04 +00:00
|
|
|
if(ns->nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
|
|
|
|
opt->nd_opt_type != ND_OPT_SOURCE_LINKADDR)
|
2002-03-11 13:14:53 +00:00
|
|
|
{
|
|
|
|
if(debug_lvl > DEBUG_TRAFFIC)
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, _("Cannot route packet: received unknown type neighbor solicitation request"));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the IPv6 address exists on the VPN */
|
2002-03-12 14:25:04 +00:00
|
|
|
#if 0
|
2002-03-11 13:14:53 +00:00
|
|
|
subnet = lookup_subnet_ipv6((ipv6_t *)&ns->nd_ns_target);
|
|
|
|
|
|
|
|
if(!subnet)
|
|
|
|
{
|
|
|
|
if(debug_lvl >= DEBUG_TRAFFIC)
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, _("Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"),
|
|
|
|
ntohs(ns->nd_ns_target.s6_addr16[0]), ntohs(ns->nd_ns_target.s6_addr16[1]), ntohs(ns->nd_ns_target.s6_addr16[2]), ntohs(ns->nd_ns_target.s6_addr16[3]),
|
|
|
|
ntohs(ns->nd_ns_target.s6_addr16[4]), ntohs(ns->nd_ns_target.s6_addr16[5]), ntohs(ns->nd_ns_target.s6_addr16[6]), ntohs(ns->nd_ns_target.s6_addr16[7]));
|
|
|
|
}
|
|
|
|
|
2002-03-12 14:25:04 +00:00
|
|
|
return;
|
2002-03-11 13:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if it is for our own subnet */
|
|
|
|
|
|
|
|
if(subnet->owner == myself)
|
2002-03-12 14:25:04 +00:00
|
|
|
return; /* silently ignore */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
syslog(LOG_DEBUG, "Neighbor solicitation request with checksum %hx", ntohs(ns->nd_ns_hdr.icmp6_cksum));
|
|
|
|
|
|
|
|
/* Create pseudo header */
|
|
|
|
|
|
|
|
memcpy(&pseudo.ip6_src, &hdr->ip6_src, 16);
|
|
|
|
memcpy(&pseudo.ip6_dst, &hdr->ip6_dst, 16);
|
|
|
|
pseudo.length = htonl(sizeof(*ns));
|
|
|
|
pseudo.junk[0] = pseudo.junk[1] = pseudo.junk[2] = 0;
|
|
|
|
pseudo.junk[3] = IPPROTO_ICMPV6;
|
|
|
|
|
|
|
|
/* Generate checksum */
|
|
|
|
|
|
|
|
ns->nd_ns_hdr.icmp6_cksum = 0;
|
|
|
|
|
|
|
|
cksum = ipv6_cksum((short int *)&pseudo, sizeof(pseudo)/2, 0);
|
2002-03-11 13:14:53 +00:00
|
|
|
|
2002-03-12 14:25:04 +00:00
|
|
|
syslog(LOG_DEBUG, "Our checksum %hx", cksum);
|
2002-03-11 13:14:53 +00:00
|
|
|
|
2002-03-12 14:25:04 +00:00
|
|
|
cksum = ipv6_cksum((short int *)ns, sizeof(*ns)/2, cksum);
|
|
|
|
|
|
|
|
syslog(LOG_DEBUG, "Our checksum %hx", cksum);
|
|
|
|
|
|
|
|
cksum = ipv6_cksum((short int *)opt, sizeof(*opt)/2, cksum);
|
|
|
|
|
|
|
|
syslog(LOG_DEBUG, "Our checksum %hx", cksum);
|
|
|
|
|
|
|
|
/* Create neighbor advertation reply */
|
|
|
|
|
|
|
|
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(&hdr->ip6_dst, &hdr->ip6_src, 16); /* swap destination and source protocol address */
|
|
|
|
memcpy(&hdr->ip6_src, &ns->nd_ns_target, 16); /* ... */
|
|
|
|
|
|
|
|
memcpy((char *)opt + sizeof(*opt), packet->data + ETHER_ADDR_LEN, 6); /* add fake source hard addr */
|
|
|
|
|
|
|
|
ns->nd_ns_hdr.icmp6_cksum = 0;
|
|
|
|
ns->nd_ns_hdr.icmp6_type = ND_NEIGHBOR_ADVERT;
|
|
|
|
opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
|
|
|
|
|
|
|
|
/* Create pseudo header */
|
|
|
|
|
|
|
|
memcpy(&pseudo.ip6_src, &hdr->ip6_src, 16);
|
|
|
|
memcpy(&pseudo.ip6_dst, &hdr->ip6_dst, 16);
|
|
|
|
pseudo.length = htonl(sizeof(struct icmp6_hdr));
|
|
|
|
pseudo.junk[0] = pseudo.junk[1] = pseudo.junk[2] = 0;
|
|
|
|
pseudo.junk[3] = IPPROTO_ICMPV6;
|
|
|
|
|
|
|
|
/* Generate checksum */
|
|
|
|
|
|
|
|
cksum = ipv6_cksum((short int *)&pseudo, sizeof(pseudo)/2, 0);
|
|
|
|
cksum = ipv6_cksum((short int *)ns, sizeof(*ns)/2, cksum);
|
|
|
|
|
|
|
|
ns->nd_ns_hdr.icmp6_cksum = htons(cksum);
|
|
|
|
|
|
|
|
write_packet(packet);
|
2002-03-11 13:14:53 +00:00
|
|
|
cp
|
|
|
|
}
|
2002-03-12 14:25:04 +00:00
|
|
|
#endif
|
2002-03-11 13:14: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;
|
|
|
|
}
|
|
|
|
|
2002-03-11 13:14:53 +00:00
|
|
|
/* Check if the IPv4 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;
|
2002-03-10 14:04:48 +00:00
|
|
|
node_t *n = NULL;
|
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);
|
2002-03-12 14:25:04 +00:00
|
|
|
#ifdef NEIGHBORSOL
|
2002-03-11 13:56:00 +00:00
|
|
|
if(!n && packet->data[0] == 0x33 && packet->data[1] == 0x33 && packet->data[2] == 0xff)
|
2002-03-12 14:25:04 +00:00
|
|
|
{
|
|
|
|
route_neighborsol(packet);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
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
|
|
|
{
|
2002-03-10 14:04:48 +00:00
|
|
|
node_t *n = NULL;
|
|
|
|
unsigned short int type;
|
2002-02-20 16:04:59 +00:00
|
|
|
|
2002-03-10 14:04:48 +00:00
|
|
|
type = ntohs(*((unsigned short*)(&packet->data[12])));
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case 0x0800:
|
|
|
|
n = route_ipv4(packet);
|
|
|
|
break;
|
|
|
|
case 0x86DD:
|
|
|
|
n = route_ipv6(packet);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
n = myself;
|
|
|
|
break;
|
|
|
|
}
|
2002-02-20 16:04:59 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|