2011-12-21 09:55:05 +00:00
|
|
|
/*
|
|
|
|
Serval Distributed Numbering Architecture (DNA)
|
|
|
|
Copyright (C) 2010 Paul Gardner-Stephen
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2012-02-23 02:15:42 +00:00
|
|
|
#include "serval.h"
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2011-08-17 01:22:17 +00:00
|
|
|
int overlay_payload_verify(overlay_frame *p)
|
2011-08-08 14:41:46 +00:00
|
|
|
{
|
|
|
|
/* Make sure that an incoming payload has a valid signature from the sender.
|
|
|
|
This is used to prevent spoofing */
|
|
|
|
|
|
|
|
return WHY("function not implemented");
|
|
|
|
}
|
|
|
|
|
2011-08-17 01:22:17 +00:00
|
|
|
int op_append_type(overlay_buffer *headers,overlay_frame *p)
|
2011-08-08 14:41:46 +00:00
|
|
|
{
|
2011-08-17 01:22:17 +00:00
|
|
|
unsigned char c[3];
|
|
|
|
switch(p->type&OF_TYPE_FLAG_BITS)
|
|
|
|
{
|
|
|
|
case OF_TYPE_FLAG_NORMAL:
|
|
|
|
c[0]=p->type|p->modifiers;
|
2012-08-01 08:24:02 +00:00
|
|
|
if (debug&DEBUG_PACKETFORMATS) DEBUGF("type resolves to %02x",c[0]);
|
2011-08-17 01:22:17 +00:00
|
|
|
if (ob_append_bytes(headers,c,1)) return -1;
|
|
|
|
break;
|
|
|
|
case OF_TYPE_FLAG_E12:
|
|
|
|
c[0]=(p->type&OF_MODIFIER_BITS)|OF_TYPE_EXTENDED12;
|
|
|
|
c[1]=(p->type>>4)&0xff;
|
2012-08-01 08:24:02 +00:00
|
|
|
if (debug&DEBUG_PACKETFORMATS) DEBUGF("type resolves to %02x%02x",c[0],c[1]);
|
2011-08-17 01:22:17 +00:00
|
|
|
if (ob_append_bytes(headers,c,2)) return -1;
|
|
|
|
break;
|
|
|
|
case OF_TYPE_FLAG_E20:
|
|
|
|
c[0]=(p->type&OF_MODIFIER_BITS)|OF_TYPE_EXTENDED20;
|
|
|
|
c[1]=(p->type>>4)&0xff;
|
|
|
|
c[2]=(p->type>>12)&0xff;
|
2012-08-01 08:24:02 +00:00
|
|
|
if (debug&DEBUG_PACKETFORMATS) DEBUGF("type resolves to %02x%02x%02x",c[0],c[1],c[2]);
|
2011-08-17 01:22:17 +00:00
|
|
|
if (ob_append_bytes(headers,c,3)) return -1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Don't know this type of frame */
|
|
|
|
WHY("Asked for format frame with unknown TYPE_FLAG bits");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2011-08-17 01:22:17 +00:00
|
|
|
int overlay_frame_package_fmt1(overlay_frame *p,overlay_buffer *b)
|
|
|
|
{
|
|
|
|
/* Convert a payload (frame) structure into a series of bytes.
|
|
|
|
Assumes that any encryption etc has already been done.
|
|
|
|
Will pick a next hop if one has not been chosen.
|
|
|
|
*/
|
|
|
|
|
2012-07-12 01:06:41 +00:00
|
|
|
int i;
|
|
|
|
overlay_buffer *headers;
|
|
|
|
|
|
|
|
headers=ob_new(256);
|
2011-08-08 14:41:46 +00:00
|
|
|
|
|
|
|
if (!headers) return WHY("could not allocate overlay buffer for headers");
|
|
|
|
|
2012-07-12 01:06:41 +00:00
|
|
|
ob_checkpoint(b);
|
|
|
|
|
2012-04-15 20:36:43 +00:00
|
|
|
if (debug&DEBUG_PACKETCONSTRUCTION)
|
|
|
|
dump_payload(p,"package_fmt1 stuffing into packet");
|
|
|
|
|
2011-08-08 14:41:46 +00:00
|
|
|
/* Build header */
|
|
|
|
|
2011-08-17 01:22:17 +00:00
|
|
|
if (p->nexthop_address_status!=OA_RESOLVED) {
|
2012-04-29 02:31:56 +00:00
|
|
|
if (0) WHYF("next hop is NOT resolved for packet to %s",
|
2012-07-03 08:29:30 +00:00
|
|
|
alloca_tohex_sid(p->destination));
|
2012-03-26 21:52:35 +00:00
|
|
|
if (overlay_address_is_broadcast(p->destination)) {
|
|
|
|
/* Broadcast frames are broadcast rather than unicast to next hop.
|
|
|
|
Just check if the broadcast frame should be dropped first. */
|
2012-07-12 01:06:41 +00:00
|
|
|
if (overlay_broadcast_drop_check(p->destination)){
|
|
|
|
WHY("This broadcast packet ID has been seen recently");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2012-04-13 21:28:36 +00:00
|
|
|
/* Copy the broadcast address exactly so that we preserve the BPI */
|
|
|
|
for(i=0;i<SID_SIZE;i++) p->nexthop[i]=p->destination[i];
|
2012-03-26 21:52:35 +00:00
|
|
|
p->nexthop_address_status=OA_RESOLVED;
|
|
|
|
} else {
|
2012-07-12 01:06:41 +00:00
|
|
|
if (overlay_get_nexthop((unsigned char *)p->destination,p->nexthop,&p->nexthop_interface)) {
|
|
|
|
WHY("could not determine next hop address for payload");
|
|
|
|
goto cleanup;
|
2012-03-26 21:52:35 +00:00
|
|
|
}
|
|
|
|
else p->nexthop_address_status=OA_RESOLVED;
|
2012-03-22 20:36:57 +00:00
|
|
|
}
|
2012-04-29 02:26:47 +00:00
|
|
|
} else {
|
|
|
|
if (0) WHYF("next hop IS resolved for packet to %s",
|
2012-07-03 08:29:30 +00:00
|
|
|
alloca_tohex_sid(p->destination));
|
2011-08-17 01:22:17 +00:00
|
|
|
}
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2012-04-29 02:26:47 +00:00
|
|
|
|
2011-09-07 12:20:20 +00:00
|
|
|
if (p->source[0]<0x10) {
|
2011-08-08 14:41:46 +00:00
|
|
|
// Make sure that addresses do not overload the special address spaces of 0x00*-0x0f*
|
2012-07-12 01:06:41 +00:00
|
|
|
WHY("packet source address begins with reserved value 0x00-0x0f");
|
|
|
|
goto cleanup;
|
2011-09-07 12:20:20 +00:00
|
|
|
}
|
|
|
|
if (p->destination[0]<0x10) {
|
|
|
|
// Make sure that addresses do not overload the special address spaces of 0x00*-0x0f*
|
2012-07-12 01:06:41 +00:00
|
|
|
WHY("packet destination address begins with reserved value 0x00-0x0f");
|
|
|
|
goto cleanup;
|
2011-09-07 12:20:20 +00:00
|
|
|
}
|
|
|
|
if (p->nexthop[0]<0x10) {
|
|
|
|
// Make sure that addresses do not overload the special address spaces of 0x00*-0x0f*
|
2012-07-12 01:06:41 +00:00
|
|
|
WHY("packet nexthop address begins with reserved value 0x00-0x0f");
|
|
|
|
goto cleanup;
|
2011-08-08 14:41:46 +00:00
|
|
|
}
|
2011-08-17 01:22:17 +00:00
|
|
|
|
2012-03-22 20:36:57 +00:00
|
|
|
/* Write fields into binary structure in correct order */
|
2011-08-17 01:22:17 +00:00
|
|
|
|
|
|
|
/* Write out type field byte(s) */
|
2012-07-12 01:06:41 +00:00
|
|
|
if (op_append_type(headers,p))
|
|
|
|
goto cleanup;
|
2011-08-17 01:22:17 +00:00
|
|
|
|
|
|
|
/* Write out TTL */
|
2012-07-12 01:06:41 +00:00
|
|
|
if (ob_append_byte(headers,p->ttl))
|
|
|
|
goto cleanup;
|
2011-08-17 01:22:17 +00:00
|
|
|
|
|
|
|
/* Length. This is the fun part, because we cannot calculate how many bytes we need until
|
|
|
|
we have abbreviated the addresses, and the length encoding we use varies according to the
|
|
|
|
length encoded. The simple option of running the abbreviations twice won't work because
|
|
|
|
we rely on context for abbreviating the addresses. So we write it initially and then patch it
|
|
|
|
after.
|
|
|
|
*/
|
2012-07-12 01:06:41 +00:00
|
|
|
int max_len=((SID_SIZE+3)*3+headers->length+p->payload->length);
|
|
|
|
if (debug&DEBUG_PACKETCONSTRUCTION)
|
|
|
|
DEBUGF("Appending RFS for max_len=%d\n",max_len);
|
|
|
|
ob_append_rfs(headers,max_len);
|
|
|
|
|
|
|
|
int addrs_start=headers->length;
|
|
|
|
|
|
|
|
/* Write out addresses as abbreviated as possible */
|
|
|
|
overlay_abbreviate_append_address(headers,p->nexthop);
|
|
|
|
overlay_abbreviate_append_address(headers,p->destination);
|
|
|
|
overlay_abbreviate_append_address(headers,p->source);
|
|
|
|
|
|
|
|
int addrs_len=headers->length-addrs_start;
|
|
|
|
int actual_len=addrs_len+p->payload->length;
|
|
|
|
if (debug&DEBUG_PACKETCONSTRUCTION)
|
|
|
|
DEBUGF("Patching RFS for actual_len=%d\n",actual_len);
|
|
|
|
ob_patch_rfs(headers,actual_len);
|
2011-08-08 14:41:46 +00:00
|
|
|
|
|
|
|
/* Write payload format plus total length of header bits */
|
2011-09-05 02:49:53 +00:00
|
|
|
if (ob_makespace(b,2+headers->length+p->payload->length)) {
|
2011-08-08 14:41:46 +00:00
|
|
|
/* Not enough space free in output buffer */
|
2012-01-10 09:57:36 +00:00
|
|
|
if (debug&DEBUG_PACKETFORMATS)
|
2012-07-12 01:06:41 +00:00
|
|
|
DEBUGF("Could not make enough space free in output buffer");
|
|
|
|
goto cleanup;
|
2011-08-08 14:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Package up headers and payload */
|
2012-07-12 01:06:41 +00:00
|
|
|
if (ob_append_bytes(b,headers->bytes,headers->length)) {
|
|
|
|
WHY("could not append header");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (ob_append_bytes(b,p->payload->bytes,p->payload->length)) {
|
|
|
|
WHY("could not append payload");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-09-07 01:56:26 +00:00
|
|
|
|
2011-08-08 14:41:46 +00:00
|
|
|
ob_free(headers);
|
2012-07-12 01:06:41 +00:00
|
|
|
return 0;
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2012-07-12 01:06:41 +00:00
|
|
|
cleanup:
|
|
|
|
ob_free(headers);
|
|
|
|
ob_rewind(b);
|
|
|
|
return -1;
|
2011-08-08 14:41:46 +00:00
|
|
|
}
|
|
|
|
|
2011-08-17 01:22:17 +00:00
|
|
|
overlay_buffer *overlay_payload_unpackage(overlay_frame *b) {
|
2011-08-08 14:41:46 +00:00
|
|
|
/* Extract the payload at the current location in the buffer. */
|
|
|
|
|
|
|
|
WHY("not implemented");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-22 06:03:25 +00:00
|
|
|
int dump_queue(char *msg,int q)
|
|
|
|
{
|
|
|
|
overlay_txqueue *qq=&overlay_tx[q];
|
2012-08-01 08:24:02 +00:00
|
|
|
DEBUGF("Contents of TX queue #%d (%s):",q,msg);
|
|
|
|
DEBUGF(" length=%d, maxLength=%d",qq->length,qq->maxLength);
|
2012-03-22 06:03:25 +00:00
|
|
|
struct overlay_frame *f=qq->first,*l=qq->last;
|
2012-08-01 08:24:02 +00:00
|
|
|
DEBUGF(" head of queue = %p, tail of queue = %p", f, l);
|
2012-03-22 06:03:25 +00:00
|
|
|
struct overlay_frame *n=f;
|
|
|
|
int count=0;
|
|
|
|
while(n) {
|
2012-08-01 08:24:02 +00:00
|
|
|
DEBUGF(" queue entry #%d : prev=%p, next=%p", count,n->prev,n->next);
|
|
|
|
if (n==n->next) {
|
|
|
|
WHY(" ERROR: loop in queue");
|
2012-03-22 06:03:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
n=n->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2012-04-15 20:36:43 +00:00
|
|
|
|
|
|
|
int dump_payload(overlay_frame *p,char *message)
|
|
|
|
{
|
2012-08-01 08:24:02 +00:00
|
|
|
DEBUGF( "+++++\nFrame from %s to %s of type 0x%02x %s:",
|
2012-07-03 08:29:30 +00:00
|
|
|
alloca_tohex_sid(p->source),
|
|
|
|
alloca_tohex_sid(p->destination),p->type,
|
2012-04-15 20:36:43 +00:00
|
|
|
message?message:"");
|
2012-08-01 08:24:02 +00:00
|
|
|
DEBUGF(" next hop is %s",alloca_tohex_sid(p->nexthop));
|
|
|
|
if (p->payload)
|
|
|
|
dump("payload contents", &p->payload->bytes[0],p->payload->length);
|
2012-04-15 20:36:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-28 08:39:58 +00:00
|
|
|
int overlay_payload_enqueue(int q,overlay_frame *p,int forceBroadcastP)
|
2011-08-08 14:41:46 +00:00
|
|
|
{
|
|
|
|
/* Add payload p to queue q.
|
2011-09-05 03:19:37 +00:00
|
|
|
|
|
|
|
Queues get scanned from first to last, so we should append new entries
|
|
|
|
on the end of the queue.
|
|
|
|
|
|
|
|
Complain if there are too many frames in the queue.
|
2011-08-08 14:41:46 +00:00
|
|
|
*/
|
2012-06-25 05:16:55 +00:00
|
|
|
if (0)
|
|
|
|
WHYF("Enqueuing packet for %s* (q[%d]length = %d)",
|
2012-07-04 01:00:46 +00:00
|
|
|
alloca_tohex(p->destination, 7),
|
2012-06-25 05:16:55 +00:00
|
|
|
q,overlay_tx[q].length);
|
2012-05-06 23:01:53 +00:00
|
|
|
|
2011-09-05 03:19:37 +00:00
|
|
|
if (q<0||q>=OQ_MAX) return WHY("Invalid queue specified");
|
|
|
|
if (!p) return WHY("Cannot queue NULL");
|
|
|
|
|
2012-04-15 20:36:43 +00:00
|
|
|
if (0) dump_payload(p,"queued for delivery");
|
|
|
|
|
2012-06-22 08:00:21 +00:00
|
|
|
if (overlay_tx[q].length>=overlay_tx[q].maxLength)
|
|
|
|
return WHYF("Queue #%d congested (size = %d)",q,overlay_tx[q].maxLength);
|
2012-03-22 06:03:25 +00:00
|
|
|
|
2012-04-13 17:27:49 +00:00
|
|
|
if (0) dump_queue("before",q);
|
2011-09-05 03:19:37 +00:00
|
|
|
|
2012-04-13 18:36:08 +00:00
|
|
|
/* If the frame is broadcast, then mark it correctly so that it can be sent
|
|
|
|
via all interfaces. */
|
2012-04-28 08:39:58 +00:00
|
|
|
if (overlay_address_is_broadcast(p->destination)||forceBroadcastP)
|
2012-04-13 18:36:08 +00:00
|
|
|
{
|
|
|
|
p->isBroadcast=1;
|
|
|
|
int i;
|
|
|
|
for(i=0;i<OVERLAY_MAX_INTERFACES;i++) p->broadcast_sent_via[i]=0;
|
|
|
|
}
|
|
|
|
else p->isBroadcast=0;
|
|
|
|
|
2011-09-05 03:19:37 +00:00
|
|
|
overlay_frame *l=overlay_tx[q].last;
|
|
|
|
if (l) l->next=p;
|
|
|
|
p->prev=l;
|
|
|
|
p->next=NULL;
|
2012-07-30 07:52:38 +00:00
|
|
|
p->enqueued_at=gettime_ms();
|
2011-09-05 03:49:00 +00:00
|
|
|
|
2011-09-05 03:19:37 +00:00
|
|
|
overlay_tx[q].last=p;
|
|
|
|
if (!overlay_tx[q].first) overlay_tx[q].first=p;
|
|
|
|
overlay_tx[q].length++;
|
2012-07-12 01:06:41 +00:00
|
|
|
|
2012-07-12 01:11:47 +00:00
|
|
|
overlay_update_queue_schedule(&overlay_tx[q], p);
|
|
|
|
|
2012-04-13 17:27:49 +00:00
|
|
|
if (0) dump_queue("after",q);
|
2012-03-22 06:03:25 +00:00
|
|
|
|
2012-07-12 01:06:41 +00:00
|
|
|
if (q==OQ_ISOCHRONOUS_VOICE&&(!forceBroadcastP)) {
|
|
|
|
// Send a packet now
|
2012-07-12 01:11:47 +00:00
|
|
|
overlay_send_packet(NULL);
|
2012-07-12 01:06:41 +00:00
|
|
|
}
|
|
|
|
|
2011-09-05 03:19:37 +00:00
|
|
|
return 0;
|
2011-08-08 14:41:46 +00:00
|
|
|
}
|
|
|
|
|
2011-08-17 01:22:17 +00:00
|
|
|
int op_free(overlay_frame *p)
|
2011-08-08 14:41:46 +00:00
|
|
|
{
|
|
|
|
if (!p) return WHY("Asked to free NULL");
|
|
|
|
if (p->prev&&p->prev->next==p) return WHY("p->prev->next still points here");
|
|
|
|
if (p->next&&p->next->prev==p) return WHY("p->next->prev still points here");
|
|
|
|
p->prev=NULL;
|
|
|
|
p->next=NULL;
|
2011-09-05 02:49:53 +00:00
|
|
|
if (p->payload) ob_free(p->payload);
|
2011-08-08 14:41:46 +00:00
|
|
|
p->payload=NULL;
|
|
|
|
free(p);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-09-05 02:49:53 +00:00
|
|
|
|
2012-04-14 01:26:03 +00:00
|
|
|
overlay_frame *op_dup(overlay_frame *in)
|
|
|
|
{
|
|
|
|
if (!in) return NULL;
|
|
|
|
|
|
|
|
/* clone the frame */
|
|
|
|
overlay_frame *out=malloc(sizeof(overlay_frame));
|
2012-05-10 03:23:57 +00:00
|
|
|
if (!out) return WHYNULL("malloc() failed");
|
2012-04-14 01:26:03 +00:00
|
|
|
|
|
|
|
/* copy main data structure */
|
|
|
|
bcopy(in,out,sizeof(overlay_frame));
|
|
|
|
out->payload=ob_new(in->payload->length);
|
|
|
|
if (!out->payload) {
|
|
|
|
free(out);
|
2012-05-10 03:23:57 +00:00
|
|
|
return WHYNULL("ob_new() failed");
|
2012-04-14 01:26:03 +00:00
|
|
|
}
|
|
|
|
if (ob_append_bytes(out->payload,&in->payload->bytes[0],in->payload->length))
|
|
|
|
{
|
|
|
|
op_free(out);
|
2012-05-10 03:23:57 +00:00
|
|
|
return WHYNULL("could not duplicate payload bytes");
|
2012-04-14 01:26:03 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2011-09-05 02:49:53 +00:00
|
|
|
int overlay_frame_set_neighbour_as_source(overlay_frame *f,overlay_neighbour *n)
|
|
|
|
{
|
|
|
|
if (!n) return WHY("Neighbour was null");
|
2011-09-05 06:25:59 +00:00
|
|
|
bcopy(n->node->sid,f->source,SID_SIZE);
|
2011-09-05 03:04:54 +00:00
|
|
|
return 0;
|
2011-09-05 02:49:53 +00:00
|
|
|
}
|
|
|
|
|
2011-09-05 06:25:59 +00:00
|
|
|
int overlay_frame_set_neighbour_as_destination(overlay_frame *f,overlay_neighbour *n)
|
|
|
|
{
|
|
|
|
if (!n) return WHY("Neighbour was null");
|
|
|
|
bcopy(n->node->sid,f->destination,SID_SIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-21 02:27:24 +00:00
|
|
|
int overlay_frame_set_broadcast_as_destination(overlay_frame *f)
|
|
|
|
{
|
|
|
|
overlay_broadcast_generate_address(f->destination);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-05 02:49:53 +00:00
|
|
|
unsigned char *overlay_get_my_sid()
|
|
|
|
{
|
|
|
|
/* Make sure we can find our SID */
|
2012-04-12 23:55:03 +00:00
|
|
|
int kp;
|
|
|
|
if (!keyring)
|
|
|
|
{ WHY("keyring is null"); return NULL; }
|
|
|
|
if (!keyring->context_count)
|
|
|
|
{ WHY("No context zero in keyring"); return NULL; }
|
|
|
|
if (!keyring->contexts[0]->identity_count)
|
|
|
|
{ WHY("No identity in keyring context zero"); return NULL; }
|
|
|
|
|
|
|
|
for(kp=0;kp<keyring->contexts[0]->identities[0]->keypair_count;kp++)
|
|
|
|
if (keyring->contexts[0]->identities[0]->keypairs[kp]->type==KEYTYPE_CRYPTOBOX)
|
|
|
|
return keyring->contexts[0]->identities[0]->keypairs[kp]->public_key;
|
|
|
|
|
|
|
|
WHY("Could not find first entry in HLR"); return NULL;
|
2011-09-05 02:49:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int overlay_frame_set_me_as_source(overlay_frame *f)
|
|
|
|
{
|
|
|
|
unsigned char *sid=overlay_get_my_sid();
|
|
|
|
if (!sid) return WHY("overlay_get_my_sid() failed.");
|
|
|
|
bcopy(sid,f->source,SID_SIZE);
|
2011-09-05 03:04:54 +00:00
|
|
|
return 0;
|
2011-09-05 02:49:53 +00:00
|
|
|
}
|