2012-08-27 00:34:59 +00:00
|
|
|
/*
|
2013-12-04 06:26:55 +00:00
|
|
|
Serval DNA MDP addressing
|
2018-03-26 12:05:34 +00:00
|
|
|
Copyright (C) 2016-2018 Flinders University
|
2016-09-27 07:26:44 +00:00
|
|
|
Copyright (C) 2012-2015 Serval Project Inc.
|
2013-12-04 06:26:55 +00:00
|
|
|
Copyright (C) 2012 Paul Gardner-Stephen
|
2012-08-27 00:34:59 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Smart-flooding of broadcast information is also a requirement. The long addresses help here, as we can make any address that begins
|
|
|
|
with the first 192 bits all ones be broadcast, and use the remaining 64 bits as a "broadcast packet identifier" (BPI).
|
|
|
|
Nodes can remember recently seen BPIs and not forward broadcast frames that have been seen recently. This should get us smart flooding
|
|
|
|
of the majority of a mesh (with some node mobility issues being a factor). We could refine this later, but it will do for now, especially
|
|
|
|
since for things like number resolution we are happy to send repeat requests.
|
|
|
|
*/
|
|
|
|
|
2013-11-25 06:13:32 +00:00
|
|
|
#include <assert.h>
|
2013-12-09 07:15:47 +00:00
|
|
|
#include <arpa/inet.h>
|
2018-03-26 12:05:34 +00:00
|
|
|
#include "lang.h" // for bool_t
|
2012-08-27 00:34:59 +00:00
|
|
|
#include "serval.h"
|
2012-12-04 03:42:28 +00:00
|
|
|
#include "conf.h"
|
2017-11-24 00:03:39 +00:00
|
|
|
#include "keyring.h"
|
2016-10-16 00:14:36 +00:00
|
|
|
#include "crypto.h"
|
2012-11-07 06:12:45 +00:00
|
|
|
#include "str.h"
|
2012-08-27 00:34:59 +00:00
|
|
|
#include "overlay_address.h"
|
|
|
|
#include "overlay_buffer.h"
|
2013-12-09 07:15:47 +00:00
|
|
|
#include "overlay_interface.h"
|
2012-09-19 04:46:40 +00:00
|
|
|
#include "overlay_packet.h"
|
2014-08-25 04:54:00 +00:00
|
|
|
#include "server.h"
|
2015-08-31 05:48:08 +00:00
|
|
|
#include "route_link.h"
|
2016-11-11 00:09:50 +00:00
|
|
|
#include "commandline.h"
|
2017-11-24 00:03:39 +00:00
|
|
|
#include "debug.h"
|
2012-08-27 00:34:59 +00:00
|
|
|
|
|
|
|
#define MAX_BPIS 1024
|
|
|
|
#define BPI_MASK 0x3ff
|
|
|
|
static struct broadcast bpilist[MAX_BPIS];
|
|
|
|
|
2012-11-30 03:15:08 +00:00
|
|
|
#define OA_CODE_SELF 0xff
|
|
|
|
#define OA_CODE_PREVIOUS 0xfe
|
2013-07-16 05:46:07 +00:00
|
|
|
#define OA_CODE_P2P_YOU 0xfd
|
|
|
|
#define OA_CODE_P2P_ME 0xfc
|
2016-07-25 06:20:43 +00:00
|
|
|
#define OA_CODE_SIGNKEY 0xfb // full sign key of an identity, from which a SID can be derived
|
2012-11-30 03:15:08 +00:00
|
|
|
|
2017-10-20 06:35:14 +00:00
|
|
|
static __thread struct tree_root root={.index_size_bytes=SID_SIZE};
|
2012-08-27 00:34:59 +00:00
|
|
|
|
2018-03-26 12:05:34 +00:00
|
|
|
static __thread bool_t primary_sid_written = 0;
|
|
|
|
static __thread struct subscriber *primary_sid = NULL;
|
|
|
|
static __thread struct subscriber *my_subscriber = NULL;
|
2016-06-15 07:38:25 +00:00
|
|
|
|
2018-03-26 12:05:34 +00:00
|
|
|
struct subscriber *get_my_subscriber(bool_t create)
|
|
|
|
{
|
2016-06-15 07:38:25 +00:00
|
|
|
if (!serverMode)
|
|
|
|
return NULL;
|
|
|
|
if (my_subscriber && my_subscriber->reachable != REACHABLE_SELF)
|
|
|
|
my_subscriber = NULL;
|
2018-03-26 12:05:34 +00:00
|
|
|
// Look for a reachable self-identity in the keyring.
|
|
|
|
if (!my_subscriber) {
|
|
|
|
assert(keyring != NULL);
|
|
|
|
keyring_identity *id;
|
|
|
|
for (id = keyring->identities; id; id = id->next)
|
|
|
|
if (id->subscriber->reachable == REACHABLE_SELF) {
|
2016-10-19 09:26:11 +00:00
|
|
|
my_subscriber = id->subscriber;
|
2018-03-26 12:05:34 +00:00
|
|
|
break;
|
2016-10-19 09:26:11 +00:00
|
|
|
}
|
2018-03-26 12:05:34 +00:00
|
|
|
}
|
|
|
|
// If there is no reachable self-identity in the keyring, then roll one in-memory, which will
|
|
|
|
// persist until the server terminates.
|
|
|
|
if (!my_subscriber && create) {
|
|
|
|
keyring_identity *id = keyring_inmemory_identity();
|
|
|
|
if (id)
|
2016-10-19 09:26:11 +00:00
|
|
|
my_subscriber = id->subscriber;
|
2018-03-26 12:05:34 +00:00
|
|
|
}
|
|
|
|
// Normally, the server creates files in proc/ before it creates its pidfile, and does not modify
|
|
|
|
// those files while running. This avoids any race conditions with other processes that read the
|
|
|
|
// files. In this case, the proc/primary_sid file is written _after_ the pidfile, and could
|
|
|
|
// potentially be re-written while running (eg, if the primary identity is deleted, causing the
|
|
|
|
// server to choose another). Potential race conditions are avoided because (1) the size of the
|
|
|
|
// file never alters, and (2) the server_write_proc_state() function overwrites any existing
|
|
|
|
// content using a single, indivisible write(2) system call.
|
|
|
|
if (!primary_sid_written || primary_sid != my_subscriber) {
|
|
|
|
primary_sid = my_subscriber;
|
|
|
|
if (my_subscriber) {
|
|
|
|
const char *sidhex = alloca_tohex_sid_t(primary_sid->sid);
|
|
|
|
server_write_proc_state("primary_sid", "%s", sidhex);
|
|
|
|
INFOF("PRIMARY IDENTITY sid=%s", sidhex);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
server_unlink_proc_state("primary_sid");
|
|
|
|
INFOF("NO PRIMARY IDENTITY");
|
2016-10-19 09:26:11 +00:00
|
|
|
}
|
2018-03-26 12:05:34 +00:00
|
|
|
primary_sid_written = 1;
|
2016-06-15 07:38:25 +00:00
|
|
|
}
|
|
|
|
return my_subscriber;
|
|
|
|
}
|
|
|
|
|
2018-03-26 12:05:34 +00:00
|
|
|
void release_my_subscriber()
|
|
|
|
{
|
2016-06-15 07:38:25 +00:00
|
|
|
if (my_subscriber && my_subscriber->identity->slot==0)
|
|
|
|
keyring_free_identity(my_subscriber->identity);
|
2018-03-26 12:05:34 +00:00
|
|
|
server_unlink_proc_state("primary_sid");
|
2016-06-15 07:38:25 +00:00
|
|
|
my_subscriber = NULL;
|
2018-03-26 12:05:34 +00:00
|
|
|
primary_sid = NULL;
|
|
|
|
primary_sid_written = 0;
|
2016-06-15 07:38:25 +00:00
|
|
|
}
|
2012-08-27 00:34:59 +00:00
|
|
|
|
2016-09-13 04:44:21 +00:00
|
|
|
static int free_node(void **record, void *UNUSED(context))
|
2013-11-06 05:06:21 +00:00
|
|
|
{
|
2016-09-13 04:44:21 +00:00
|
|
|
struct subscriber *subscriber = (struct subscriber *)*record;
|
2013-11-06 05:06:21 +00:00
|
|
|
if (subscriber->link_state || subscriber->destination)
|
2017-05-02 03:22:07 +00:00
|
|
|
FATALF("Can't free a subscriber that is being used in routing (%s, %p, %p)",
|
|
|
|
alloca_tohex_sid_t(subscriber->sid), subscriber->link_state, subscriber->destination);
|
2013-11-06 05:06:21 +00:00
|
|
|
if (subscriber->sync_state)
|
2017-05-02 03:22:07 +00:00
|
|
|
FATALF("Can't free a subscriber that is being used by rhizome (%s, %p)",
|
|
|
|
alloca_tohex_sid_t(subscriber->sid), subscriber->sync_state);
|
2013-11-06 05:06:21 +00:00
|
|
|
if (subscriber->identity)
|
|
|
|
FATAL("Can't free a subscriber that is unlocked in the keyring");
|
2018-03-26 12:05:34 +00:00
|
|
|
if (subscriber == my_subscriber)
|
|
|
|
FATAL("Can't free a subscriber that is the primary identity");
|
2013-11-06 05:06:21 +00:00
|
|
|
free(subscriber);
|
2016-09-13 04:44:21 +00:00
|
|
|
*record=NULL;
|
|
|
|
return 0;
|
2013-11-06 05:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void free_subscribers()
|
|
|
|
{
|
|
|
|
// don't attempt to free anything if we're running as a server
|
|
|
|
// who knows where subscriber ptr's may have leaked to.
|
|
|
|
if (serverMode)
|
|
|
|
FATAL("Freeing subscribers from a running daemon is not supported");
|
2016-09-13 04:44:21 +00:00
|
|
|
tree_walk(&root, NULL, 0, free_node, NULL);
|
2013-11-06 05:06:21 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 04:44:21 +00:00
|
|
|
static void *create_subscriber(void *UNUSED(context), const uint8_t *binary, size_t bin_length)
|
2013-10-09 08:24:21 +00:00
|
|
|
{
|
2016-09-13 04:44:21 +00:00
|
|
|
assert(bin_length == SID_SIZE);
|
|
|
|
struct subscriber *ret = (struct subscriber *) emalloc_zero(sizeof(struct subscriber));
|
|
|
|
if (ret){
|
|
|
|
ret->sid = *(const sid_t *)binary;
|
|
|
|
DEBUGF(subscriber, "Stored %s", alloca_tohex_sid_t(ret->sid));
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
2016-09-13 04:44:21 +00:00
|
|
|
return ret;
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 04:44:21 +00:00
|
|
|
// find a subscriber struct from a whole or abbreviated subscriber id
|
|
|
|
struct subscriber *find_subscriber(const uint8_t *sidp, int len, int create)
|
2016-01-12 01:17:29 +00:00
|
|
|
{
|
2016-09-13 04:44:21 +00:00
|
|
|
struct subscriber *result;
|
|
|
|
tree_find(&root, (void**)&result, sidp, len, create && len == SID_SIZE ? create_subscriber : NULL, NULL);
|
|
|
|
// ignore return code, just return the result
|
|
|
|
return result;
|
2016-01-12 01:17:29 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 23:43:08 +00:00
|
|
|
// iterate over subscribers in ascending binary order
|
|
|
|
|
|
|
|
void subscriber_iterator_start(subscriber_iterator *it)
|
|
|
|
{
|
|
|
|
tree_iterator_start(&it->tree_iterator, &root);
|
|
|
|
}
|
|
|
|
|
|
|
|
void subscriber_iterator_advance_to(subscriber_iterator *it, const sid_t *sid)
|
|
|
|
{
|
|
|
|
tree_iterator_advance_to(&it->tree_iterator, sid->binary, sizeof sid->binary);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct subscriber **subscriber_iterator_get_current(subscriber_iterator *it)
|
|
|
|
{
|
|
|
|
return (struct subscriber **) tree_iterator_get_node(&it->tree_iterator);
|
|
|
|
}
|
|
|
|
|
|
|
|
void subscriber_iterator_advance(subscriber_iterator *it)
|
|
|
|
{
|
|
|
|
tree_iterator_advance(&it->tree_iterator);
|
|
|
|
}
|
|
|
|
|
|
|
|
void subscriber_iterator_free(subscriber_iterator *it)
|
|
|
|
{
|
|
|
|
tree_iterator_free(&it->tree_iterator);
|
|
|
|
}
|
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
/*
|
2012-10-09 01:34:02 +00:00
|
|
|
walk the tree, starting at start inclusive, calling the supplied callback function
|
2012-08-27 00:34:59 +00:00
|
|
|
*/
|
2016-09-13 04:44:21 +00:00
|
|
|
void enum_subscribers(struct subscriber *start, walk_callback callback, void *context)
|
2013-10-09 08:24:21 +00:00
|
|
|
{
|
2016-09-13 04:44:21 +00:00
|
|
|
tree_walk(&root, start?start->sid.binary:NULL, SID_SIZE, callback, context);
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// generate a new random broadcast address
|
|
|
|
int overlay_broadcast_generate_address(struct broadcast *addr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0;i<BROADCAST_LEN;i++) addr->id[i]=random()&0xff;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// test if the broadcast address has been seen
|
|
|
|
int overlay_broadcast_drop_check(struct broadcast *addr)
|
|
|
|
{
|
|
|
|
/* Hash the BPI and see if we have seen it recently.
|
|
|
|
If so, drop the frame.
|
|
|
|
The occassional failure to supress a broadcast frame is not
|
|
|
|
something we are going to worry about just yet. For byzantine
|
|
|
|
robustness it is however required. */
|
|
|
|
int bpi_index=0;
|
|
|
|
int i;
|
|
|
|
for(i=0;i<BROADCAST_LEN;i++)
|
|
|
|
{
|
|
|
|
bpi_index=((bpi_index<<3)&0xfff8)+((bpi_index>>13)&0x7);
|
|
|
|
bpi_index^=addr->id[i];
|
|
|
|
}
|
|
|
|
bpi_index&=BPI_MASK;
|
|
|
|
|
|
|
|
if (memcmp(bpilist[bpi_index].id, addr->id, BROADCAST_LEN)){
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(broadcasts, "BPI %s is new", alloca_tohex(addr->id, BROADCAST_LEN));
|
2012-08-27 00:34:59 +00:00
|
|
|
bcopy(addr->id, bpilist[bpi_index].id, BROADCAST_LEN);
|
|
|
|
return 0; /* don't drop */
|
|
|
|
}else{
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(broadcasts, "BPI %s is a duplicate", alloca_tohex(addr->id, BROADCAST_LEN));
|
2012-08-27 00:34:59 +00:00
|
|
|
return 1; /* drop frame because we have seen this BPI recently */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 06:13:32 +00:00
|
|
|
void overlay_broadcast_append(struct overlay_buffer *b, struct broadcast *broadcast)
|
2012-08-27 00:34:59 +00:00
|
|
|
{
|
2013-11-25 06:13:32 +00:00
|
|
|
ob_append_bytes(b, broadcast->id, BROADCAST_LEN);
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// append an appropriate abbreviation into the address
|
2013-11-25 06:13:32 +00:00
|
|
|
void overlay_address_append(struct decode_context *context, struct overlay_buffer *b, struct subscriber *subscriber)
|
2012-08-27 00:34:59 +00:00
|
|
|
{
|
2013-11-25 06:13:32 +00:00
|
|
|
assert(subscriber != NULL);
|
|
|
|
if (context && subscriber == context->point_to_point_device)
|
|
|
|
ob_append_byte(b, OA_CODE_P2P_YOU);
|
|
|
|
else if(context
|
2013-07-16 05:46:07 +00:00
|
|
|
&& !subscriber->send_full
|
2016-10-19 09:26:11 +00:00
|
|
|
&& subscriber == get_my_subscriber(1)
|
2013-08-08 05:50:31 +00:00
|
|
|
&& context->point_to_point_device
|
2015-11-16 03:17:28 +00:00
|
|
|
&& ((context->flags & DECODE_FLAG_ENCODING_HEADER)==0 || !context->interface->local_echo))
|
2013-11-25 06:13:32 +00:00
|
|
|
ob_append_byte(b, OA_CODE_P2P_ME);
|
|
|
|
else if (context && subscriber==context->sender)
|
|
|
|
ob_append_byte(b, OA_CODE_SELF);
|
|
|
|
else if (context && subscriber==context->previous)
|
|
|
|
ob_append_byte(b, OA_CODE_PREVIOUS);
|
|
|
|
else {
|
2012-11-26 04:58:13 +00:00
|
|
|
if (subscriber->send_full){
|
2016-07-25 06:20:43 +00:00
|
|
|
// TODO work out when we can use OA_CODE_SIGNKEY
|
|
|
|
ob_append_byte(b, SID_SIZE);
|
|
|
|
ob_append_bytes(b, subscriber->sid.binary, SID_SIZE);
|
2012-11-26 04:58:13 +00:00
|
|
|
subscriber->send_full=0;
|
|
|
|
}else{
|
2016-09-13 04:44:21 +00:00
|
|
|
// always send 8-12 extra bits to disambiguate abbreviations
|
2016-09-27 00:58:46 +00:00
|
|
|
unsigned len=(subscriber->tree_depth >> 3) + 1;
|
2017-05-01 00:36:24 +00:00
|
|
|
// add another 8 bits when we need more certainty
|
|
|
|
if (context && (context->flags & DECODE_FLAG_EXTRA_BITS))
|
2012-11-26 04:58:13 +00:00
|
|
|
len++;
|
|
|
|
if (len>SID_SIZE)
|
|
|
|
len=SID_SIZE;
|
2016-07-25 06:20:43 +00:00
|
|
|
ob_append_byte(b, len);
|
|
|
|
ob_append_bytes(b, subscriber->sid.binary, len);
|
2012-11-26 04:58:13 +00:00
|
|
|
}
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
2012-11-22 22:34:42 +00:00
|
|
|
if (context)
|
|
|
|
context->previous = subscriber;
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 04:44:21 +00:00
|
|
|
static int add_explain_response(void **record, void *context)
|
2013-11-25 06:13:32 +00:00
|
|
|
{
|
2016-09-13 04:44:21 +00:00
|
|
|
struct subscriber *subscriber = *record;
|
2012-09-19 04:46:40 +00:00
|
|
|
struct decode_context *response = context;
|
2013-06-19 05:57:17 +00:00
|
|
|
// only explain a SID once every half second.
|
|
|
|
time_ms_t now = gettime_ms();
|
|
|
|
if (now - subscriber->last_explained < 500)
|
|
|
|
return 0;
|
|
|
|
subscriber->last_explained = now;
|
|
|
|
|
2012-09-19 04:46:40 +00:00
|
|
|
if (!response->please_explain){
|
2013-11-25 06:13:32 +00:00
|
|
|
if ((response->please_explain = emalloc_zero(sizeof(struct overlay_frame))) == NULL)
|
|
|
|
return 1; // stop walking
|
|
|
|
if ((response->please_explain->payload = ob_new()) == NULL) {
|
|
|
|
free(response->please_explain);
|
|
|
|
response->please_explain = NULL;
|
|
|
|
return 1; // stop walking
|
|
|
|
}
|
2012-09-19 04:46:40 +00:00
|
|
|
ob_limitsize(response->please_explain->payload, 1024);
|
|
|
|
}
|
2016-07-25 06:20:43 +00:00
|
|
|
|
|
|
|
// if our primary routing identities is unknown,
|
2012-12-11 03:47:01 +00:00
|
|
|
// the header of this packet must include our full sid.
|
2016-10-19 09:26:11 +00:00
|
|
|
if (subscriber==get_my_subscriber(1)){
|
2016-07-25 06:20:43 +00:00
|
|
|
DEBUGF(subscriber, "Explaining SELF sid=%s", alloca_tohex_sid_t(subscriber->sid));
|
|
|
|
response->please_explain->source_full=1;
|
|
|
|
return 0;
|
2012-12-11 03:47:01 +00:00
|
|
|
}
|
2012-09-19 07:02:25 +00:00
|
|
|
|
2016-07-25 06:20:43 +00:00
|
|
|
struct overlay_buffer *b = response->please_explain->payload;
|
|
|
|
|
2012-09-19 04:46:40 +00:00
|
|
|
// add the whole subscriber id to the payload, stop if we run out of space
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(subscriber, "Explaining sid=%s", alloca_tohex_sid_t(subscriber->sid));
|
2016-07-25 06:20:43 +00:00
|
|
|
ob_checkpoint(b);
|
|
|
|
|
2016-08-15 03:43:26 +00:00
|
|
|
if (subscriber->id_combined && response->sender && response->sender->id_combined){
|
2016-07-25 06:20:43 +00:00
|
|
|
// TODO better condition for when we should send this?
|
|
|
|
ob_append_byte(b, OA_CODE_SIGNKEY);
|
2016-08-15 03:43:26 +00:00
|
|
|
ob_append_bytes(b, subscriber->id_public.binary, crypto_sign_PUBLICKEYBYTES);
|
2016-07-25 06:20:43 +00:00
|
|
|
}else{
|
|
|
|
ob_append_byte(b, SID_SIZE);
|
|
|
|
ob_append_bytes(b, subscriber->sid.binary, SID_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ob_overrun(b)) {
|
|
|
|
ob_rewind(b);
|
2012-09-19 04:46:40 +00:00
|
|
|
return 1;
|
2013-11-25 12:10:14 +00:00
|
|
|
}
|
2013-04-26 07:23:04 +00:00
|
|
|
// let the routing engine know that we had to explain this sid, we probably need to re-send routing info
|
|
|
|
link_explained(subscriber);
|
2012-09-07 00:31:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-27 00:58:46 +00:00
|
|
|
static int find_subscr_buffer(struct decode_context *context, struct overlay_buffer *b, unsigned len, struct subscriber **subscriber)
|
2013-11-25 12:10:14 +00:00
|
|
|
{
|
2016-01-11 23:58:30 +00:00
|
|
|
assert(subscriber);
|
2016-09-27 00:58:46 +00:00
|
|
|
if (len>SID_SIZE)
|
2013-09-13 04:32:35 +00:00
|
|
|
return WHYF("Invalid abbreviation length %d", len);
|
2012-12-06 01:34:13 +00:00
|
|
|
|
2016-07-25 06:20:43 +00:00
|
|
|
uint8_t *id = ob_get_bytes_ptr(b, len);
|
2016-01-11 23:58:30 +00:00
|
|
|
if (!id)
|
2012-09-06 07:08:11 +00:00
|
|
|
return WHY("Not enough space in buffer to parse address");
|
2012-09-19 04:46:40 +00:00
|
|
|
|
2012-11-26 04:58:13 +00:00
|
|
|
*subscriber=find_subscriber(id, len, 1);
|
2012-09-06 07:08:11 +00:00
|
|
|
|
|
|
|
if (!*subscriber){
|
2016-04-12 06:13:56 +00:00
|
|
|
if (!context)
|
|
|
|
return WHYF("Unable to decode %s, with no context", alloca_tohex(id, len));
|
|
|
|
|
2015-11-16 03:17:28 +00:00
|
|
|
context->flags|=DECODE_FLAG_INVALID_ADDRESS;
|
2012-09-06 07:08:11 +00:00
|
|
|
|
2016-01-11 23:58:30 +00:00
|
|
|
if (context->flags & DECODE_FLAG_DONT_EXPLAIN){
|
2016-09-13 04:44:21 +00:00
|
|
|
DEBUGF(subscriber, "Ignoring unknown prefix %s", alloca_tohex(id, len));
|
2016-01-11 23:58:30 +00:00
|
|
|
}else{
|
2015-11-16 03:17:28 +00:00
|
|
|
// generate a please explain in the passed in context
|
|
|
|
|
|
|
|
// add the abbreviation you told me about
|
|
|
|
if (!context->please_explain){
|
|
|
|
context->please_explain = calloc(sizeof(struct overlay_frame),1);
|
|
|
|
if ((context->please_explain->payload = ob_new()) == NULL)
|
|
|
|
return -1;
|
|
|
|
ob_limitsize(context->please_explain->payload, MDP_MTU);
|
|
|
|
}
|
|
|
|
|
|
|
|
// And I'll tell you about any subscribers I know that match this abbreviation,
|
|
|
|
// so you don't try to use an abbreviation that's too short in future.
|
2016-09-13 04:44:21 +00:00
|
|
|
tree_walk_prefix(&root, id, len, add_explain_response, context);
|
2015-11-16 03:17:28 +00:00
|
|
|
|
2016-01-11 23:58:30 +00:00
|
|
|
DEBUGF(subscriber, "Asking for explanation of %s", alloca_tohex(id, len));
|
2015-11-16 03:17:28 +00:00
|
|
|
ob_append_byte(context->please_explain->payload, len);
|
|
|
|
ob_append_bytes(context->please_explain->payload, id, len);
|
2012-09-19 04:46:40 +00:00
|
|
|
}
|
|
|
|
}else{
|
2012-12-07 05:34:40 +00:00
|
|
|
if (context)
|
|
|
|
context->previous=*subscriber;
|
2012-09-06 07:08:11 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
|
|
|
|
2012-11-26 04:58:13 +00:00
|
|
|
int overlay_broadcast_parse(struct overlay_buffer *b, struct broadcast *broadcast)
|
|
|
|
{
|
|
|
|
return ob_get_bytes(b, broadcast->id, BROADCAST_LEN);
|
|
|
|
}
|
|
|
|
|
2016-07-25 06:20:43 +00:00
|
|
|
static int decode_sid_from_signkey(struct overlay_buffer *b, struct subscriber **subscriber)
|
|
|
|
{
|
2016-10-16 00:14:36 +00:00
|
|
|
const sign_public_t *id = (const sign_public_t *)ob_get_bytes_ptr(b, crypto_sign_PUBLICKEYBYTES);
|
2016-07-25 06:20:43 +00:00
|
|
|
if (!id)
|
|
|
|
return WHY("Not enough space in buffer to parse address");
|
|
|
|
sid_t sid;
|
2016-10-16 00:14:36 +00:00
|
|
|
if (crypto_sign_to_sid(id, &sid))
|
|
|
|
return -1;
|
2016-07-25 06:20:43 +00:00
|
|
|
struct subscriber *s = find_subscriber(sid.binary, SID_SIZE, 1);
|
2016-08-15 03:43:26 +00:00
|
|
|
if (s && !s->id_combined){
|
|
|
|
bcopy(id, s->id_public.binary, crypto_sign_PUBLICKEYBYTES);
|
|
|
|
s->id_valid=1;
|
|
|
|
s->id_combined=1;
|
2016-07-25 06:20:43 +00:00
|
|
|
DEBUGF(subscriber, "Stored combined SID:SAS mapping, SID=%s SAS=%s",
|
|
|
|
alloca_tohex_sid_t(s->sid),
|
2016-08-15 03:43:26 +00:00
|
|
|
alloca_tohex_identity_t(&s->id_public)
|
2016-07-25 06:20:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (subscriber)
|
|
|
|
*subscriber=s;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-06 07:08:11 +00:00
|
|
|
// returns 0 = success, -1 = fatal parsing error, 1 = unable to identify address
|
2012-11-26 04:58:13 +00:00
|
|
|
int overlay_address_parse(struct decode_context *context, struct overlay_buffer *b, struct subscriber **subscriber)
|
2012-08-27 00:34:59 +00:00
|
|
|
{
|
2012-11-26 04:58:13 +00:00
|
|
|
int len = ob_get(b);
|
2012-12-08 04:39:41 +00:00
|
|
|
if (len<0)
|
|
|
|
return WHY("Buffer too small");
|
2012-11-26 04:58:13 +00:00
|
|
|
|
|
|
|
switch(len){
|
2013-07-16 05:46:07 +00:00
|
|
|
case OA_CODE_P2P_YOU:
|
2013-08-08 05:50:31 +00:00
|
|
|
// if we don't know who they are, we can't assume they mean us.
|
|
|
|
if (context->point_to_point_device){
|
2016-10-19 09:26:11 +00:00
|
|
|
context->previous = *subscriber = get_my_subscriber(1);
|
2013-07-16 05:46:07 +00:00
|
|
|
}else{
|
|
|
|
WHYF("Could not resolve address on %s, this isn't a configured point to point link", context->interface->name);
|
2015-11-16 03:17:28 +00:00
|
|
|
context->flags|=DECODE_FLAG_INVALID_ADDRESS;
|
2013-07-16 05:46:07 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case OA_CODE_P2P_ME:
|
2013-08-08 05:50:31 +00:00
|
|
|
if (context->point_to_point_device){
|
|
|
|
*subscriber=context->point_to_point_device;
|
2013-07-16 05:46:07 +00:00
|
|
|
context->previous=*subscriber;
|
|
|
|
}else{
|
2015-11-16 03:17:28 +00:00
|
|
|
if ((context->flags & DECODE_FLAG_DONT_EXPLAIN) == 0){
|
|
|
|
// add the abbreviation you told me about
|
|
|
|
if (!context->please_explain){
|
|
|
|
context->please_explain = calloc(sizeof(struct overlay_frame),1);
|
|
|
|
if ((context->please_explain->payload = ob_new()) == NULL)
|
|
|
|
return -1;
|
|
|
|
ob_limitsize(context->please_explain->payload, MDP_MTU);
|
|
|
|
}
|
|
|
|
|
2016-01-11 23:58:30 +00:00
|
|
|
DEBUGF(subscriber, "Asking for explanation of YOU");
|
2015-11-16 03:17:28 +00:00
|
|
|
ob_append_byte(context->please_explain->payload, OA_CODE_P2P_YOU);
|
2013-09-17 02:32:26 +00:00
|
|
|
}
|
2015-11-16 03:17:28 +00:00
|
|
|
context->flags|=DECODE_FLAG_INVALID_ADDRESS;
|
2013-07-16 05:46:07 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
case OA_CODE_SELF:
|
2012-11-26 04:58:13 +00:00
|
|
|
if (!context->sender){
|
2016-01-11 23:58:30 +00:00
|
|
|
DEBUGF(subscriber, "Could not resolve address, sender has not been set");
|
2015-11-16 03:17:28 +00:00
|
|
|
context->flags|=DECODE_FLAG_INVALID_ADDRESS;
|
2012-09-19 04:46:40 +00:00
|
|
|
}else{
|
2012-11-22 22:34:42 +00:00
|
|
|
*subscriber=context->sender;
|
|
|
|
context->previous=context->sender;
|
2012-09-06 07:08:11 +00:00
|
|
|
}
|
2012-08-27 00:34:59 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case OA_CODE_PREVIOUS:
|
2012-11-26 04:58:13 +00:00
|
|
|
if (!context->previous){
|
2016-01-11 23:58:30 +00:00
|
|
|
DEBUGF(subscriber, "Unable to decode previous address");
|
2015-11-16 03:17:28 +00:00
|
|
|
context->flags|=DECODE_FLAG_INVALID_ADDRESS;
|
2012-11-26 04:58:13 +00:00
|
|
|
}else{
|
|
|
|
*subscriber=context->previous;
|
2012-09-06 07:08:11 +00:00
|
|
|
}
|
2012-08-27 00:34:59 +00:00
|
|
|
return 0;
|
2016-07-25 06:20:43 +00:00
|
|
|
|
|
|
|
case OA_CODE_SIGNKEY:
|
2017-11-22 07:08:01 +00:00
|
|
|
if (decode_sid_from_signkey(b, subscriber) == -1)
|
|
|
|
return -1;
|
|
|
|
context->previous = *subscriber;
|
|
|
|
return 0;
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
|
|
|
|
2012-11-26 04:58:13 +00:00
|
|
|
return find_subscr_buffer(context, b, len, subscriber);
|
2012-09-19 04:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// once we've finished parsing a packet, complete and send a please explain if required.
|
2013-11-25 12:10:14 +00:00
|
|
|
int send_please_explain(struct decode_context *context, struct subscriber *source, struct subscriber *destination)
|
|
|
|
{
|
2012-11-23 05:55:32 +00:00
|
|
|
IN();
|
2012-12-04 04:17:57 +00:00
|
|
|
struct overlay_frame *frame=context->please_explain;
|
2013-11-25 06:13:32 +00:00
|
|
|
if (frame == NULL)
|
2012-11-23 05:55:32 +00:00
|
|
|
RETURN(0);
|
2013-11-25 06:13:32 +00:00
|
|
|
assert(frame->payload != NULL);
|
2012-12-04 04:17:57 +00:00
|
|
|
frame->type = OF_TYPE_PLEASEEXPLAIN;
|
2012-09-19 04:46:40 +00:00
|
|
|
|
|
|
|
if (source)
|
2012-12-04 04:17:57 +00:00
|
|
|
frame->source = source;
|
2012-09-19 04:46:40 +00:00
|
|
|
else
|
2016-10-19 09:26:11 +00:00
|
|
|
frame->source = get_my_subscriber(1);
|
2012-09-19 04:46:40 +00:00
|
|
|
|
2013-02-07 04:46:07 +00:00
|
|
|
if (!context->sender)
|
|
|
|
frame->source_full=1;
|
|
|
|
|
2015-08-03 01:42:39 +00:00
|
|
|
frame->destination = destination;
|
2013-08-09 06:49:45 +00:00
|
|
|
if (destination){
|
2013-04-23 06:32:39 +00:00
|
|
|
frame->ttl = PAYLOAD_TTL_DEFAULT; // MAX?
|
2013-08-09 06:49:45 +00:00
|
|
|
frame->source_full=1;
|
2012-09-19 04:46:40 +00:00
|
|
|
}else{
|
2013-08-08 05:50:31 +00:00
|
|
|
// send both a broadcast & unicast response out the same interface this packet arrived on.
|
2012-12-04 04:17:57 +00:00
|
|
|
frame->ttl=1;// how will this work with olsr??
|
|
|
|
if (context->interface){
|
2015-03-30 00:57:37 +00:00
|
|
|
frame_add_destination(frame, NULL, context->interface->destination);
|
2013-08-08 05:50:31 +00:00
|
|
|
|
2013-12-09 07:15:47 +00:00
|
|
|
struct network_destination *dest = create_unicast_destination(&context->addr, context->interface);
|
2017-09-11 04:39:05 +00:00
|
|
|
if (dest){
|
2015-03-30 00:57:37 +00:00
|
|
|
frame_add_destination(frame, NULL, dest);
|
2017-09-11 04:39:05 +00:00
|
|
|
release_destination_ref(dest);
|
|
|
|
}
|
2013-08-08 05:50:31 +00:00
|
|
|
|
|
|
|
}else{
|
|
|
|
FATAL("This context doesn't have an interface?");
|
2012-12-04 04:17:57 +00:00
|
|
|
}
|
2012-09-19 04:46:40 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 04:17:57 +00:00
|
|
|
frame->queue=OQ_MESH_MANAGEMENT;
|
2013-11-25 06:13:32 +00:00
|
|
|
if (overlay_payload_enqueue(frame) != -1)
|
2012-11-23 05:55:32 +00:00
|
|
|
RETURN(0);
|
2012-12-04 04:17:57 +00:00
|
|
|
op_free(frame);
|
2012-11-26 04:22:49 +00:00
|
|
|
RETURN(-1);
|
2013-02-16 17:47:24 +00:00
|
|
|
OUT();
|
2012-09-19 04:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// process an incoming request for explanation of subscriber abbreviations
|
2013-11-25 12:10:14 +00:00
|
|
|
int process_explain(struct overlay_frame *frame)
|
|
|
|
{
|
2012-09-19 04:46:40 +00:00
|
|
|
struct overlay_buffer *b=frame->payload;
|
|
|
|
|
2012-11-23 05:38:23 +00:00
|
|
|
struct decode_context context;
|
|
|
|
bzero(&context, sizeof context);
|
2013-05-29 02:18:48 +00:00
|
|
|
context.sender = frame->source;
|
2013-08-08 05:50:31 +00:00
|
|
|
context.interface = frame->interface;
|
|
|
|
|
2012-11-27 04:02:10 +00:00
|
|
|
while(ob_remaining(b)>0){
|
2012-11-26 04:58:13 +00:00
|
|
|
int len = ob_get(b);
|
2016-09-27 00:58:46 +00:00
|
|
|
if (len<0)
|
|
|
|
return WHY("Badly formatted explain message");
|
2016-07-25 06:20:43 +00:00
|
|
|
switch (len){
|
|
|
|
case OA_CODE_P2P_YOU:
|
2016-09-13 04:44:21 +00:00
|
|
|
{
|
2016-10-19 09:26:11 +00:00
|
|
|
void *sid = get_my_subscriber(1);
|
2016-09-13 04:44:21 +00:00
|
|
|
add_explain_response(&sid, &context);
|
|
|
|
}
|
2016-07-25 06:20:43 +00:00
|
|
|
break;
|
|
|
|
case OA_CODE_SIGNKEY:
|
|
|
|
decode_sid_from_signkey(b, NULL);
|
|
|
|
break;
|
|
|
|
case SID_SIZE:
|
|
|
|
{
|
|
|
|
// This message is also used to inform people of previously unknown subscribers
|
|
|
|
// make sure we know this one
|
|
|
|
uint8_t *sid = ob_get_bytes_ptr(b, SID_SIZE);
|
|
|
|
if (!sid)
|
|
|
|
return WHY("Ran past end of buffer");
|
|
|
|
DEBUGF(subscriber, "Storing explain response for %s", alloca_tohex(sid, SID_SIZE));
|
|
|
|
find_subscriber(sid, SID_SIZE, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
2016-09-27 00:58:46 +00:00
|
|
|
if ((unsigned)len>SID_SIZE)
|
2016-07-25 06:20:43 +00:00
|
|
|
return WHY("Badly formatted explain message");
|
|
|
|
uint8_t *sid = ob_get_bytes_ptr(b, len);
|
|
|
|
// reply to the sender with all subscribers that match this abbreviation
|
|
|
|
DEBUGF(subscriber, "Sending explain responses for %s", alloca_tohex(sid, len));
|
2016-09-13 04:44:21 +00:00
|
|
|
tree_walk_prefix(&root, sid, len, add_explain_response, &context);
|
2016-07-25 06:20:43 +00:00
|
|
|
}
|
2012-09-19 04:46:40 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-25 12:10:14 +00:00
|
|
|
if (context.please_explain)
|
|
|
|
send_please_explain(&context, frame->destination, frame->source);
|
2012-09-19 04:46:40 +00:00
|
|
|
return 0;
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|