Commit b54949cb authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Initial import.

parents
Copyright (c) 2007 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
PREFIX = /usr/local
CDEBUGFLAGS = -Os -g -Wall
DEFINES = $(PLATFORM_DEFINES)
CFLAGS = $(CDEBUGFLAGS) $(DEFINES) $(EXTRA_DEFINES)
SRCS = ariadne.c net.c kernel.c util.c destination.c neighbour.c \
route.c xroute.c message.c
OBJS = ariadne.o net.o kernel.o util.o destination.o neighbour.o \
route.o xroute.o message.o
ariadne: $(OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) -o ariadne $(OBJS) $(LDLIBS)
ariadne.html: ariadne.man
groff -man -Thtml ariadne.man > ariadne.html
.PHONY: all install uninstall clean
all: ariadne
install: ariadne ariadne.man
-rm -f $(TARGET)$(PREFIX)/bin/ariadne
cp -f ariadne $(TARGET)$(PREFIX)/bin
mkdir -p $(TARGET)$(PREFIX)/man/man8
cp -f ariadne.man $(TARGET)$(PREFIX)/man/man8/ariadne.8
uninstall:
-rm -f $(TARGET)$(PREFIX)/bin/ariadne
-rm -f $(TARGET)$(PREFIX)/man/man8/ariadne.8
clean:
-rm -f ariadne ariadne.html *.o *~ core TAGS gmon.out
Ariadne
*******
Ariadne is a loop-avoiding distance-vector routing protocol roughly
based on HSDV and AODV, but with provisions for link cost estimation
and injection of external routes.
Installation
************
$ make
$ su -c 'make install'
Running Ariadne
***************
In order to run Ariadne on a node (wireless or wired), just give it an
IPv6 address, then run the routing daemon. Assuming your wireless
interface is eth1, and your IPv6 address is $IPv6, do
# ip -6 addr add $IPv6 dev eth1
# ariadne $IPv6 eth1
If your node has multiple interfaces which you want to participate in
the Ariadne netowk, just list them all:
# ariadne $IPv6 eth0 eth1 sit1
On an access point, you'll probably want to inject external routes
into the Ariadne network. First check that you have a default route
on your access point:
$ ip -6 route show default
default via ...
Then run the routing daemon as so:
# ariadne -n default 256 $IPv6 eth1
Juliusz
This diff is collapsed.
/*
Copyright (c) 2007 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#define MAXROUTES 512
#define MAXDESTS 1024
#define MAXNEIGHBOURS 128
#define MAXNETS 8
#define MAXXROUTES 64
#define MAXMYXROUTES 8
#define MAX_BUFFERED_UPDATES 100
#define INFINITY ((unsigned short)(~0))
#undef MAX
#undef MIN
#define MAX(x,y) ((x)<=(y)?(y):(x))
#define MIN(x,y) ((x)<=(y)?(x):(y))
#if defined(__GNUC__) && (__GNUC__ >= 3)
#define ATTRIBUTE(x) __attribute__(x)
#else
#define ATTRIBUTE(x) /**/
#endif
#ifndef IF_NAMESIZE
#include <net/if.h>
#endif
#ifdef HAVE_VALGRIND
#include <valgrind/memcheck.h>
#else
#ifndef VALGRIND_MAKE_MEM_UNDEFINED
#define VALGRIND_MAKE_MEM_UNDEFINED(a, b) do {} while(0)
#endif
#ifndef VALGRIND_CHECK_MEM_IS_DEFINED
#define VALGRIND_CHECK_MEM_IS_DEFINED(a, b) do {} while(0)
#endif
#endif
struct network {
unsigned int ifindex;
int wired;
unsigned short cost;
int hello_time;
int self_update_time;
int update_time;
char ifname[IF_NAMESIZE];
int buffered;
struct timeval flush_time;
int bufsize;
unsigned char *sendbuf;
unsigned char hello_seqno;
unsigned int hello_interval;
unsigned int self_update_interval;
};
extern struct timeval now;
extern int debug;
extern int reboot_time;
extern unsigned char myid[16];
extern struct network nets[MAXNETS];
extern int numnets;
extern int protocol_port;
extern unsigned char protocol_group[16];
extern int protocol_socket;
.TH ARIADNE 1
.SH NAME
ariadne \- ad-hoc network routing daemon
.SH SYNOPSIS
.B ariadne
[
.B \-m
.I multicast-address
]
[
.B \-p
.I port
] [
.B -S
.I state-file
] [
.B \-h
.I hello-interval
] [
.B \-H
.I wired-hello-interval
] [
.B \-u
.I update-interval
] [
.B \-k
.I kernel-metric
] [
.B \-s
] [
.B \-b
] [
.B \-p
] [
.B \-d
.I level
] [
.B \-n
.I
.I prefix cost
]...
.I address
.IR interface ...
.SH DESCRIPTION
Ariadne is a loop-avoiding distance-vector routing protocol roughly
based on HSDV and AODV, but with provisions for link cost estimation
and injection of external routes.
While it is optimised for wireless mesh networks, Ariadne will also
work efficiently on classical wired networks. In the worst case, it
will generate roughly double the amount of traffic that RIPng would
generate, while never counting to infinity.
.SH OPTIONS
.TP
.BI \-m " multicast-address"
Specify the link-local multicast address to be used by the protocol.
.TP
.BI \-p " port"
Specify the UDP port number to be used by the Ariadne protocol.
.TP
.BI \-S " state-file"
Set the name of the file used for preserving long-term information
between invocations of the
.B ariadne
daemon. If this file is deleted, the daemon will run in passive mode
for 3 minutes when it is next invoked (see
.B -P
below), and other hosts might initially not accept to route to it.
The default is
.BR /var/lib/ariadne-state .
.TP
.BI \-h " hello-interval"
Specify the interval in seconds at which periodic hello packets are
sent on wireless interfaces. The default is 8.
.TP
.BI \-H " wired-hello-interval"
Specify the interval in seconds at which periodic hello packets are
sent on wired interfaces. The default is 30.
.TP
.BI \-u " update-interval"
Specify the interval in seconds at which periodic routing table dumps
are made on all interfaces. The default is either the wired hello
interval, or 150, whichever is less.
.TP
.BI \-k " kernel-metric"
Specify a value that will be added to routes' metrics before
installing them in the kernel; this is useful when a single node
participates in multiple routing protocols. The default is 0.
.TP
.B \-s
Do not perform split-horizon processing on wired interfaces.
Split-horizon is never performed on wireless interfaces.
.TP
.B \-b
Generate messages for detecting bidirectional communication failures
on wired interfaces. By default, wired interfaces are assumed to be
symmetric.
.TP
.B \-P
Run in parasitic (passive) mode. The daemon will not announce any
routes except for itself.
.TP
.BI \-d " level"
Debug level. A value of 1 requests a routing table dump at every
iteration through the daemon's main loop. A value of 2 additionally
requests tracing every message sent or received. The default is 0.
.TP
.BI \-n " network cost"
Advertise an external route for the given network with the given cost.
We suggest a cost of 256 \[mu]
.RI ( n
+ 1) for a network
.I n
hops away.
.I Network
should be either the string
.BR default ,
or a nework specification in CIDR notation
.IB prefix / length.
If
.I length
is omitted, it defaults to 128 (a host route).
.TP
.I address
The IPv6 address that the node should advertise for itself.
(Additional addresses can be advertised as external routes with a cost
of 0.)
.TP
.IR interface ...
The list of interfaces on which the protocol should operate.
.SH WIRED INTERFACES
By default, the daemon optimises traffic on wired interfaces by
sending fewer periodic hello messages, performing split-horizon
processing and not generating any reverse reachability information.
These optimisations can be disabled using the
.BR -H ,
.B -s
and
.B -b
options respectively.
No link cost estimation is performed on wired interfaces: a wired
adjacency is assumed to be up if at least one of the last three hello
messages has been received, and down otherwise.
.SH FILES
.TP
.B /var/lib/ariadne-state
The default location of the file storing long-term state.
.SH SECURITY
Ariadne is a completely insecure protocol: any attacker able to
inject IP packets with a link-local source address can disrupt the
protocol's operation.
Since Ariadne uses link-local addresses only, there is no need to
update firewalls to allow forwarding of Ariadne protocol packets. If
ingress filtering is done on a host, the protocol port should be
opened. As Ariadne uses unicast packets in some cases, just allowing
packets destined to the multicast address is not enough.
.SH BUGS
Plenty. This is experimental software, run at your own risk.
.SH SEE ALSO
.BR routed (8),
.BR route6d (8),
.BR zebra (8),
.BR ahcpd (8).
.SH AUTHOR
Juliusz Chroboczek.
/*
Copyright (c) 2007 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ariadne.h"
#include "util.h"
#include "destination.h"
struct destination dests[MAXDESTS];
int numdests = 0;
struct destination *
find_destination(const unsigned char *d, int create, unsigned char seqno)
{
int i;
for(i = 0; i < numdests; i++) {
if(memcmp(dests[i].address, d, 16) == 0)
return &dests[i];
}
if(!create)
return NULL;
if(i >= numdests) {
if(numdests >= MAXDESTS) {
fprintf(stderr, "Too many destinations.\n");
return NULL;
}
memcpy(dests[numdests].address, d, 16);
numdests++;
}
dests[i].seqno = seqno;
dests[i].metric = INFINITY;
return &dests[i];
}
void
update_destination(struct destination *dest,
unsigned char seqno, unsigned short metric)
{
if(seqno_compare(dest->seqno, seqno) < 0 ||
(dest->seqno == seqno && dest->metric > metric)) {
dest->time = now.tv_sec;
dest->seqno = seqno;
dest->metric = metric;
}
}
/*
Copyright (c) 2007 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
struct destination {
unsigned char address[16];
unsigned char seqno;
unsigned short metric;
int time;
};
struct destination *find_destination(const unsigned char *d,
int create, unsigned char seqno);
void update_destination(struct destination *dest,
unsigned char seqno, unsigned short metric);
/*
Copyright (c) 2007 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/route.h>
#include <net/if.h>
#include "kernel.h"
static int old_forwarding = -1;
static int old_accept_redirects = -1;
static int
read_proc(char *filename)
{
char buf[100];
int fd, rc;
fd = open(filename, O_RDONLY);
if(fd < 0)
return -1;
rc = read(fd, buf, 99);
if(rc < 0) {
int saved_errno = errno;
close(fd);
errno = saved_errno;
return -1;
}
close(fd);
if(rc == 0)
return -1;
buf[rc] = '\0';
return atoi(buf);
}
static int
write_proc(char *filename, int value)
{
char buf[100];
int fd, rc, n;
n = snprintf(buf, 100, "%d", value);
fd = open(filename, O_WRONLY);
if(fd < 0)
return -1;
rc = write(fd, buf, n);
if(rc < n) {
int saved_errno = errno;
close(fd);
errno = saved_errno;
return -1;
}
close(fd);
return 1;
}
int
kernel_setup(int setup)
{
int rc;
if(setup) {
old_forwarding = read_proc("/proc/sys/net/ipv6/conf/all/forwarding");
if(old_forwarding < 0) {
perror("Couldn't read forwarding knob.");
return -1;
}
rc = write_proc("/proc/sys/net/ipv6/conf/all/forwarding", 1);
if(rc < 0) {
perror("Couldn't write forwarding knob.");
return -1;
}
old_accept_redirects =
read_proc("/proc/sys/net/ipv6/conf/all/accept_redirects");
if(old_accept_redirects < 0) {
perror("Couldn't read accept_redirects knob.");
return -1;
}
rc = write_proc("/proc/sys/net/ipv6/conf/all/accept_redirects", 0);
if(rc < 0) {
perror("Couldn't write accept_redirects knob.");
return -1;
}
return 1;
} else {
if(old_forwarding >= 0) {
rc = write_proc("/proc/sys/net/ipv6/conf/all/forwarding",
old_forwarding);
if(rc < 0) {
perror("Couldn't write accept_redirects knob.\n");
return -1;
}
}
if(old_accept_redirects >= 0) {
rc = write_proc("/proc/sys/net/ipv6/conf/all/accept_redirects",
old_accept_redirects);
if(rc < 0) {
perror("Couldn't write accept_redirects knob.\n");
return -1;
}
}
return 1;
}
}
int
kernel_setup_interface(int setup, const char *ifname, int ifindex)
{
return 1;
}
int
kernel_interface_mtu(const char *ifname, int ifindex)
{
struct ifreq req;
int s, rc;
s = socket(PF_INET, SOCK_DGRAM, 0);
if(s < 0)
return -1;
memset(&req, 0, sizeof(req));
strncpy(req.ifr_name, ifname, sizeof(req.ifr_name));
rc = ioctl(s, SIOCGIFMTU, &req);
if(rc < 0) {
close(s);
return -1;
}
return req.ifr_mtu;
}
int
kernel_interface_wireless(const char *ifname, int ifindex)
{
#ifndef SIOCGIWNAME
#define SIOCGIWNAME 0x8B01
#endif
struct ifreq req;
int s, rc;
s = socket(PF_INET, SOCK_DGRAM, 0);
if(s < 0)
return -1;
memset(&req, 0, sizeof(req));
strncpy(req.ifr_name, ifname, sizeof(req.ifr_name));
rc = ioctl(s, SIOCGIWNAME, &req);
if(rc < 0) {
if(errno == EOPNOTSUPP || errno == EINVAL)
rc = 0;
else {
perror("ioctl(SIOCGIWNAME)");
rc = -1;
}
} else {
rc = 1;
}
close(s);
return rc;
}
static int route_socket = -1;
int
kernel_route(int add, const unsigned char *dest, unsigned short plen,
const unsigned char *gate, int ifindex, int metric)
{
struct in6_rtmsg msg;
int rc;
if(route_socket < 0) {
route_socket = socket(AF_INET6, SOCK_DGRAM, 0);
if(route_socket < 0)
return -1;
}
memset(&msg, 0, sizeof(msg));
msg.rtmsg_flags = RTF_UP;
if(plen < 128) {
msg.rtmsg_flags |= RTF_GATEWAY;
} else {
msg.rtmsg_flags |= RTF_HOST;
if(memcmp(dest, gate, 16) != 0)
msg.rtmsg_flags |= RTF_GATEWAY;
}
msg.rtmsg_metric = metric;
memcpy(&msg.rtmsg_dst, dest, sizeof(struct in6_addr));
msg.rtmsg_dst_len = plen;
memcpy(&msg.rtmsg_gateway, gate, sizeof(struct in6_addr));
msg.rtmsg_ifindex = ifindex;
rc = ioctl(route_socket, add ? SIOCADDRT : SIOCDELRT, &msg);
if(rc < 0)
return -1;
return 1;
}
/*
Copyright (c) 2007 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
int kernel_setup(int setup);
int kernel_setup_interface(int setup, const char *ifname, int ifindex);
int kernel_interface_mtu(const char *ifname, int ifindex);
int kernel_interface_wireless(const char *ifname, int ifindex);
int kernel_route(int add, const unsigned char *dest, unsigned short plen,
const unsigned char *gate, int ifindex, int metric);
This diff is collapsed.
/*
Copyright (c) 2007 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
extern unsigned char seqno;
extern int seqno_time;
extern int seqno_interval;
extern unsigned int jitter;
extern unsigned int update_jitter;
extern int parasitic;
extern int silent_time;
extern int broadcast_txcost;
extern int split_horizon;
extern struct timeval update_flush_time;
extern const unsigned char packet_header[4];
void parse_packet(const unsigned char *from, struct network *net,
const unsigned char *packet, int len);
void flushbuf(struct network *net);
void send_hello(struct network *net);
void send_request(struct network *net, struct destination *dest);
void send_unicast_request(struct neighbour *neigh, struct destination *dest);
void send_update(struct destination *dest, struct network *net);
void send_self_update(struct network *net);
void send_self_retract(struct network *net);
void send_neighbour_update(struct neighbour *neigh, struct network *net);
void send_txcost(struct neighbour *neigh, struct network *net);
void schedule_flush_now(struct network *net);
void flushupdates(void);
/*
Copyright (c) 2007 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include "ariadne.h"
#include "util.h"
#include "neighbour.h"
#include "route.h"
#include "message.h"
struct neighbour neighs[MAXNEIGHBOURS];
int numneighs = 0;
struct neighbour *find_neighbour(const unsigned char *address,
struct network *net);
struct neighbour *
add_neighbour(const unsigned char *id, const unsigned char *address,
struct network *net);
void
flush_neighbour(struct neighbour *neigh)
{
flush_neighbour_routes(neigh);
memset(neigh, 0, sizeof(*neigh));
if(neigh == &neighs[numneighs - 1]) {
numneighs--;
VALGRIND_MAKE_MEM_UNDEFINED(neigh, sizeof(*neigh));
} else {
/* The first byte is used to mark unused entries */
VALGRIND_MAKE_MEM_UNDEFINED((char*)neigh + 1, sizeof(*neigh) - 1);
}
}
struct neighbour *
find_neighbour(const unsigned char *address, struct network *net)
{
int i;
for(i = 0; i < numneighs; i++) {
if(neighs[i].id[0] == 0)
continue;
if(memcmp(address, neighs[i].address, 16) == 0 &&
neighs[i].network == net)
return &neighs[i];
}
return NULL;
}
struct neighbour *
add_neighbour(const unsigned char *id, const unsigned char *address,
struct network *net)
{
struct neighbour *neigh;
int i;
neigh = find_neighbour(address, net);
if(neigh) {
if(memcmp(neigh->id, id, 16) == 0) {
return neigh;
} else {
fprintf(stderr, "Neighbour changed id (%s -> %s)!\n",
format_address(neigh->id), format_address(id));
flush_neighbour(neigh);
neigh = NULL;
}
}
debugf("Creating neighbour %s (%s).\n",
format_address(id), format_address(address));
for(i = 0; i < numneighs; i++) {
if(neighs[i].id[0] == 0)
neigh = &neighs[i];
}
if(!neigh) {
if(numneighs >= MAXNEIGHBOURS) {
fprintf(stderr, "Too many neighbours.\n");
return NULL;
}
neigh = &neighs[numneighs++];
}
memcpy(neigh->id, id, 16);
memcpy(neigh->address, address, 16);
neigh->hello_interval = 0;
neigh->reach = 0;
neigh->txcost = INFINITY;
neigh->txcost_time = now.tv_sec;
neigh->hello_time = 0;
neigh->network = net;
send_hello(net);
return neigh;
}
void
update_neighbour(struct neighbour *neigh, int hello, int hello_interval)
{
int missed_hellos;
if(hello < 0) {
if(neigh->hello_interval <= 0)
return;
missed_hellos =
(now.tv_sec - 3 - neigh->hello_time) / neigh->hello_interval;
if(missed_hellos <= 0)
return;
neigh->hello_time += missed_hellos * neigh->hello_interval;
} else {
if(neigh->reach > 0) {
missed_hellos = seqno_minus(hello, neigh->hello_seqno) - 1;
while(missed_hellos < 0) {
/* This neighbour has increased its hello interval, and we
didn't notice. */
neigh->reach <<= 1;
missed_hellos++;
}
} else {
missed_hellos = 0;
}
neigh->hello_time = now.tv_sec;
neigh->hello_interval = hello_interval;
}
while(missed_hellos) {
neigh->reach >>= 1;
missed_hellos--;
}
if(hello >= 0) {
neigh->hello_seqno = hello;
neigh->reach >>= 1;
neigh->reach |= 0x8000;
}
/* Make sure to give neighbours some feedback early after association */
if(neigh->reach == 0x8000) {
send_hello(neigh->network);
send_txcost(neigh, NULL);
} else {
int a = (neigh->reach & 0xC000);
int b = (neigh->reach & 0x3000);
if((a == 0xC000 && b == 0) || (a == 0 && b == 0x3000)) {
send_txcost(neigh, NULL);
send_self_update(neigh->network);
send_neighbour_update(neigh, NULL);
} else {
if((neigh->reach & 0xF000) != 0 && (neigh->reach & 0x0F000) == 0)
send_txcost(neigh, NULL);
}
}
}
void
check_neighbour(struct neighbour *neigh)
{
update_neighbour(neigh, -1, 0);
if(neigh->reach == 0) {
flush_neighbour(neigh);
return;
}
if(!neigh->network->wired) {
while(neigh->txcost < INFINITY &&
neigh->txcost_time <= now.tv_sec - 30) {
neigh->txcost = MIN((int)neigh->txcost * 5 / 4, INFINITY);
neigh->txcost_time += 30;
}
}
}
int
neighbour_cost(struct neighbour *neigh)
{
update_neighbour(neigh, -1, 0);
if(neigh->reach == 0)
return INFINITY;
else if(neigh->network->wired) {
if((neigh->reach & 0xE000) != 0)
return neigh->network->cost;
else
return INFINITY;
} else {
int r = (neigh->reach & 0x7FFF) + ((neigh->reach & 0x8000) >> 1);
double c = (((double)0xC000 * 0x100) / r);
/* Avoid neighbours that give poor feedback */
if(neigh->hello_interval == 0)
c += 256 + (now.tv_sec - neigh->hello_time) * 10;
else if(neigh->hello_interval >= 20)
c *= (neigh->hello_interval / 20.0);
return MIN((int)(c + neigh->network->cost + 0.5), INFINITY);
}
}
int
neighbour_symmetric_cost(struct neighbour *neigh)
{
int c;
/* (1/(alpha * beta) + 1/beta) / 2, which is half the expected
number of transmissions, in both directions.
ETX uses 1/(alpha * beta), which is the expected number of
transmissions in the forward direction. */
if(neigh->txcost >= INFINITY)
return INFINITY;
c = neighbour_cost(neigh);
if(c >= INFINITY)
return INFINITY;
return (c + ((c * neigh->txcost + 128) >> 8) + 1) / 2;
}
/*
Copyright (c) 2007 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
struct neighbour {
unsigned char id[16];
unsigned char address[16];
unsigned short reach;
unsigned short txcost;
int txcost_time;
int hello_time;
int hello_interval;
int hello_seqno;
struct network *network;
};
extern struct neighbour neighs[MAXNEIGHBOURS];
extern int numneighs;
void flush_neighbour(struct neighbour *neigh);
struct neighbour *find_neighbour(const unsigned char *address,
struct network *net);
struct neighbour *
add_neighbour(const unsigned char *id, const unsigned char *address,
struct network *net);
void update_neighbour(struct neighbour *neigh, int hello, int hello_interval);
void check_neighbour(struct neighbour *neigh);
int neighbour_cost(struct neighbour *neigh);
int neighbour_symmetric_cost(struct neighbour *neigh);
/*
Copyright (c) 2007 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include "net.h"
int
ariadne_socket(int port)
{
struct sockaddr_in6 sin6;
int s, rc;
int saved_errno;
int one = 1, zero = 0;
s = socket(PF_INET6, SOCK_DGRAM, 0);
if(s < 0)
return -1;
rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if(rc < 0)
goto fail;
memset(&sin6, 0, sizeof(sin6));
sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons(port);
rc = bind(s, (struct sockaddr*)&sin6, sizeof(sin6));
if(rc < 0)
goto fail;
rc = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
&zero, sizeof(zero));
if(rc < 0)
goto fail;
rc = fcntl(s, F_GETFL, 0);
if(rc < 0)
goto fail;
rc = fcntl(s, F_SETFL, (rc | O_NONBLOCK));
if(rc < 0)
goto fail;
return s;
fail:
saved_errno = errno;
close(s);
errno = saved_errno;
return -1;
}
int
ariadne_recv(int s, void *buf, int buflen, struct sockaddr *sin, int slen)
{
struct iovec iovec;
struct msghdr msg;
int rc;
memset(&msg, 0, sizeof(msg));
iovec.iov_base = buf;
iovec.iov_len = buflen;
msg.msg_name = sin;
msg.msg_namelen = slen;
msg.msg_iov = &iovec;
msg.msg_iovlen = 1;
rc = recvmsg(s, &msg, 0);
return rc;
}
int
ariadne_send(int s,
const void *buf1, int buflen1, const void *buf2, int buflen2,
const struct sockaddr *sin, int slen)
{
struct iovec iovec[2];
struct msghdr msg;
int rc;
iovec[0].iov_base = (void*)buf1;
iovec[0].iov_len = buflen1;
iovec[1].iov_base = (void*)buf2;
iovec[1].iov_len = buflen2;
memset(&msg, 0, sizeof(msg));
msg.msg_name = (struct sockaddr*)sin;
msg.msg_namelen = slen;
msg.msg_iov = iovec;
msg.msg_iovlen = 2;
rc = sendmsg(s, &msg, 0);
return rc;
}
/*
Copyright (c) 2007 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
int ariadne_socket(int port);
int ariadne_recv(int s, void *buf, int buflen, struct sockaddr *sin, int slen);
int ariadne_send(int s,
const void *buf1, int buflen1, const void *buf2, int buflen2,
const struct sockaddr *sin, int slen);
This diff is collapsed.
/*
Copyright (c) 2007 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
struct xroute;
struct route {
struct destination *dest;
unsigned short metric;
unsigned short refmetric;
unsigned char seqno;
struct neighbour *nexthop;
int time;
int origtime;
int installed;
};
extern struct route routes[MAXROUTES];
extern int numroutes;
extern int kernel_metric;
struct route *find_route(const unsigned char *dest, struct neighbour *nexthop);
struct route *find_installed_route(struct destination *dest);
void flush_route(struct route *route);
void flush_neighbour_routes(struct neighbour *neigh);
unsigned int metric_to_kernel(int metric);
void install_route(struct route *route);
void uninstall_route(struct route *route);
int route_feasible(struct route *route);
int update_feasible(unsigned char seqno, unsigned short metric,
struct destination *dest, struct route *route);
struct route *find_best_route(struct destination *dest);
void update_neighbour_metric(struct neighbour *neigh);
void update_route_metric(struct route *route);
struct route *update_route(const unsigned char *d, int seqno, int refmetric,
struct neighbour *nexthop,
struct xroute *pxroutes, int numpxroutes);
void consider_route(struct route *route);
void tweak_route(struct route *route,
int newseqno, int newrefmetric, int newmetric);
void send_triggered_update(struct route *route, int oldmetric);
/*
Copyright (c) 2007 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "ariadne.h"
int
seqno_compare(unsigned char s1, unsigned char s2)
{
if(s1 == s2)
return 0;
else if(((s2 - s1) & 0xFF) < 128)
return -1;
else
return 1;
}
int
seqno_minus(unsigned char s1, unsigned char s2)
{
if(s1 == s2)
return 0;
else if(((s2 - s1) & 0xFF) < 128)
return -(int)((s2 - s1) & 0xFF);
else
return ((s1 - s2) & 0xFF);
}
void
timeval_minus(struct timeval *d,
const struct timeval *s1, const struct timeval *s2)
{
if(s1->tv_usec > s2->tv_usec) {
d->tv_usec = s1->tv_usec - s2->tv_usec;
d->tv_sec = s1->tv_sec - s2->tv_sec;
} else {
d->tv_usec = s1->tv_usec + 1000000 - s2->tv_usec;
d->tv_sec = s1->tv_sec - s2->tv_sec - 1;
}
}
int
timeval_minus_msec(const struct timeval *s1, const struct timeval *s2)
{
return (s1->tv_sec - s2->tv_sec) * 1000 +
(s1->tv_usec - s2->tv_usec) / 1000;
}
int
timeval_compare(const struct timeval *s1, const struct timeval *s2)
{
if(s1->tv_sec < s2->tv_sec)
return -1;
else if(s1->tv_sec > s2->tv_sec)
return 1;
else if(s1->tv_usec < s2->tv_usec)
return -1;
else if(s1->tv_usec > s2->tv_usec)
return 1;
else
return 0;
}
/* {0, 0} represents infinity */
void
timeval_min(struct timeval *d, const struct timeval *s)
{
if(s->tv_sec == 0)
return;
if(d->tv_sec == 0 || timeval_compare(d, s) > 0) {
*d = *s;
}
}
void
timeval_min_sec(struct timeval *d, int secs)
{
if(d->tv_sec == 0 || d->tv_sec > secs) {
d->tv_sec = secs;
d->tv_usec = random() % 1000000;
}
}
void
do_debugf(const char *format, ...)
{
va_list args;
va_start(args, format);
if(debug >= 2)
vfprintf(stderr, format, args);
va_end(args);
fflush(stderr);
}
const char *
format_address(const unsigned char *address)
{
static char buf[4][INET6_ADDRSTRLEN];
static int i = 0;
i = (i + 1) % 4;
return inet_ntop(AF_INET6, address, buf[i], INET6_ADDRSTRLEN);
}
int
parse_address(const char *address, unsigned char *addr_r)
{
struct in6_addr ina6;
int rc;
rc = inet_pton(AF_INET6, address, &ina6);
if(rc > 0) {
memcpy(addr_r, &ina6, 16);
return 0;
}
return -1;
}
int
parse_net(const char *net, unsigned char *prefix_r, unsigned short *plen_r)
{
char buf[INET6_ADDRSTRLEN];
char *slash, *end;
unsigned char prefix[16];
unsigned short plen;
int rc;
if(strcmp(net, "default") == 0) {
memset(prefix, 0, 16);
plen = 0;
} else {
slash = strchr(net, '/');
if(slash == NULL) {
rc = parse_address(net, prefix);
if(rc < 0)
return rc;
plen = 128;
} else {
if(slash - net >= INET6_ADDRSTRLEN)
return -1;
memcpy(buf, net, slash - net);
buf[slash - net] = '\0';
rc = parse_address(buf, prefix);
if(rc < 0)
return rc;
plen = strtol(slash + 1, &end, 0);
if(*end != '\0')
return -1;
}
}
memcpy(prefix_r, prefix, 16);
*plen_r = plen;
return 0;
}
/*
Copyright (c) 2007 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
int seqno_compare(unsigned char s1, unsigned char s2)
ATTRIBUTE ((const));
int seqno_minus(unsigned char s1, unsigned char s2)
ATTRIBUTE ((const));
void timeval_minus(struct timeval *d,
const struct timeval *s1, const struct timeval *s2);
int timeval_minus_msec(const struct timeval *s1, const struct timeval *s2)
ATTRIBUTE ((pure));
int timeval_compare(const struct timeval *s1, const struct timeval *s2)
ATTRIBUTE ((pure));
void timeval_min(struct timeval *d, const struct timeval *s);
void timeval_min_sec(struct timeval *d, int secs);
void do_debugf(const char *format, ...) ATTRIBUTE ((format (printf, 1, 2)));
const char *format_address(const unsigned char *address);
int parse_address(const char *address, unsigned char *addr_r);
int parse_net(const char *net, unsigned char *prefix_r, unsigned short *plen_r);
/* If debugging is disabled, we want to avoid calling format_address
for every omitted debugging message. So debug is a macro. But
vararg macros are not portable... */
#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
#define debugf(...) \
do { \
if(debug >= 2) do_debugf(__VA_ARGS__); \
} while(0)
#elif defined(__GNUC__)
#define debugf(_args...) \
do { \
if(debug >= 2) do_debugf(_args); \
} while(0)
#else
#define debugf do_debugf
#endif
/*
Copyright (c) 2007 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "ariadne.h"
#include "kernel.h"
#include "neighbour.h"
#include "route.h"
#include "xroute.h"
struct xroute xroutes[MAXXROUTES];
int numxroutes = 0;
struct xroute myxroutes[MAXMYXROUTES];
int nummyxroutes = 0;
static struct xroute *
find_installed_xroute(unsigned char *prefix, unsigned short plen)
{
int i;
for(i = 0; i < numxroutes; i++) {
if(xroutes[i].plen == plen &&
memcmp(xroutes[i].prefix, prefix, 16) == 0) {
return &xroutes[i];
}
}
return NULL;
}
static struct xroute *
find_best_xroute(unsigned char *prefix, unsigned short plen)
{
struct xroute *xroute = NULL;
int i;
for(i = 0; i < numxroutes; i++) {
if(xroutes[i].plen == plen &&
memcmp(xroutes[i].prefix, prefix, 16) == 0) {
if((!xroute || xroutes[i].metric < xroute->metric) &&
find_installed_route(xroutes[i].gateway) != NULL)
xroute = &xroutes[i];
}
}
return xroute;
}
void
install_xroute(struct xroute *xroute)
{
struct route *gwroute;
struct xroute *installed;
int rc;
if(xroute->installed)
return;
if(xroute->metric >= INFINITY)
return;
if(xroute->plen >= 8 &&
(xroute->prefix[0] == 0 || xroute->prefix[0] == 0xFF)) {
fprintf(stderr, "Attempted to install martian xroute.\n");
return;
}
gwroute = find_installed_route(xroute->gateway);
if(!gwroute) {
fprintf(stderr,
"Attempted to install a blackhole xroute "
"(this shouldn't happen).\n");
return;
}
installed = find_installed_xroute(xroute->prefix, xroute->plen);
if(installed)
uninstall_xroute(installed);
rc = kernel_route(1, xroute->prefix, xroute->plen,
gwroute->nexthop->address,
gwroute->nexthop->network->ifindex,
metric_to_kernel(xroute->metric));
if(rc < 0) {
perror("kernel_route(1)");
if(errno != EEXIST)
return;
}
xroute->installed = 1;
}
void
uninstall_xroute(struct xroute *xroute)
{
struct route *gwroute;
int rc;
if(!xroute->installed)
return;
gwroute = find_installed_route(xroute->gateway);
if(!gwroute) {
fprintf(stderr,
"Attempted to uninstall a blackhole xroute "
"(this shouldn't happen).\n");
return;
}
rc = kernel_route(0, xroute->prefix, xroute->plen,
gwroute->nexthop->address,
gwroute->nexthop->network->ifindex,
metric_to_kernel(xroute->metric));
if(rc < 0)
perror("kernel_route(0)");
xroute->installed = 0;
}
void
consider_xroute(struct xroute *xroute)
{
struct xroute *installed;
if(xroute->installed)
return;
if(xroute->metric >= INFINITY)
return;
if(find_installed_route(xroute->gateway) == NULL)
return;
installed = find_installed_xroute(xroute->prefix, xroute->plen);
if(!installed || installed->metric >= xroute->metric)
install_xroute(xroute);
}
void
flush_xroute(struct xroute *xroute)
{
int n;
int install = 0;
unsigned char prefix[16];
unsigned short plen = 0;
n = xroute - xroutes;
assert(n >= 0 && n < numxroutes);
if(xroute->installed) {
uninstall_xroute(xroute);
memcpy(prefix, xroute->prefix, 16);
plen = xroute->plen;
install = 1;
}
if(n != numxroutes - 1)
memcpy(xroutes + n, xroutes + numxroutes - 1,
sizeof(struct xroute));
numxroutes--;
VALGRIND_MAKE_MEM_UNDEFINED(xroutes + numxroutes, sizeof(struct xroute));
if(install) {
struct xroute *xroute;
xroute = find_best_xroute(prefix, plen);
if(xroute)
install_xroute(xroute);
}
}
void
flush_xroutes(struct destination *gateway,
const struct xroute *except, int numexcept)
{
int i, j;
i = 0;
while(i < numxroutes) {
if(xroutes[i].gateway == gateway) {
for(j = 0; j < numexcept; j++) {
if(memcmp(xroutes[i].prefix, except[j].prefix, 16) == 0 &&
xroutes[i].plen == except[j].plen)
goto skip;
}
flush_xroute(&xroutes[i]);
continue;
}
skip:
i++;
}
}
struct xroute *
update_xroute(const unsigned char *prefix, unsigned short plen,
struct destination *gateway, int cost)
{
int i;
struct xroute *xroute = NULL;
struct route *gwroute;
if(prefix[0] == 0xFF || (plen >= 8 && prefix[0] == 0)) {
fprintf(stderr, "Ignoring martian xroute.\n");
return NULL;
}
if(gateway == NULL) {
fprintf(stderr, "Ignoring xroute through unknown destination.\n");
return NULL;
}
for(i = 0; i < numxroutes; i++) {
xroute = &xroutes[i];
if(xroute->gateway == gateway &&
memcmp(xroute->prefix, prefix, 16) == 0 && xroute->plen == plen) {
update_xroute_metric(xroute, cost);
xroute->time = now.tv_sec;
return xroute;
}
}
if(numxroutes >= MAXXROUTES) {
fprintf(stderr, "Too many xroutes.\n");
return NULL;
}
gwroute = find_installed_route(gateway);
xroute = &xroutes[numxroutes];
memcpy(&xroute->prefix, prefix, 16);
xroute->plen = plen;
xroute->gateway = gateway;
xroute->cost = cost;
xroute->metric =
gwroute ? MIN(gwroute->metric + cost, INFINITY) : INFINITY;
xroute->time = now.tv_sec;
xroute->installed = 0;
numxroutes++;
if(gwroute)
install_xroute(xroute);
return xroute;
}
void
update_xroute_metric(struct xroute *xroute, int cost)
{
struct route *gwroute;
gwroute = find_installed_route(xroute->gateway);
if(!gwroute)
gwroute = find_best_route(xroute->gateway);
if(!gwroute)
return;
if(xroute->cost != cost || xroute->metric != gwroute->metric + cost) {
int install = 0;
if(xroute->installed) {
uninstall_xroute(xroute);
install = 1;
}
xroute->cost = cost;
xroute->metric = gwroute->metric + cost;
if(install) {
struct xroute *best;
best = find_best_xroute(xroute->prefix, xroute->plen);
if(best)
install_xroute(best);
}
}
}
/*
Copyright (c) 2007 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
struct route;
struct xroute {
unsigned char prefix[16];
unsigned short plen;
struct destination *gateway;
int cost;
int metric;
int time;
int installed;
};
extern struct xroute xroutes[MAXXROUTES];
extern int numxroutes;
extern struct xroute myxroutes[MAXMYXROUTES];
extern int nummyxroutes;
void install_xroute(struct xroute *xroute);
void uninstall_xroute(struct xroute *xroute);
void consider_xroute(struct xroute *xroute);
void flush_xroute(struct xroute *xroute);
void flush_xroutes(struct destination *gateway,
const struct xroute *except, int numexcept);
struct xroute * update_xroute(const unsigned char *prefix, unsigned short plen,
struct destination *gateway, int cost);
void update_xroute_metric(struct xroute *xroute, int cost);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment