2012-11-26 19:53:38 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012, Thingsquare, http://www.thingsquare.com/.
|
|
|
|
* 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 copyright holder 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 COPYRIGHT HOLDERS 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
|
|
|
|
* COPYRIGHT HOLDER 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.
|
|
|
|
*
|
|
|
|
*/
|
2015-02-15 19:02:07 +01:00
|
|
|
/**
|
|
|
|
* \addtogroup uip6
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Routing table manipulation
|
|
|
|
*/
|
2013-11-22 09:17:54 +01:00
|
|
|
#include "net/ipv6/uip-ds6.h"
|
|
|
|
#include "net/ip/uip.h"
|
2012-11-26 19:53:38 +01:00
|
|
|
|
|
|
|
#include "lib/list.h"
|
|
|
|
#include "lib/memb.h"
|
2013-07-29 18:49:21 +02:00
|
|
|
#include "net/nbr-table.h"
|
2012-11-26 19:53:38 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-09-21 18:32:15 +02:00
|
|
|
/* A configurable function called after adding a new neighbor as next hop */
|
|
|
|
#ifdef NETSTACK_CONF_ROUTING_NEIGHBOR_ADDED_CALLBACK
|
|
|
|
void NETSTACK_CONF_ROUTING_NEIGHBOR_ADDED_CALLBACK(const linkaddr_t *addr);
|
|
|
|
#endif /* NETSTACK_CONF_ROUTING_NEIGHBOR_ADDED_CALLBACK */
|
|
|
|
|
|
|
|
/* A configurable function called after removing a next hop neighbor */
|
|
|
|
#ifdef NETSTACK_CONF_ROUTING_NEIGHBOR_REMOVED_CALLBACK
|
|
|
|
void NETSTACK_CONF_ROUTING_NEIGHBOR_REMOVED_CALLBACK(const linkaddr_t *addr);
|
|
|
|
#endif /* NETSTACK_CONF_ROUTING_NEIGHBOR_REMOVED_CALLBACK */
|
|
|
|
|
2013-08-12 00:22:21 +02:00
|
|
|
/* The nbr_routes holds a neighbor table to be able to maintain
|
|
|
|
information about what routes go through what neighbor. This
|
|
|
|
neighbor table is registered with the central nbr-table repository
|
|
|
|
so that it will be maintained along with the rest of the neighbor
|
|
|
|
tables in the system. */
|
2015-09-21 18:32:15 +02:00
|
|
|
NBR_TABLE_GLOBAL(struct uip_ds6_route_neighbor_routes, nbr_routes);
|
2013-11-22 15:28:46 +01:00
|
|
|
MEMB(neighborroutememb, struct uip_ds6_route_neighbor_route, UIP_DS6_ROUTE_NB);
|
2013-08-12 00:22:21 +02:00
|
|
|
|
|
|
|
/* Each route is repressented by a uip_ds6_route_t structure and
|
|
|
|
memory for each route is allocated from the routememb memory
|
2013-11-22 15:28:46 +01:00
|
|
|
block. These routes are maintained on the routelist. */
|
|
|
|
LIST(routelist);
|
2012-11-26 19:53:38 +01:00
|
|
|
MEMB(routememb, uip_ds6_route_t, UIP_DS6_ROUTE_NB);
|
|
|
|
|
2013-08-12 00:22:21 +02:00
|
|
|
/* Default routes are held on the defaultrouterlist and their
|
|
|
|
structures are allocated from the defaultroutermemb memory block.*/
|
2012-11-26 19:53:38 +01:00
|
|
|
LIST(defaultrouterlist);
|
|
|
|
MEMB(defaultroutermemb, uip_ds6_defrt_t, UIP_DS6_DEFRT_NB);
|
|
|
|
|
2013-05-15 12:11:56 +02:00
|
|
|
#if UIP_DS6_NOTIFICATIONS
|
2012-11-26 19:53:38 +01:00
|
|
|
LIST(notificationlist);
|
2013-05-15 12:11:56 +02:00
|
|
|
#endif
|
2012-11-26 19:53:38 +01:00
|
|
|
|
2013-07-03 19:45:56 +02:00
|
|
|
static int num_routes = 0;
|
|
|
|
|
2013-05-15 12:11:56 +02:00
|
|
|
#undef DEBUG
|
2012-11-26 19:53:38 +01:00
|
|
|
#define DEBUG DEBUG_NONE
|
2013-11-22 09:17:54 +01:00
|
|
|
#include "net/ip/uip-debug.h"
|
2012-11-26 19:53:38 +01:00
|
|
|
|
2013-08-12 00:22:21 +02:00
|
|
|
static void rm_routelist_callback(nbr_table_item_t *ptr);
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#if DEBUG != DEBUG_NONE
|
|
|
|
static void
|
|
|
|
assert_nbr_routes_list_sane(void)
|
|
|
|
{
|
|
|
|
uip_ds6_route_t *r;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
/* Check if the route list has an infinite loop. */
|
|
|
|
for(r = uip_ds6_route_head(),
|
|
|
|
count = 0;
|
|
|
|
r != NULL &&
|
2013-11-22 15:28:46 +01:00
|
|
|
count < UIP_DS6_ROUTE_NB * 2;
|
2013-08-12 00:22:21 +02:00
|
|
|
r = uip_ds6_route_next(r),
|
|
|
|
count++);
|
|
|
|
|
2013-11-22 15:28:46 +01:00
|
|
|
if(count > UIP_DS6_ROUTE_NB) {
|
2013-08-12 00:22:21 +02:00
|
|
|
printf("uip-ds6-route.c: assert_nbr_routes_list_sane route list is in infinite loop\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the route list has as many entries as the
|
|
|
|
num_routes vairable. */
|
|
|
|
if(count < num_routes) {
|
|
|
|
printf("uip-ds6-route.c: assert_nbr_routes_list_sane too few entries on route list: should be %d, is %d, max %d\n",
|
|
|
|
num_routes, count, UIP_CONF_MAX_ROUTES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* DEBUG != DEBUG_NONE */
|
2012-11-26 19:53:38 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-05-15 12:11:56 +02:00
|
|
|
#if UIP_DS6_NOTIFICATIONS
|
2012-11-26 19:53:38 +01:00
|
|
|
static void
|
|
|
|
call_route_callback(int event, uip_ipaddr_t *route,
|
|
|
|
uip_ipaddr_t *nexthop)
|
|
|
|
{
|
|
|
|
int num;
|
|
|
|
struct uip_ds6_notification *n;
|
|
|
|
for(n = list_head(notificationlist);
|
|
|
|
n != NULL;
|
|
|
|
n = list_item_next(n)) {
|
|
|
|
if(event == UIP_DS6_NOTIFICATION_DEFRT_ADD ||
|
|
|
|
event == UIP_DS6_NOTIFICATION_DEFRT_RM) {
|
|
|
|
num = list_length(defaultrouterlist);
|
|
|
|
} else {
|
2013-07-03 19:45:56 +02:00
|
|
|
num = num_routes;
|
2012-11-26 19:53:38 +01:00
|
|
|
}
|
|
|
|
n->callback(event, route, nexthop, num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
uip_ds6_notification_add(struct uip_ds6_notification *n,
|
|
|
|
uip_ds6_notification_callback c)
|
|
|
|
{
|
|
|
|
if(n != NULL && c != NULL) {
|
|
|
|
n->callback = c;
|
|
|
|
list_add(notificationlist, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
uip_ds6_notification_rm(struct uip_ds6_notification *n)
|
|
|
|
{
|
|
|
|
list_remove(notificationlist, n);
|
|
|
|
}
|
2013-05-15 12:11:56 +02:00
|
|
|
#endif
|
2012-11-26 19:53:38 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
uip_ds6_route_init(void)
|
|
|
|
{
|
|
|
|
memb_init(&routememb);
|
2013-11-22 15:28:46 +01:00
|
|
|
list_init(routelist);
|
2013-08-12 00:22:21 +02:00
|
|
|
nbr_table_register(nbr_routes,
|
|
|
|
(nbr_table_callback *)rm_routelist_callback);
|
2012-11-26 19:53:38 +01:00
|
|
|
|
|
|
|
memb_init(&defaultroutermemb);
|
|
|
|
list_init(defaultrouterlist);
|
|
|
|
|
2013-05-15 12:11:56 +02:00
|
|
|
#if UIP_DS6_NOTIFICATIONS
|
2012-11-26 19:53:38 +01:00
|
|
|
list_init(notificationlist);
|
2013-05-15 12:11:56 +02:00
|
|
|
#endif
|
2012-11-26 19:53:38 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-07-03 19:45:56 +02:00
|
|
|
static uip_lladdr_t *
|
|
|
|
uip_ds6_route_nexthop_lladdr(uip_ds6_route_t *route)
|
|
|
|
{
|
|
|
|
if(route != NULL) {
|
2013-11-22 15:28:46 +01:00
|
|
|
return (uip_lladdr_t *)nbr_table_get_lladdr(nbr_routes,
|
|
|
|
route->neighbor_routes);
|
2013-07-03 19:45:56 +02:00
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uip_ipaddr_t *
|
|
|
|
uip_ds6_route_nexthop(uip_ds6_route_t *route)
|
|
|
|
{
|
|
|
|
if(route != NULL) {
|
|
|
|
return uip_ds6_nbr_ipaddr_from_lladdr(uip_ds6_route_nexthop_lladdr(route));
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uip_ds6_route_t *
|
|
|
|
uip_ds6_route_head(void)
|
|
|
|
{
|
2013-11-22 15:28:46 +01:00
|
|
|
return list_head(routelist);
|
2013-07-03 19:45:56 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-11-26 19:53:38 +01:00
|
|
|
uip_ds6_route_t *
|
2013-07-03 19:45:56 +02:00
|
|
|
uip_ds6_route_next(uip_ds6_route_t *r)
|
2012-11-26 19:53:38 +01:00
|
|
|
{
|
2013-07-03 19:45:56 +02:00
|
|
|
if(r != NULL) {
|
|
|
|
uip_ds6_route_t *n = list_item_next(r);
|
2013-11-22 15:28:46 +01:00
|
|
|
return n;
|
2013-07-03 19:45:56 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
2012-11-26 19:53:38 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2015-08-20 17:00:47 +02:00
|
|
|
uip_ds6_route_is_nexthop(const uip_lladdr_t *lladdr)
|
|
|
|
{
|
|
|
|
return nbr_table_get_from_lladdr(nbr_routes, (linkaddr_t *)lladdr) != NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2012-11-26 19:53:38 +01:00
|
|
|
uip_ds6_route_num_routes(void)
|
|
|
|
{
|
2013-07-03 19:45:56 +02:00
|
|
|
return num_routes;
|
2012-11-26 19:53:38 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uip_ds6_route_t *
|
|
|
|
uip_ds6_route_lookup(uip_ipaddr_t *addr)
|
|
|
|
{
|
|
|
|
uip_ds6_route_t *r;
|
|
|
|
uip_ds6_route_t *found_route;
|
|
|
|
uint8_t longestmatch;
|
|
|
|
|
|
|
|
PRINTF("uip-ds6-route: Looking up route for ");
|
|
|
|
PRINT6ADDR(addr);
|
|
|
|
PRINTF("\n");
|
|
|
|
|
|
|
|
|
|
|
|
found_route = NULL;
|
|
|
|
longestmatch = 0;
|
2013-07-03 19:45:56 +02:00
|
|
|
for(r = uip_ds6_route_head();
|
2012-11-26 19:53:38 +01:00
|
|
|
r != NULL;
|
2013-07-03 19:45:56 +02:00
|
|
|
r = uip_ds6_route_next(r)) {
|
2012-11-26 19:53:38 +01:00
|
|
|
if(r->length >= longestmatch &&
|
|
|
|
uip_ipaddr_prefixcmp(addr, &r->ipaddr, r->length)) {
|
|
|
|
longestmatch = r->length;
|
|
|
|
found_route = r;
|
2014-05-30 23:14:13 +02:00
|
|
|
/* check if total match - e.g. all 128 bits do match */
|
|
|
|
if(longestmatch == 128) {
|
|
|
|
break;
|
|
|
|
}
|
2012-11-26 19:53:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(found_route != NULL) {
|
2013-07-03 19:45:56 +02:00
|
|
|
PRINTF("uip-ds6-route: Found route: ");
|
2012-11-26 19:53:38 +01:00
|
|
|
PRINT6ADDR(addr);
|
|
|
|
PRINTF(" via ");
|
2013-07-03 19:45:56 +02:00
|
|
|
PRINT6ADDR(uip_ds6_route_nexthop(found_route));
|
2012-11-26 19:53:38 +01:00
|
|
|
PRINTF("\n");
|
|
|
|
} else {
|
|
|
|
PRINTF("uip-ds6-route: No route found\n");
|
|
|
|
}
|
|
|
|
|
2014-05-30 23:14:13 +02:00
|
|
|
if(found_route != NULL && found_route != list_head(routelist)) {
|
|
|
|
/* If we found a route, we put it at the start of the routeslist
|
2013-11-22 15:28:46 +01:00
|
|
|
list. The list is ordered by how recently we looked them up:
|
2014-05-30 23:14:13 +02:00
|
|
|
the least recently used route will be at the end of the
|
|
|
|
list - for fast lookups (assuming multiple packets to the same node). */
|
|
|
|
|
2013-11-22 15:28:46 +01:00
|
|
|
list_remove(routelist, found_route);
|
2014-05-30 23:14:13 +02:00
|
|
|
list_push(routelist, found_route);
|
2013-11-22 15:28:46 +01:00
|
|
|
}
|
|
|
|
|
2012-11-26 19:53:38 +01:00
|
|
|
return found_route;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uip_ds6_route_t *
|
|
|
|
uip_ds6_route_add(uip_ipaddr_t *ipaddr, uint8_t length,
|
2013-07-03 19:45:56 +02:00
|
|
|
uip_ipaddr_t *nexthop)
|
2012-11-26 19:53:38 +01:00
|
|
|
{
|
|
|
|
uip_ds6_route_t *r;
|
2013-11-22 15:28:46 +01:00
|
|
|
struct uip_ds6_route_neighbor_route *nbrr;
|
2013-08-12 00:22:21 +02:00
|
|
|
|
|
|
|
#if DEBUG != DEBUG_NONE
|
|
|
|
assert_nbr_routes_list_sane();
|
|
|
|
#endif /* DEBUG != DEBUG_NONE */
|
2013-07-03 19:45:56 +02:00
|
|
|
|
|
|
|
/* Get link-layer address of next hop, make sure it is in neighbor table */
|
2013-11-16 14:42:57 +01:00
|
|
|
const uip_lladdr_t *nexthop_lladdr = uip_ds6_nbr_lladdr_from_ipaddr(nexthop);
|
2013-07-03 19:45:56 +02:00
|
|
|
if(nexthop_lladdr == NULL) {
|
2013-11-22 15:28:46 +01:00
|
|
|
PRINTF("uip_ds6_route_add: neighbor link-local address unknown for ");
|
|
|
|
PRINT6ADDR(nexthop);
|
2013-07-03 19:45:56 +02:00
|
|
|
PRINTF("\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-26 19:53:38 +01:00
|
|
|
/* First make sure that we don't add a route twice. If we find an
|
2014-06-05 18:15:20 +02:00
|
|
|
existing route for our destination, we'll delete the old
|
|
|
|
one first. */
|
2012-11-26 19:53:38 +01:00
|
|
|
r = uip_ds6_route_lookup(ipaddr);
|
|
|
|
if(r != NULL) {
|
2014-10-19 20:48:10 +02:00
|
|
|
uip_ipaddr_t *current_nexthop;
|
|
|
|
current_nexthop = uip_ds6_route_nexthop(r);
|
2015-05-07 16:58:23 +02:00
|
|
|
if(current_nexthop != NULL && uip_ipaddr_cmp(nexthop, current_nexthop)) {
|
2014-10-19 20:48:10 +02:00
|
|
|
/* no need to update route - already correct! */
|
|
|
|
return r;
|
|
|
|
}
|
2014-06-05 18:15:20 +02:00
|
|
|
PRINTF("uip_ds6_route_add: old route for ");
|
2012-11-26 19:53:38 +01:00
|
|
|
PRINT6ADDR(ipaddr);
|
2014-06-05 18:15:20 +02:00
|
|
|
PRINTF(" found, deleting it\n");
|
2014-10-19 20:48:10 +02:00
|
|
|
|
2014-06-05 18:15:20 +02:00
|
|
|
uip_ds6_route_rm(r);
|
|
|
|
}
|
|
|
|
{
|
2013-08-12 00:22:21 +02:00
|
|
|
struct uip_ds6_route_neighbor_routes *routes;
|
2013-11-22 15:28:46 +01:00
|
|
|
/* If there is no routing entry, create one. We first need to
|
|
|
|
check if we have room for this route. If not, we remove the
|
|
|
|
least recently used one we have. */
|
|
|
|
|
|
|
|
if(uip_ds6_route_num_routes() == UIP_DS6_ROUTE_NB) {
|
2015-08-20 17:00:47 +02:00
|
|
|
uip_ds6_route_t *oldest;
|
|
|
|
oldest = NULL;
|
|
|
|
#if UIP_DS6_ROUTE_REMOVE_LEAST_RECENTLY_USED
|
2013-11-22 15:28:46 +01:00
|
|
|
/* Removing the oldest route entry from the route table. The
|
|
|
|
least recently used route is the first route on the list. */
|
2015-08-20 17:00:47 +02:00
|
|
|
oldest = list_tail(routelist);
|
|
|
|
#endif
|
|
|
|
if(oldest == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-11-22 15:28:46 +01:00
|
|
|
PRINTF("uip_ds6_route_add: dropping route to ");
|
|
|
|
PRINT6ADDR(&oldest->ipaddr);
|
|
|
|
PRINTF("\n");
|
|
|
|
uip_ds6_route_rm(oldest);
|
|
|
|
}
|
|
|
|
|
2013-08-12 00:22:21 +02:00
|
|
|
|
|
|
|
/* Every neighbor on our neighbor table holds a struct
|
|
|
|
uip_ds6_route_neighbor_routes which holds a list of routes that
|
|
|
|
go through the neighbor. We add our route entry to this list.
|
|
|
|
|
|
|
|
We first check to see if we already have this neighbor in our
|
|
|
|
nbr_route table. If so, the neighbor already has a route entry
|
|
|
|
list.
|
|
|
|
*/
|
|
|
|
routes = nbr_table_get_from_lladdr(nbr_routes,
|
2013-12-12 23:58:52 +01:00
|
|
|
(linkaddr_t *)nexthop_lladdr);
|
2013-08-12 00:22:21 +02:00
|
|
|
|
|
|
|
if(routes == NULL) {
|
|
|
|
/* If the neighbor did not have an entry in our neighbor table,
|
|
|
|
we create one. The nbr_table_add_lladdr() function returns a
|
|
|
|
pointer to a pointer that we may use for our own purposes. We
|
|
|
|
initialize this pointer with the list of routing entries that
|
|
|
|
are attached to this neighbor. */
|
|
|
|
routes = nbr_table_add_lladdr(nbr_routes,
|
2015-09-20 21:02:30 +02:00
|
|
|
(linkaddr_t *)nexthop_lladdr,
|
|
|
|
NBR_TABLE_REASON_ROUTE, NULL);
|
2013-08-12 00:22:21 +02:00
|
|
|
if(routes == NULL) {
|
2013-11-22 15:28:46 +01:00
|
|
|
/* This should not happen, as we explicitly deallocated one
|
|
|
|
route table entry above. */
|
|
|
|
PRINTF("uip_ds6_route_add: could not allocate neighbor table entry\n");
|
2013-07-03 19:45:56 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-12 00:22:21 +02:00
|
|
|
LIST_STRUCT_INIT(routes, route_list);
|
2015-09-21 18:32:15 +02:00
|
|
|
#ifdef NETSTACK_CONF_ROUTING_NEIGHBOR_ADDED_CALLBACK
|
|
|
|
NETSTACK_CONF_ROUTING_NEIGHBOR_ADDED_CALLBACK((const linkaddr_t *)nexthop_lladdr);
|
|
|
|
#endif
|
2013-07-03 19:45:56 +02:00
|
|
|
}
|
|
|
|
|
2013-08-12 00:22:21 +02:00
|
|
|
/* Allocate a routing entry and populate it. */
|
2012-11-26 19:53:38 +01:00
|
|
|
r = memb_alloc(&routememb);
|
2013-08-12 00:22:21 +02:00
|
|
|
|
2012-11-26 19:53:38 +01:00
|
|
|
if(r == NULL) {
|
2013-11-22 15:28:46 +01:00
|
|
|
/* This should not happen, as we explicitly deallocated one
|
|
|
|
route table entry above. */
|
|
|
|
PRINTF("uip_ds6_route_add: could not allocate route\n");
|
2012-11-26 19:53:38 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-12 00:22:21 +02:00
|
|
|
|
2014-05-30 23:14:13 +02:00
|
|
|
/* add new routes first - assuming that there is a reason to add this
|
|
|
|
and that there is a packet coming soon. */
|
|
|
|
list_push(routelist, r);
|
2013-08-12 00:22:21 +02:00
|
|
|
|
2013-11-22 15:28:46 +01:00
|
|
|
nbrr = memb_alloc(&neighborroutememb);
|
|
|
|
if(nbrr == NULL) {
|
|
|
|
/* This should not happen, as we explicitly deallocated one
|
|
|
|
route table entry above. */
|
|
|
|
PRINTF("uip_ds6_route_add: could not allocate neighbor route list entry\n");
|
|
|
|
memb_free(&routememb, r);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
nbrr->route = r;
|
2013-07-03 19:45:56 +02:00
|
|
|
/* Add the route to this neighbor */
|
2013-11-22 15:28:46 +01:00
|
|
|
list_add(routes->route_list, nbrr);
|
|
|
|
r->neighbor_routes = routes;
|
2013-07-03 19:45:56 +02:00
|
|
|
num_routes++;
|
2012-11-26 19:53:38 +01:00
|
|
|
|
2013-07-03 19:45:56 +02:00
|
|
|
PRINTF("uip_ds6_route_add num %d\n", num_routes);
|
2015-08-26 18:23:06 +02:00
|
|
|
|
|
|
|
/* lock this entry so that nexthop is not removed */
|
|
|
|
nbr_table_lock(nbr_routes, routes);
|
2012-11-26 19:53:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uip_ipaddr_copy(&(r->ipaddr), ipaddr);
|
|
|
|
r->length = length;
|
|
|
|
|
|
|
|
#ifdef UIP_DS6_ROUTE_STATE_TYPE
|
|
|
|
memset(&r->state, 0, sizeof(UIP_DS6_ROUTE_STATE_TYPE));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PRINTF("uip_ds6_route_add: adding route: ");
|
|
|
|
PRINT6ADDR(ipaddr);
|
|
|
|
PRINTF(" via ");
|
|
|
|
PRINT6ADDR(nexthop);
|
|
|
|
PRINTF("\n");
|
|
|
|
ANNOTATE("#L %u 1;blue\n", nexthop->u8[sizeof(uip_ipaddr_t) - 1]);
|
|
|
|
|
2013-05-15 12:11:56 +02:00
|
|
|
#if UIP_DS6_NOTIFICATIONS
|
2012-11-26 19:53:38 +01:00
|
|
|
call_route_callback(UIP_DS6_NOTIFICATION_ROUTE_ADD, ipaddr, nexthop);
|
2013-05-15 12:11:56 +02:00
|
|
|
#endif
|
2012-11-26 19:53:38 +01:00
|
|
|
|
2013-08-12 00:22:21 +02:00
|
|
|
#if DEBUG != DEBUG_NONE
|
|
|
|
assert_nbr_routes_list_sane();
|
|
|
|
#endif /* DEBUG != DEBUG_NONE */
|
2012-11-26 19:53:38 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
uip_ds6_route_rm(uip_ds6_route_t *route)
|
|
|
|
{
|
2013-11-22 15:28:46 +01:00
|
|
|
struct uip_ds6_route_neighbor_route *neighbor_route;
|
2013-08-12 00:22:21 +02:00
|
|
|
#if DEBUG != DEBUG_NONE
|
|
|
|
assert_nbr_routes_list_sane();
|
|
|
|
#endif /* DEBUG != DEBUG_NONE */
|
2013-11-22 15:28:46 +01:00
|
|
|
if(route != NULL && route->neighbor_routes != NULL) {
|
2013-07-03 19:45:56 +02:00
|
|
|
|
2013-08-12 00:22:21 +02:00
|
|
|
PRINTF("uip_ds6_route_rm: removing route: ");
|
|
|
|
PRINT6ADDR(&route->ipaddr);
|
|
|
|
PRINTF("\n");
|
|
|
|
|
2014-05-30 23:14:13 +02:00
|
|
|
/* Remove the route from the route list */
|
2013-11-22 15:28:46 +01:00
|
|
|
list_remove(routelist, route);
|
|
|
|
|
|
|
|
/* Find the corresponding neighbor_route and remove it. */
|
|
|
|
for(neighbor_route = list_head(route->neighbor_routes->route_list);
|
|
|
|
neighbor_route != NULL && neighbor_route->route != route;
|
|
|
|
neighbor_route = list_item_next(neighbor_route));
|
|
|
|
|
|
|
|
if(neighbor_route == NULL) {
|
|
|
|
PRINTF("uip_ds6_route_rm: neighbor_route was NULL for ");
|
|
|
|
uip_debug_ipaddr_print(&route->ipaddr);
|
|
|
|
PRINTF("\n");
|
|
|
|
}
|
|
|
|
list_remove(route->neighbor_routes->route_list, neighbor_route);
|
|
|
|
if(list_head(route->neighbor_routes->route_list) == NULL) {
|
2013-08-12 00:22:21 +02:00
|
|
|
/* If this was the only route using this neighbor, remove the
|
2015-08-26 18:23:06 +02:00
|
|
|
neighbor from the table - this implicitly unlocks nexthop */
|
2015-11-18 15:24:35 +01:00
|
|
|
#if (DEBUG) & DEBUG_ANNOTATE
|
|
|
|
uip_ipaddr_t *nexthop = uip_ds6_route_nexthop(route);
|
|
|
|
if(nexthop != NULL) {
|
|
|
|
ANNOTATE("#L %u 0\n", nexthop->u8[sizeof(uip_ipaddr_t) - 1]);
|
|
|
|
}
|
|
|
|
#endif /* (DEBUG) & DEBUG_ANNOTATE */
|
2013-08-12 00:22:21 +02:00
|
|
|
PRINTF("uip_ds6_route_rm: removing neighbor too\n");
|
2013-11-22 15:28:46 +01:00
|
|
|
nbr_table_remove(nbr_routes, route->neighbor_routes->route_list);
|
2015-09-21 18:32:15 +02:00
|
|
|
#ifdef NETSTACK_CONF_ROUTING_NEIGHBOR_REMOVED_CALLBACK
|
|
|
|
NETSTACK_CONF_ROUTING_NEIGHBOR_REMOVED_CALLBACK(
|
|
|
|
(const linkaddr_t *)nbr_table_get_lladdr(nbr_routes, route->neighbor_routes->route_list));
|
|
|
|
#endif
|
2013-07-03 19:45:56 +02:00
|
|
|
}
|
|
|
|
memb_free(&routememb, route);
|
2013-11-22 15:28:46 +01:00
|
|
|
memb_free(&neighborroutememb, neighbor_route);
|
2013-07-03 19:45:56 +02:00
|
|
|
|
|
|
|
num_routes--;
|
2012-11-26 19:53:38 +01:00
|
|
|
|
2013-07-03 19:45:56 +02:00
|
|
|
PRINTF("uip_ds6_route_rm num %d\n", num_routes);
|
2012-11-26 19:53:38 +01:00
|
|
|
|
2013-05-15 12:11:56 +02:00
|
|
|
#if UIP_DS6_NOTIFICATIONS
|
2013-07-03 19:45:56 +02:00
|
|
|
call_route_callback(UIP_DS6_NOTIFICATION_ROUTE_RM,
|
|
|
|
&route->ipaddr, uip_ds6_route_nexthop(route));
|
|
|
|
#endif
|
2012-11-26 19:53:38 +01:00
|
|
|
}
|
2013-08-12 00:22:21 +02:00
|
|
|
|
|
|
|
#if DEBUG != DEBUG_NONE
|
|
|
|
assert_nbr_routes_list_sane();
|
|
|
|
#endif /* DEBUG != DEBUG_NONE */
|
2013-07-03 19:45:56 +02:00
|
|
|
return;
|
2012-11-26 19:53:38 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-08-12 00:22:21 +02:00
|
|
|
static void
|
|
|
|
rm_routelist(struct uip_ds6_route_neighbor_routes *routes)
|
2012-11-26 19:53:38 +01:00
|
|
|
{
|
2013-08-12 00:22:21 +02:00
|
|
|
#if DEBUG != DEBUG_NONE
|
|
|
|
assert_nbr_routes_list_sane();
|
|
|
|
#endif /* DEBUG != DEBUG_NONE */
|
|
|
|
PRINTF("uip_ds6_route_rm_routelist\n");
|
|
|
|
if(routes != NULL && routes->route_list != NULL) {
|
2013-11-22 15:28:46 +01:00
|
|
|
struct uip_ds6_route_neighbor_route *r;
|
2013-08-12 00:22:21 +02:00
|
|
|
r = list_head(routes->route_list);
|
2013-07-03 19:45:56 +02:00
|
|
|
while(r != NULL) {
|
2013-11-22 15:28:46 +01:00
|
|
|
uip_ds6_route_rm(r->route);
|
2013-08-12 00:22:21 +02:00
|
|
|
r = list_head(routes->route_list);
|
2012-11-26 19:53:38 +01:00
|
|
|
}
|
2013-08-12 00:22:21 +02:00
|
|
|
nbr_table_remove(nbr_routes, routes);
|
2012-11-26 19:53:38 +01:00
|
|
|
}
|
2013-08-12 00:22:21 +02:00
|
|
|
#if DEBUG != DEBUG_NONE
|
|
|
|
assert_nbr_routes_list_sane();
|
|
|
|
#endif /* DEBUG != DEBUG_NONE */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
rm_routelist_callback(nbr_table_item_t *ptr)
|
|
|
|
{
|
|
|
|
rm_routelist((struct uip_ds6_route_neighbor_routes *)ptr);
|
2012-11-26 19:53:38 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-07-03 19:45:56 +02:00
|
|
|
void
|
|
|
|
uip_ds6_route_rm_by_nexthop(uip_ipaddr_t *nexthop)
|
|
|
|
{
|
|
|
|
/* Get routing entry list of this neighbor */
|
2013-11-16 14:42:57 +01:00
|
|
|
const uip_lladdr_t *nexthop_lladdr;
|
2013-08-12 00:22:21 +02:00
|
|
|
struct uip_ds6_route_neighbor_routes *routes;
|
|
|
|
|
|
|
|
nexthop_lladdr = uip_ds6_nbr_lladdr_from_ipaddr(nexthop);
|
|
|
|
routes = nbr_table_get_from_lladdr(nbr_routes,
|
2013-12-12 23:58:52 +01:00
|
|
|
(linkaddr_t *)nexthop_lladdr);
|
2013-08-12 00:22:21 +02:00
|
|
|
rm_routelist(routes);
|
2013-07-03 19:45:56 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-11-26 19:53:38 +01:00
|
|
|
uip_ds6_defrt_t *
|
|
|
|
uip_ds6_defrt_add(uip_ipaddr_t *ipaddr, unsigned long interval)
|
|
|
|
{
|
|
|
|
uip_ds6_defrt_t *d;
|
|
|
|
|
2013-08-12 00:22:21 +02:00
|
|
|
#if DEBUG != DEBUG_NONE
|
|
|
|
assert_nbr_routes_list_sane();
|
|
|
|
#endif /* DEBUG != DEBUG_NONE */
|
|
|
|
|
|
|
|
PRINTF("uip_ds6_defrt_add\n");
|
2012-11-26 19:53:38 +01:00
|
|
|
d = uip_ds6_defrt_lookup(ipaddr);
|
|
|
|
if(d == NULL) {
|
|
|
|
d = memb_alloc(&defaultroutermemb);
|
|
|
|
if(d == NULL) {
|
|
|
|
PRINTF("uip_ds6_defrt_add: could not add default route to ");
|
|
|
|
PRINT6ADDR(ipaddr);
|
|
|
|
PRINTF(", out of memory\n");
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
PRINTF("uip_ds6_defrt_add: adding default route to ");
|
|
|
|
PRINT6ADDR(ipaddr);
|
|
|
|
PRINTF("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
list_push(defaultrouterlist, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
uip_ipaddr_copy(&d->ipaddr, ipaddr);
|
|
|
|
if(interval != 0) {
|
|
|
|
stimer_set(&d->lifetime, interval);
|
|
|
|
d->isinfinite = 0;
|
|
|
|
} else {
|
|
|
|
d->isinfinite = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ANNOTATE("#L %u 1\n", ipaddr->u8[sizeof(uip_ipaddr_t) - 1]);
|
|
|
|
|
2013-05-15 12:11:56 +02:00
|
|
|
#if UIP_DS6_NOTIFICATIONS
|
2012-11-26 19:53:38 +01:00
|
|
|
call_route_callback(UIP_DS6_NOTIFICATION_DEFRT_ADD, ipaddr, ipaddr);
|
2013-05-15 12:11:56 +02:00
|
|
|
#endif
|
2012-11-26 19:53:38 +01:00
|
|
|
|
2013-08-12 00:22:21 +02:00
|
|
|
#if DEBUG != DEBUG_NONE
|
|
|
|
assert_nbr_routes_list_sane();
|
|
|
|
#endif /* DEBUG != DEBUG_NONE */
|
|
|
|
|
2012-11-26 19:53:38 +01:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
uip_ds6_defrt_rm(uip_ds6_defrt_t *defrt)
|
|
|
|
{
|
|
|
|
uip_ds6_defrt_t *d;
|
2013-08-12 00:22:21 +02:00
|
|
|
|
|
|
|
#if DEBUG != DEBUG_NONE
|
|
|
|
assert_nbr_routes_list_sane();
|
|
|
|
#endif /* DEBUG != DEBUG_NONE */
|
|
|
|
|
2012-11-26 19:53:38 +01:00
|
|
|
/* Make sure that the defrt is in the list before we remove it. */
|
|
|
|
for(d = list_head(defaultrouterlist);
|
|
|
|
d != NULL;
|
|
|
|
d = list_item_next(d)) {
|
|
|
|
if(d == defrt) {
|
|
|
|
PRINTF("Removing default route\n");
|
|
|
|
list_remove(defaultrouterlist, defrt);
|
|
|
|
memb_free(&defaultroutermemb, defrt);
|
|
|
|
ANNOTATE("#L %u 0\n", defrt->ipaddr.u8[sizeof(uip_ipaddr_t) - 1]);
|
2013-05-15 12:11:56 +02:00
|
|
|
#if UIP_DS6_NOTIFICATIONS
|
2012-11-26 19:53:38 +01:00
|
|
|
call_route_callback(UIP_DS6_NOTIFICATION_DEFRT_RM,
|
|
|
|
&defrt->ipaddr, &defrt->ipaddr);
|
2013-05-15 12:11:56 +02:00
|
|
|
#endif
|
2012-11-26 19:53:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-08-12 00:22:21 +02:00
|
|
|
#if DEBUG != DEBUG_NONE
|
|
|
|
assert_nbr_routes_list_sane();
|
|
|
|
#endif /* DEBUG != DEBUG_NONE */
|
|
|
|
|
2012-11-26 19:53:38 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uip_ds6_defrt_t *
|
|
|
|
uip_ds6_defrt_lookup(uip_ipaddr_t *ipaddr)
|
|
|
|
{
|
|
|
|
uip_ds6_defrt_t *d;
|
|
|
|
for(d = list_head(defaultrouterlist);
|
|
|
|
d != NULL;
|
|
|
|
d = list_item_next(d)) {
|
|
|
|
if(uip_ipaddr_cmp(&d->ipaddr, ipaddr)) {
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uip_ipaddr_t *
|
|
|
|
uip_ds6_defrt_choose(void)
|
|
|
|
{
|
|
|
|
uip_ds6_defrt_t *d;
|
|
|
|
uip_ds6_nbr_t *bestnbr;
|
|
|
|
uip_ipaddr_t *addr;
|
|
|
|
|
|
|
|
addr = NULL;
|
|
|
|
for(d = list_head(defaultrouterlist);
|
|
|
|
d != NULL;
|
|
|
|
d = list_item_next(d)) {
|
|
|
|
PRINTF("Defrt, IP address ");
|
|
|
|
PRINT6ADDR(&d->ipaddr);
|
|
|
|
PRINTF("\n");
|
|
|
|
bestnbr = uip_ds6_nbr_lookup(&d->ipaddr);
|
|
|
|
if(bestnbr != NULL && bestnbr->state != NBR_INCOMPLETE) {
|
|
|
|
PRINTF("Defrt found, IP address ");
|
|
|
|
PRINT6ADDR(&d->ipaddr);
|
|
|
|
PRINTF("\n");
|
|
|
|
return &d->ipaddr;
|
|
|
|
} else {
|
|
|
|
addr = &d->ipaddr;
|
|
|
|
PRINTF("Defrt INCOMPLETE found, IP address ");
|
|
|
|
PRINT6ADDR(&d->ipaddr);
|
|
|
|
PRINTF("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
uip_ds6_defrt_periodic(void)
|
|
|
|
{
|
|
|
|
uip_ds6_defrt_t *d;
|
|
|
|
d = list_head(defaultrouterlist);
|
|
|
|
while(d != NULL) {
|
|
|
|
if(!d->isinfinite &&
|
|
|
|
stimer_expired(&d->lifetime)) {
|
2012-11-27 23:01:10 +01:00
|
|
|
PRINTF("uip_ds6_defrt_periodic: defrt lifetime expired\n");
|
2012-11-26 19:53:38 +01:00
|
|
|
uip_ds6_defrt_rm(d);
|
|
|
|
d = list_head(defaultrouterlist);
|
|
|
|
} else {
|
|
|
|
d = list_item_next(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-02-15 19:02:07 +01:00
|
|
|
/** @} */
|