2006-06-17 22:41:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2005, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
2007-11-18 11:21:29 +00:00
|
|
|
* $Id: uaodv.c,v 1.34 2007/11/18 11:21:29 nvt-se Exp $
|
2006-06-17 22:41:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Micro implementation of the AODV ad hoc routing protocol
|
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "net/uaodv-def.h"
|
|
|
|
#include "net/uaodv-rt.h"
|
|
|
|
|
2007-05-07 11:51:20 +00:00
|
|
|
#define NDEBUG
|
|
|
|
#include "lib/assert.h"
|
|
|
|
|
2007-01-24 16:32:34 +00:00
|
|
|
#ifdef CC2420_RADIO
|
2006-06-17 22:41:10 +00:00
|
|
|
#include "dev/cc2420.h"
|
2007-05-21 15:26:57 +00:00
|
|
|
#define RSSI_THRESHOLD -39 /* accept -39 ... xx */
|
2006-06-17 22:41:10 +00:00
|
|
|
#endif
|
|
|
|
|
2007-04-30 15:26:09 +00:00
|
|
|
/* This implementation never expires routes!!! */
|
2007-05-31 10:10:26 +00:00
|
|
|
#define MY_ROUTE_TIMEOUT 0x7fffffff /* Should be 0xffffffff! */
|
|
|
|
#define MY_NET_DIAMETER 20
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
PROCESS(uaodv_process, "uAODV");
|
|
|
|
|
2007-04-30 15:26:09 +00:00
|
|
|
static struct uip_udp_conn *bcastconn, *unicastconn;
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-04-24 16:37:45 +00:00
|
|
|
/* Compare sequence numbers as per RFC 3561. */
|
2007-05-12 21:04:05 +00:00
|
|
|
#define SCMP32(a, b) ((s32_t)((a) - (b)))
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-05-07 11:51:20 +00:00
|
|
|
static CC_INLINE u32_t
|
2007-05-08 08:41:26 +00:00
|
|
|
last_known_seqno(uip_ipaddr_t *host)
|
2007-05-07 11:51:20 +00:00
|
|
|
{
|
2007-05-08 08:41:26 +00:00
|
|
|
struct uaodv_rt_entry *route = uaodv_rt_lookup_any(host);
|
|
|
|
|
|
|
|
if(route != NULL)
|
2007-05-09 13:54:41 +00:00
|
|
|
return htonl(route->hseqno);
|
2007-05-08 08:41:26 +00:00
|
|
|
|
2007-05-07 11:51:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-31 14:24:20 +00:00
|
|
|
#ifndef NOINIT
|
2007-04-04 12:12:21 +00:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#define NOINIT __attribute__((section(".noinit")))
|
2007-04-18 21:26:09 +00:00
|
|
|
#else /* __GNUC__ */
|
2007-04-04 12:12:21 +00:00
|
|
|
#define NOINIT
|
2007-04-18 21:26:09 +00:00
|
|
|
#endif /* __GNUC__ */
|
2007-05-31 14:24:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When possible, keep state across reboots.
|
|
|
|
*/
|
2007-05-09 13:54:41 +00:00
|
|
|
NOINIT static u32_t rreq_id, my_hseqno; /* In host byte order! */
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-04-04 12:12:21 +00:00
|
|
|
#define NFWCACHE 16
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-04-04 12:12:21 +00:00
|
|
|
static struct {
|
|
|
|
uip_ipaddr_t orig;
|
|
|
|
u32_t id;
|
|
|
|
} fwcache[NFWCACHE];
|
|
|
|
|
2007-04-18 21:26:09 +00:00
|
|
|
static CC_INLINE int
|
2007-04-05 12:09:32 +00:00
|
|
|
fwc_lookup(const uip_ipaddr_t *orig, const u32_t *id)
|
2007-04-04 12:12:21 +00:00
|
|
|
{
|
|
|
|
unsigned n = (orig->u8[2] + orig->u8[3]) % NFWCACHE;
|
|
|
|
return fwcache[n].id == *id && uip_ipaddr_cmp(&fwcache[n].orig, orig);
|
|
|
|
}
|
|
|
|
|
2007-04-18 21:26:09 +00:00
|
|
|
static CC_INLINE void
|
2007-04-05 12:09:32 +00:00
|
|
|
fwc_add(const uip_ipaddr_t *orig, const u32_t *id)
|
2007-04-04 12:12:21 +00:00
|
|
|
{
|
2007-05-20 20:08:28 +00:00
|
|
|
unsigned n = (orig->u8[2] + orig->u8[3]) % NFWCACHE;
|
2007-04-04 12:12:21 +00:00
|
|
|
fwcache[n].id = *id;
|
Support for z80(sdcc) port.
In order to support, some core modules are modified as follows:
core/sys/dsc.h
- If CTK_CONF_ICONS is diabled, the whole icon-related code is disabled.
- DSC_HEADER is changed to remove extra semicolon.
core/sys/process.h
- process_data_t is expressed by void* in signatures (known bug on sdcc).
core/sys/autostart.h
- autostart_processes is changed to remove extra semicolon.
core/sys/cc.h
- CC_CONF_ASSIGN_AGGREGATE is introduced.
- CC_CONF_INC_CAST_POINTER is introduced, a workaround of a kind
of sdcc bug for an increment.
core/net/hc.c
core/net/uip_arp.c
core/net/uaodv.c
- Aggregation assignments are changed to uip_ipaddr_copy.
core/net/psock.c
core/net/uipbuf.c
core/net/dhcpc.c
apps/shell/shell.c
core/ctk/vnc-server.c
core/ctk/vnc-out.c
- "register" keyword in a signature cannot be used in sdcc,
CC_REGISTER_ARG is used instead.
core/net/uip-over-mesh.c
- An extra semicolon is removed.
apps/dhcp/dhcp-dsc.c
apps/shell/shell-dsc.
apps/ftp/ftp-dsc.c
apps/process-list/process-list-dsc.c
apps/email/email-dsc.c
apps/webserver/webserver-dsc.c
apps/vnc/vnc-dsc.c
apps/vnc/vnc-viewer.h
apps/webbrowser/www-dsc.c
apps/about/about-dsc.c
apps/irc/irc-dsc.c
apps/telnet/telnet-dsc.c
apps/telnetd/telnetd-dsc.c
apps/netconf/netconf-dsc.c
apps/directory/directory-dsc.c
pps/calc/calc-dsc.c
- Modify an extern type to a real declaration, which is static
to prevent a compile error.
core/net/mac/xmac.c
- Variables cannot be defined in a head of block on sdcc.
core/ctk/ctk.h
core/ctk/ctk.c
apps/program-handler/program-handler.c
- If CTK_CONF_ICONS is diabled, the whole icon-related code is disabled.
Makefile.include
- Add a set of configuration for an assembler.
- $(CLEAN) variable is introduced for customized cleanup.
apps/process-list/process-list.c
- PROCESSLIST_CONF_HEIGHT is introduced to address smaller screen size.
core/lib/ctk-filedialog.c
- FILES_CONF_HEIGHT is introduced to address smaller screen size.
- "register" keyword in a signature cannot be used in sdcc,
CC_REGISTER_ARG is used instead.
apps/vnc/vnc-viewer.c
- A cast is added to prevent a compile error.
- "register" keyword in a signature cannot be used in sdcc,
CC_REGISTER_ARG is used instead.
apps/webbrowser/webclient.c
- CC_CONF_INC_CAST_POINTER is introduced, a workaround of a kind
of sdcc bug for an increment.
core/loader/elfloader.c
- A cast is added to prevent a compile error.
core/net/rime/rimeaddr.c
- An initialization is added to prevent a compile error.
core/net/rime/rudolph0.c
- NULL is changed to 0, because NULL causes a compile error.
core/net/rime/route-discovery.c
- Add an argument to match the definition of nf_callbacks.
cpu/z80/strcasecmp.h
cpu/z80/strcasecmp.c
cpu/z80/contiki-sdcc-conf.h
cpu/z80/mtarch.c
cpu/z80/mtarch.h
cpu/z80/Makefile.z80
- New files to make compilation availble on sdcc.
- Added support for multithreading.
2007-08-30 14:39:16 +00:00
|
|
|
uip_ipaddr_copy(&fwcache[n].orig, orig);
|
2007-04-04 12:12:21 +00:00
|
|
|
}
|
|
|
|
|
2007-05-07 11:51:20 +00:00
|
|
|
#ifdef NDEBUG
|
2007-02-01 14:33:05 +00:00
|
|
|
#define PRINTF(...) do {} while (0)
|
2006-06-17 22:41:10 +00:00
|
|
|
#define print_debug(...) do{}while(0)
|
|
|
|
#else
|
2007-02-01 14:33:05 +00:00
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
2007-04-18 21:26:09 +00:00
|
|
|
#ifdef __GNUC__
|
2007-02-01 14:33:05 +00:00
|
|
|
static void
|
|
|
|
print_debug(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
|
2007-04-18 21:26:09 +00:00
|
|
|
#endif /* __GNUC__ */
|
2006-06-17 22:41:10 +00:00
|
|
|
static void
|
|
|
|
print_debug(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2007-02-01 14:33:05 +00:00
|
|
|
printf("%d.%d.%d.%d: ", uip_ipaddr_to_quad(&uip_hostaddr));
|
2006-06-17 22:41:10 +00:00
|
|
|
vprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-04-30 09:49:32 +00:00
|
|
|
#define BUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
|
|
|
|
#define uip_udp_sender() (&BUF->srcipaddr)
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2007-04-04 12:12:21 +00:00
|
|
|
sendto(const uip_ipaddr_t *dest, const void *buf, int len)
|
2006-06-17 22:41:10 +00:00
|
|
|
{
|
|
|
|
/* XXX: this is a HACK! We're updating the uIP UDP connection
|
|
|
|
"unicastconn" so that the destination address is the next-hop,
|
|
|
|
and we're patching the "uip_udp_conn" variable so that it points
|
|
|
|
the this connection instead. THIS IS NOT A NICE WAY TO DO THIS,
|
|
|
|
but it is currently nicer than the alternative (requesting a new
|
|
|
|
poll, and remembering the state, etc.). */
|
|
|
|
|
|
|
|
uip_ipaddr_copy(&unicastconn->ripaddr, dest);
|
|
|
|
uip_udp_conn = unicastconn;
|
2007-04-30 15:26:09 +00:00
|
|
|
uip_udp_packet_send(unicastconn, buf, len);
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-06-28 15:06:56 +00:00
|
|
|
#ifdef AODV_BAD_HOP_EXTENSION
|
|
|
|
static unsigned
|
|
|
|
add_rreq_extensions(void *_p)
|
|
|
|
{
|
|
|
|
struct uaodv_bad_hop_ext *p = _p;
|
|
|
|
uip_ipaddr_t *a = p->addrs;
|
|
|
|
unsigned i, n;
|
|
|
|
|
|
|
|
#define SCALE_RETRANS_THRESHOLD (3*4)
|
|
|
|
|
|
|
|
cc2420_check_remote(0xffff); /* Age table. */
|
|
|
|
n = 0;
|
|
|
|
for (i = 0; i < NNEIGBOURS; i++) {
|
|
|
|
if (neigbours[i].nretrans >= SCALE_RETRANS_THRESHOLD
|
|
|
|
&& neigbours[i].mac != 0xffff) {
|
|
|
|
a->u16[0] = uip_hostaddr.u16[0];
|
|
|
|
a->u16[1] = neigbours[i].mac;
|
|
|
|
n++;
|
|
|
|
if(n == 15)
|
|
|
|
break; /* Avoid buffer overrun */
|
|
|
|
print_debug("BAD HOP %d.%d.%d.%d\t%d\n",
|
|
|
|
uip_ipaddr_to_quad(a), neigbours[i].nretrans);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(n == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
p->type = RREQ_BAD_HOP_EXT;
|
|
|
|
p->length = 2 + 4*n; /* Two unused bytes + addresses */
|
|
|
|
return 2 + p->length; /* Type + len + extension data */
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define add_rreq_extensions(p) 0 /* Don't add anything */
|
|
|
|
#endif
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
static void
|
|
|
|
send_rreq(uip_ipaddr_t *addr)
|
|
|
|
{
|
|
|
|
struct uaodv_msg_rreq *rm = (struct uaodv_msg_rreq *)uip_appdata;
|
2007-06-28 15:06:56 +00:00
|
|
|
int len;
|
|
|
|
|
2007-04-24 16:08:10 +00:00
|
|
|
print_debug("send RREQ for %d.%d.%d.%d\n", uip_ipaddr_to_quad(addr));
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
rm->type = UAODV_RREQ_TYPE;
|
2007-05-08 08:41:26 +00:00
|
|
|
rm->dest_seqno = last_known_seqno(addr);
|
2007-05-07 11:51:20 +00:00
|
|
|
if(rm->dest_seqno == 0)
|
|
|
|
rm->flags = UAODV_RREQ_UNKSEQNO;
|
|
|
|
else
|
|
|
|
rm->flags = 0;
|
2006-06-17 22:41:10 +00:00
|
|
|
rm->reserved = 0;
|
|
|
|
rm->hop_count = 0;
|
2007-04-04 12:12:21 +00:00
|
|
|
rm->rreq_id = htonl(rreq_id++);
|
2006-06-17 22:41:10 +00:00
|
|
|
uip_ipaddr_copy(&rm->dest_addr, addr);
|
2007-04-04 12:12:21 +00:00
|
|
|
uip_gethostaddr(&rm->orig_addr);
|
2007-05-09 13:54:41 +00:00
|
|
|
my_hseqno++; /* Always */
|
|
|
|
rm->orig_seqno = htonl(my_hseqno);
|
2007-05-31 10:10:26 +00:00
|
|
|
bcastconn->ttl = MY_NET_DIAMETER;
|
2007-06-28 15:06:56 +00:00
|
|
|
len = sizeof(struct uaodv_msg_rreq);
|
|
|
|
len += add_rreq_extensions(rm + 1);
|
|
|
|
uip_udp_packet_send(bcastconn, rm, len);
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2007-04-24 16:08:10 +00:00
|
|
|
send_rrep(uip_ipaddr_t *dest, uip_ipaddr_t *nexthop, uip_ipaddr_t *orig,
|
|
|
|
u32_t *seqno, unsigned hop_count)
|
2006-06-17 22:41:10 +00:00
|
|
|
{
|
|
|
|
struct uaodv_msg_rrep *rm = (struct uaodv_msg_rrep *)uip_appdata;
|
|
|
|
|
2007-04-24 16:08:10 +00:00
|
|
|
print_debug("send RREP orig=%d.%d.%d.%d hops=%d\n",
|
|
|
|
uip_ipaddr_to_quad(orig), hop_count);
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
rm->type = UAODV_RREP_TYPE;
|
|
|
|
rm->flags = 0;
|
2007-05-21 15:26:57 +00:00
|
|
|
rm->prefix_sz = 0; /* I.e a /32 route. */
|
2007-04-24 16:08:10 +00:00
|
|
|
rm->hop_count = hop_count;
|
2007-04-04 12:12:21 +00:00
|
|
|
uip_ipaddr_copy(&rm->orig_addr, orig);
|
2007-05-07 11:51:20 +00:00
|
|
|
rm->dest_seqno = *seqno;
|
2007-04-04 12:12:21 +00:00
|
|
|
uip_ipaddr_copy(&rm->dest_addr, dest);
|
|
|
|
rm->lifetime = HTONL(MY_ROUTE_TIMEOUT);
|
|
|
|
sendto(nexthop, rm, sizeof(struct uaodv_msg_rrep));
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2007-04-04 12:12:21 +00:00
|
|
|
send_rerr(uip_ipaddr_t *addr, u32_t *seqno)
|
2006-06-17 22:41:10 +00:00
|
|
|
{
|
|
|
|
struct uaodv_msg_rerr *rm = (struct uaodv_msg_rerr *)uip_appdata;
|
|
|
|
|
2007-11-17 18:01:00 +00:00
|
|
|
print_debug("send RERR for %d.%d.%d.%d\n", uip_ipaddr_to_quad(addr));
|
2007-04-30 15:26:09 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
rm->type = UAODV_RERR_TYPE;
|
|
|
|
rm->reserved = 0;
|
|
|
|
rm->dest_count = 1;
|
|
|
|
uip_ipaddr_copy(&rm->unreach[0].addr, addr);
|
2007-04-04 12:12:21 +00:00
|
|
|
rm->unreach[0].seqno = *seqno;
|
2007-05-08 13:33:57 +00:00
|
|
|
if(*seqno == 0)
|
|
|
|
rm->flags = UAODV_RERR_UNKNOWN;
|
|
|
|
else
|
|
|
|
rm->flags = 0;
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-04-30 15:26:09 +00:00
|
|
|
uip_udp_packet_send(bcastconn, rm, sizeof(struct uaodv_msg_rerr));
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
handle_incoming_rreq(void)
|
|
|
|
{
|
|
|
|
struct uaodv_msg_rreq *rm = (struct uaodv_msg_rreq *)uip_appdata;
|
2007-04-04 12:12:21 +00:00
|
|
|
uip_ipaddr_t dest_addr, orig_addr;
|
2007-04-05 12:09:32 +00:00
|
|
|
struct uaodv_rt_entry *rt, *fw = NULL;
|
2007-04-30 15:26:09 +00:00
|
|
|
|
|
|
|
print_debug("RREQ %d.%d.%d.%d -> %d.%d.%d.%d ttl=%u"
|
|
|
|
" orig=%d.%d.%d.%d seq=%lu hops=%u dest=%d.%d.%d.%d seq=%lu\n",
|
|
|
|
uip_ipaddr_to_quad(&BUF->srcipaddr),
|
|
|
|
uip_ipaddr_to_quad(&BUF->destipaddr),
|
|
|
|
BUF->ttl,
|
|
|
|
uip_ipaddr_to_quad(&rm->orig_addr), ntohl(rm->orig_seqno),
|
|
|
|
rm->hop_count,
|
|
|
|
uip_ipaddr_to_quad(&rm->dest_addr), ntohl(rm->dest_seqno));
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-05-07 11:51:20 +00:00
|
|
|
if(uip_ipaddr_cmp(&rm->orig_addr, &uip_hostaddr)) {
|
|
|
|
return; /* RREQ looped back! */
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
2007-04-04 12:12:21 +00:00
|
|
|
|
2007-05-07 11:51:20 +00:00
|
|
|
#ifdef CC2420_RADIO
|
|
|
|
{
|
|
|
|
int ret = cc2420_check_remote(uip_udp_sender()->u16[1]);
|
|
|
|
|
|
|
|
if(ret == REMOTE_YES) {
|
|
|
|
print_debug("RREQ drop is remote\n");
|
|
|
|
return;
|
|
|
|
} else if (ret == REMOTE_NO) {
|
|
|
|
/* Is neigbour, accept it. */
|
2007-05-21 15:26:57 +00:00
|
|
|
} else if(cc2420_last_rssi < RSSI_THRESHOLD) {
|
2007-05-09 13:07:55 +00:00
|
|
|
print_debug("RREQ drop %d %d\n", cc2420_last_rssi,
|
|
|
|
cc2420_last_correlation);
|
2007-05-07 11:51:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2006-06-17 22:41:10 +00:00
|
|
|
#endif
|
|
|
|
|
2007-06-28 15:06:56 +00:00
|
|
|
#ifdef AODV_BAD_HOP_EXTENSION
|
|
|
|
if(uip_len > (sizeof(*rm) + 2)) {
|
|
|
|
struct uaodv_bad_hop_ext *ext = (void *)(uip_appdata + sizeof(*rm));
|
|
|
|
u8_t *end = uip_appdata + uip_len;
|
|
|
|
for(;
|
|
|
|
(u8_t *)ext < end;
|
|
|
|
ext = (void *)((u8_t *)ext + ext->length + 2)) {
|
|
|
|
u8_t *eend = (u8_t *)ext + ext->length;
|
|
|
|
if(eend > end)
|
|
|
|
eend = end;
|
|
|
|
|
|
|
|
if(ext->type == RREQ_BAD_HOP_EXT) {
|
|
|
|
uip_ipaddr_t *a;
|
|
|
|
for(a = ext->addrs; (u8_t *)a < eend; a++) {
|
|
|
|
if(uip_ipaddr_cmp(a, &uip_hostaddr)) {
|
|
|
|
print_debug("BAD_HOP drop\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* AODV_BAD_HOP_EXTENSION */
|
|
|
|
|
2007-04-04 12:12:21 +00:00
|
|
|
/* New reverse route? */
|
|
|
|
rt = uaodv_rt_lookup(&rm->orig_addr);
|
|
|
|
if(rt == NULL
|
2007-05-09 13:54:41 +00:00
|
|
|
|| (SCMP32(ntohl(rm->orig_seqno), rt->hseqno) > 0) /* New route. */
|
|
|
|
|| (SCMP32(ntohl(rm->orig_seqno), rt->hseqno) == 0
|
2007-04-04 12:12:21 +00:00
|
|
|
&& rm->hop_count < rt->hop_count)) { /* Better route. */
|
2007-04-30 15:26:09 +00:00
|
|
|
print_debug("Inserting1\n");
|
2007-04-04 12:12:21 +00:00
|
|
|
rt = uaodv_rt_add(&rm->orig_addr, uip_udp_sender(),
|
2007-05-09 13:54:41 +00:00
|
|
|
rm->hop_count, &rm->orig_seqno);
|
2007-04-04 12:12:21 +00:00
|
|
|
}
|
|
|
|
|
2007-05-07 11:51:20 +00:00
|
|
|
/* Check if it is for our address or a fresh route. */
|
2007-05-09 14:53:54 +00:00
|
|
|
if(uip_ipaddr_cmp(&rm->dest_addr, &uip_hostaddr)
|
|
|
|
|| rm->flags & UAODV_RREQ_DESTONLY) {
|
2007-05-07 11:51:20 +00:00
|
|
|
fw = NULL;
|
|
|
|
} else {
|
|
|
|
fw = uaodv_rt_lookup(&rm->dest_addr);
|
|
|
|
if(!(rm->flags & UAODV_RREQ_UNKSEQNO)
|
|
|
|
&& fw != NULL
|
2007-05-09 14:53:54 +00:00
|
|
|
&& SCMP32(fw->hseqno, ntohl(rm->dest_seqno)) <= 0) {
|
2007-05-07 11:51:20 +00:00
|
|
|
fw = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fw != NULL) {
|
|
|
|
u32_t net_seqno;
|
|
|
|
|
|
|
|
print_debug("RREQ for known route\n");
|
2007-04-04 12:12:21 +00:00
|
|
|
uip_ipaddr_copy(&dest_addr, &rm->dest_addr);
|
|
|
|
uip_ipaddr_copy(&orig_addr, &rm->orig_addr);
|
2007-05-09 13:54:41 +00:00
|
|
|
net_seqno = htonl(fw->hseqno);
|
2007-05-07 11:51:20 +00:00
|
|
|
send_rrep(&dest_addr, &rt->nexthop, &orig_addr, &net_seqno,
|
|
|
|
fw->hop_count + 1);
|
|
|
|
} else if(uip_ipaddr_cmp(&rm->dest_addr, &uip_hostaddr)) {
|
|
|
|
u32_t net_seqno;
|
|
|
|
|
|
|
|
print_debug("RREQ for our address\n");
|
|
|
|
uip_ipaddr_copy(&dest_addr, &rm->dest_addr);
|
|
|
|
uip_ipaddr_copy(&orig_addr, &rm->orig_addr);
|
|
|
|
|
2007-05-31 10:10:26 +00:00
|
|
|
my_hseqno++;
|
2007-05-07 11:51:20 +00:00
|
|
|
if(!(rm->flags & UAODV_RREQ_UNKSEQNO)
|
2007-11-17 18:01:00 +00:00
|
|
|
&& SCMP32(my_hseqno, ntohl(rm->dest_seqno)) < 0) {
|
2007-05-09 13:54:41 +00:00
|
|
|
print_debug("New my_hseqno %lu\n", my_hseqno); /* We have rebooted. */
|
|
|
|
my_hseqno = ntohl(rm->dest_seqno) + 1;
|
2007-04-05 12:09:32 +00:00
|
|
|
}
|
2007-05-09 13:54:41 +00:00
|
|
|
net_seqno = htonl(my_hseqno);
|
2007-05-07 11:51:20 +00:00
|
|
|
send_rrep(&dest_addr, &rt->nexthop, &orig_addr, &net_seqno, 0);
|
2007-05-31 10:10:26 +00:00
|
|
|
} else if(BUF->ttl > 1) {
|
2007-06-28 15:06:56 +00:00
|
|
|
int len;
|
|
|
|
|
2007-05-07 11:51:20 +00:00
|
|
|
/* Have we seen this RREQ before? */
|
|
|
|
if(fwc_lookup(&rm->orig_addr, &rm->rreq_id)) {
|
|
|
|
print_debug("RREQ cached, not fwd\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fwc_add(&rm->orig_addr, &rm->rreq_id);
|
|
|
|
|
2007-04-30 15:26:09 +00:00
|
|
|
print_debug("RREQ fwd\n");
|
2006-06-17 22:41:10 +00:00
|
|
|
rm->hop_count++;
|
2007-05-31 10:10:26 +00:00
|
|
|
bcastconn->ttl = BUF->ttl - 1;
|
2007-06-28 15:06:56 +00:00
|
|
|
len = sizeof(struct uaodv_msg_rreq);
|
|
|
|
len += add_rreq_extensions(rm + 1);
|
|
|
|
uip_udp_packet_send(bcastconn, rm, len);
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
handle_incoming_rrep(void)
|
|
|
|
{
|
|
|
|
struct uaodv_msg_rrep *rm = (struct uaodv_msg_rrep *)uip_appdata;
|
|
|
|
struct uaodv_rt_entry *rt;
|
2007-04-04 12:12:21 +00:00
|
|
|
|
2007-05-31 10:10:26 +00:00
|
|
|
/* Useless HELLO message? */
|
2007-05-21 15:26:57 +00:00
|
|
|
if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr)) {
|
2007-06-01 14:49:54 +00:00
|
|
|
#ifdef AODV_RESPOND_TO_HELLOS
|
|
|
|
u32_t net_seqno;
|
|
|
|
#ifdef CC2420_RADIO
|
|
|
|
int ret = cc2420_check_remote(uip_udp_sender()->u16[1]);
|
|
|
|
|
|
|
|
if(ret == REMOTE_YES) {
|
|
|
|
print_debug("HELLO drop is remote\n");
|
|
|
|
return;
|
|
|
|
} else if (ret == REMOTE_NO) {
|
|
|
|
/* Is neigbour, accept it. */
|
|
|
|
} else if(cc2420_last_rssi < RSSI_THRESHOLD) {
|
|
|
|
print_debug("HELLO drop %d %d\n", cc2420_last_rssi, cc2420_last_correlation);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* Sometimes it helps to send a non-requested RREP in response! */
|
|
|
|
net_seqno = htonl(my_hseqno);
|
2007-05-21 15:26:57 +00:00
|
|
|
send_rrep(&uip_hostaddr, &BUF->srcipaddr, &BUF->srcipaddr, &net_seqno, 0);
|
2007-05-31 10:10:26 +00:00
|
|
|
#endif
|
2007-05-21 15:26:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-04-30 15:26:09 +00:00
|
|
|
print_debug("RREP %d.%d.%d.%d -> %d.%d.%d.%d"
|
|
|
|
" dest=%d.%d.%d.%d seq=%lu hops=%u orig=%d.%d.%d.%d\n",
|
|
|
|
uip_ipaddr_to_quad(&BUF->srcipaddr),
|
|
|
|
uip_ipaddr_to_quad(&BUF->destipaddr),
|
|
|
|
uip_ipaddr_to_quad(&rm->dest_addr), ntohl(rm->dest_seqno),
|
|
|
|
rm->hop_count,
|
|
|
|
uip_ipaddr_to_quad(&rm->orig_addr));
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
rt = uaodv_rt_lookup(&rm->dest_addr);
|
2007-02-01 14:33:05 +00:00
|
|
|
|
2007-04-04 12:12:21 +00:00
|
|
|
/* New forward route? */
|
2007-05-09 13:54:41 +00:00
|
|
|
if(rt == NULL || (SCMP32(ntohl(rm->dest_seqno), rt->hseqno) > 0)) {
|
2007-04-30 15:26:09 +00:00
|
|
|
print_debug("Inserting3\n");
|
2007-04-04 12:12:21 +00:00
|
|
|
rt = uaodv_rt_add(&rm->dest_addr, uip_udp_sender(),
|
2007-05-09 13:54:41 +00:00
|
|
|
rm->hop_count, &rm->dest_seqno);
|
2007-01-24 16:32:34 +00:00
|
|
|
#ifdef CC2420_RADIO
|
2006-06-17 22:41:10 +00:00
|
|
|
/* This link is ok since he is unicasting back to us! */
|
|
|
|
cc2420_recv_ok(uip_udp_sender());
|
2007-04-30 15:26:09 +00:00
|
|
|
print_debug("RREP recv ok %d %d\n",
|
|
|
|
cc2420_last_rssi, cc2420_last_correlation);
|
2006-06-17 22:41:10 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
2007-04-30 15:26:09 +00:00
|
|
|
print_debug("Not inserting\n");
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
2007-04-04 12:12:21 +00:00
|
|
|
|
|
|
|
/* Forward RREP towards originator? */
|
|
|
|
if(uip_ipaddr_cmp(&rm->orig_addr, &uip_hostaddr)) {
|
2007-06-05 10:04:20 +00:00
|
|
|
print_debug("ROUTE FOUND\n");
|
|
|
|
if(rm->flags & UAODV_RREP_ACK) {
|
|
|
|
struct uaodv_msg_rrep_ack *ack = (void *)uip_appdata;
|
|
|
|
ack->type = UAODV_RREP_ACK_TYPE;
|
|
|
|
ack->reserved = 0;
|
|
|
|
sendto(uip_udp_sender(), ack, sizeof(*ack));
|
|
|
|
}
|
2006-06-17 22:41:10 +00:00
|
|
|
} else {
|
2007-04-04 12:12:21 +00:00
|
|
|
rt = uaodv_rt_lookup(&rm->orig_addr);
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
if(rt == NULL) {
|
|
|
|
print_debug("RREP received, but no route back to originator... :-( \n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-05 10:04:20 +00:00
|
|
|
if(rm->flags & UAODV_RREP_ACK) {
|
|
|
|
print_debug("RREP with ACK request (ignored)!\n");
|
|
|
|
/* Don't want any RREP-ACKs in return! */
|
|
|
|
rm->flags &= ~UAODV_RREP_ACK;
|
|
|
|
}
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
rm->hop_count++;
|
|
|
|
|
2007-04-04 12:12:21 +00:00
|
|
|
print_debug("Fwd RREP to %d.%d.%d.%d\n", uip_ipaddr_to_quad(&rt->nexthop));
|
2007-02-01 14:33:05 +00:00
|
|
|
|
2007-04-04 12:12:21 +00:00
|
|
|
sendto(&rt->nexthop, rm, sizeof(struct uaodv_msg_rrep));
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
handle_incoming_rerr(void)
|
|
|
|
{
|
|
|
|
struct uaodv_msg_rerr *rm = (struct uaodv_msg_rerr *)uip_appdata;
|
|
|
|
struct uaodv_rt_entry *rt;
|
|
|
|
|
2007-04-30 15:26:09 +00:00
|
|
|
print_debug("RERR %d.%d.%d.%d -> %d.%d.%d.%d"
|
|
|
|
" unreach=%d.%d.%d.%d seq=%lu\n",
|
|
|
|
uip_ipaddr_to_quad(&BUF->srcipaddr),
|
|
|
|
uip_ipaddr_to_quad(&BUF->destipaddr),
|
|
|
|
uip_ipaddr_to_quad((uip_ipaddr_t *)&rm->unreach[0]),
|
2007-04-04 12:12:21 +00:00
|
|
|
ntohl(rm->unreach[0].seqno));
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-05-08 08:41:26 +00:00
|
|
|
if(uip_ipaddr_cmp(&rm->unreach[0].addr, &uip_hostaddr))
|
|
|
|
return;
|
|
|
|
|
2007-05-09 16:45:03 +00:00
|
|
|
rt = uaodv_rt_lookup_any(&rm->unreach[0].addr);
|
2007-05-08 13:33:57 +00:00
|
|
|
if(rt != NULL && uip_ipaddr_cmp(&rt->nexthop, uip_udp_sender())) {
|
2007-06-28 15:16:41 +00:00
|
|
|
if((rm->flags & UAODV_RERR_UNKNOWN) || rm->unreach[0].seqno == 0
|
2007-05-09 13:54:41 +00:00
|
|
|
|| SCMP32(rt->hseqno, ntohl(rm->unreach[0].seqno)) <= 0) {
|
2007-05-08 13:33:57 +00:00
|
|
|
rt->is_bad = 1;
|
2007-05-31 10:10:26 +00:00
|
|
|
if(rm->flags & UAODV_RERR_UNKNOWN) {
|
|
|
|
rm->flags &= ~UAODV_RERR_UNKNOWN;
|
|
|
|
rm->unreach[0].seqno = htonl(rt->hseqno);
|
|
|
|
}
|
2007-05-08 13:33:57 +00:00
|
|
|
print_debug("RERR rebroadcast\n");
|
|
|
|
uip_udp_packet_send(bcastconn, rm, sizeof(struct uaodv_msg_rerr));
|
|
|
|
}
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
handle_incoming_packet(void)
|
|
|
|
{
|
|
|
|
struct uaodv_msg *m = (struct uaodv_msg *)uip_appdata;
|
|
|
|
|
|
|
|
/* print_debug("New UDP data, AODV packet type %d\n", m->type);*/
|
|
|
|
switch(m->type) {
|
|
|
|
case UAODV_RREQ_TYPE:
|
|
|
|
handle_incoming_rreq();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UAODV_RREP_TYPE:
|
|
|
|
handle_incoming_rrep();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UAODV_RERR_TYPE:
|
|
|
|
handle_incoming_rerr();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static enum {
|
|
|
|
COMMAND_NONE,
|
|
|
|
COMMAND_SEND_RREQ,
|
|
|
|
COMMAND_SEND_RERR,
|
|
|
|
} command;
|
|
|
|
|
2007-04-05 12:09:32 +00:00
|
|
|
static uip_ipaddr_t bad_dest;
|
|
|
|
static u32_t bad_seqno; /* In network byte order! */
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
void
|
2007-05-08 13:33:57 +00:00
|
|
|
uaodv_bad_dest(uip_ipaddr_t *dest)
|
2006-06-17 22:41:10 +00:00
|
|
|
{
|
2007-05-09 16:45:03 +00:00
|
|
|
struct uaodv_rt_entry *rt = uaodv_rt_lookup_any(dest);
|
2007-05-08 13:33:57 +00:00
|
|
|
|
2007-05-08 08:41:26 +00:00
|
|
|
if(rt == NULL)
|
2007-05-08 13:33:57 +00:00
|
|
|
bad_seqno = 0; /* Or flag this in RERR? */
|
|
|
|
else {
|
|
|
|
rt->is_bad = 1;
|
2007-05-09 13:54:41 +00:00
|
|
|
bad_seqno = htonl(rt->hseqno);
|
2007-05-08 13:33:57 +00:00
|
|
|
}
|
2007-05-08 08:41:26 +00:00
|
|
|
|
2007-05-08 13:33:57 +00:00
|
|
|
uip_ipaddr_copy(&bad_dest, dest);
|
2006-06-17 22:41:10 +00:00
|
|
|
command = COMMAND_SEND_RERR;
|
|
|
|
process_post(&uaodv_process, PROCESS_EVENT_MSG, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uip_ipaddr_t rreq_addr;
|
|
|
|
static struct timer next_time;
|
|
|
|
|
|
|
|
struct uaodv_rt_entry *
|
|
|
|
uaodv_request_route_to(uip_ipaddr_t *host)
|
|
|
|
{
|
|
|
|
struct uaodv_rt_entry *route = uaodv_rt_lookup(host);
|
|
|
|
|
|
|
|
if(route != NULL) {
|
2007-04-04 12:12:21 +00:00
|
|
|
uaodv_rt_lru(route);
|
2006-06-17 22:41:10 +00:00
|
|
|
return route;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Broadcast protocols must be rate-limited!
|
|
|
|
*/
|
|
|
|
if(!timer_expired(&next_time)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(command != COMMAND_NONE) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
uip_ipaddr_copy(&rreq_addr, host);
|
|
|
|
command = COMMAND_SEND_RREQ;
|
|
|
|
process_post(&uaodv_process, PROCESS_EVENT_MSG, NULL);
|
2007-04-05 12:30:17 +00:00
|
|
|
timer_set(&next_time, CLOCK_SECOND/8); /* Max 10/s per RFC3561. */
|
2006-06-17 22:41:10 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_THREAD(uaodv_process, ev, data)
|
|
|
|
{
|
|
|
|
PROCESS_EXITHANDLER(goto exit);
|
|
|
|
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
2007-11-18 11:21:29 +00:00
|
|
|
printf("uaodv_process starting %lu\n", (unsigned long) my_hseqno);
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-04-30 15:26:09 +00:00
|
|
|
bcastconn = udp_broadcast_new(HTONS(UAODV_UDPPORT), NULL);
|
2006-06-17 22:41:10 +00:00
|
|
|
unicastconn = udp_broadcast_new(HTONS(UAODV_UDPPORT), NULL);
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
PROCESS_WAIT_EVENT();
|
|
|
|
|
|
|
|
if(ev == tcpip_event) {
|
|
|
|
if(uip_newdata()) {
|
|
|
|
handle_incoming_packet();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(uip_poll()) {
|
2007-04-05 12:30:17 +00:00
|
|
|
if(command == COMMAND_SEND_RREQ) {
|
|
|
|
if(uaodv_rt_lookup(&rreq_addr) == NULL)
|
|
|
|
send_rreq(&rreq_addr);
|
|
|
|
} else if (command == COMMAND_SEND_RERR) {
|
2007-04-05 12:09:32 +00:00
|
|
|
send_rerr(&bad_dest, &bad_seqno);
|
2007-04-05 12:30:17 +00:00
|
|
|
}
|
2006-06-17 22:41:10 +00:00
|
|
|
command = COMMAND_NONE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ev == PROCESS_EVENT_MSG) {
|
2007-04-30 15:26:09 +00:00
|
|
|
tcpip_poll_udp(bcastconn);
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
command = COMMAND_NONE;
|
|
|
|
uaodv_rt_flush_all();
|
2007-04-30 15:26:09 +00:00
|
|
|
uip_udp_remove(bcastconn);
|
|
|
|
bcastconn = NULL;
|
2006-06-17 22:41:10 +00:00
|
|
|
uip_udp_remove(unicastconn);
|
|
|
|
unicastconn = NULL;
|
|
|
|
printf("uaodv_process exiting\n");
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|