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"
|
2012-08-22 00:51:38 +00:00
|
|
|
#include "overlay_buffer.h"
|
2012-08-27 00:34:59 +00:00
|
|
|
#include "overlay_packet.h"
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
static int op_append_type(struct overlay_buffer *headers, struct overlay_frame *p)
|
2011-08-08 14:41:46 +00:00
|
|
|
{
|
2011-08-17 01:22:17 +00:00
|
|
|
unsigned char c[3];
|
2012-10-09 05:39:33 +00:00
|
|
|
// pack the QOS queue into the modifiers
|
|
|
|
int q_bits = p->queue;
|
|
|
|
if (q_bits>0) q_bits--;
|
|
|
|
if (q_bits>3) q_bits=3;
|
|
|
|
|
|
|
|
p->modifiers = (p->modifiers & ~OF_QUEUE_BITS)|q_bits;
|
|
|
|
|
2011-08-17 01:22:17 +00:00
|
|
|
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
|
|
|
|
2012-09-07 00:31:34 +00:00
|
|
|
int overlay_frame_append_payload(overlay_interface *interface, struct overlay_frame *p, struct subscriber *next_hop, struct overlay_buffer *b)
|
2011-08-17 01:22:17 +00:00
|
|
|
{
|
|
|
|
/* 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-08-22 00:51:38 +00:00
|
|
|
struct overlay_buffer *headers;
|
2012-07-12 01:06:41 +00:00
|
|
|
|
2012-08-22 00:51:38 +00:00
|
|
|
headers=ob_new();
|
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)
|
2012-08-27 00:34:59 +00:00
|
|
|
dump_payload(p,"append_payload stuffing into packet");
|
2012-04-15 20:36:43 +00:00
|
|
|
|
2011-08-08 14:41:46 +00:00
|
|
|
/* Build header */
|
|
|
|
|
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-10-10 05:52:31 +00:00
|
|
|
if (p->ttl>64)
|
|
|
|
p->ttl=64;
|
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-08-22 00:51:38 +00:00
|
|
|
int max_len=((SID_SIZE+3)*3+headers->position+p->payload->position);
|
2012-07-12 01:06:41 +00:00
|
|
|
if (debug&DEBUG_PACKETCONSTRUCTION)
|
|
|
|
DEBUGF("Appending RFS for max_len=%d\n",max_len);
|
|
|
|
ob_append_rfs(headers,max_len);
|
|
|
|
|
2012-08-22 00:51:38 +00:00
|
|
|
int addrs_start=headers->position;
|
2012-07-12 01:06:41 +00:00
|
|
|
|
|
|
|
/* Write out addresses as abbreviated as possible */
|
2012-08-27 00:34:59 +00:00
|
|
|
if (p->sendBroadcast){
|
|
|
|
overlay_broadcast_append(headers, &p->broadcast_id);
|
|
|
|
}else{
|
|
|
|
overlay_address_append(headers, next_hop);
|
|
|
|
}
|
|
|
|
if (p->destination)
|
|
|
|
overlay_address_append(headers,p->destination);
|
|
|
|
else
|
|
|
|
ob_append_byte(headers, OA_CODE_PREVIOUS);
|
2012-09-07 00:31:34 +00:00
|
|
|
|
|
|
|
if (p->source==my_subscriber){
|
|
|
|
overlay_address_append_self(interface, headers);
|
|
|
|
}else{
|
|
|
|
overlay_address_append(headers,p->source);
|
|
|
|
}
|
2012-07-12 01:06:41 +00:00
|
|
|
|
2012-08-22 00:51:38 +00:00
|
|
|
int addrs_len=headers->position-addrs_start;
|
|
|
|
int actual_len=addrs_len+p->payload->position;
|
2012-07-12 01:06:41 +00:00
|
|
|
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 */
|
2012-08-22 00:51:38 +00:00
|
|
|
if (ob_makespace(b,2+headers->position+p->payload->position)) {
|
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-08-22 00:51:38 +00:00
|
|
|
if (ob_append_bytes(b,headers->bytes,headers->position)) {
|
2012-07-12 01:06:41 +00:00
|
|
|
WHY("could not append header");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-08-22 00:51:38 +00:00
|
|
|
if (ob_append_bytes(b,p->payload->bytes,p->payload->position)) {
|
2012-07-12 01:06:41 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
int dump_payload(struct overlay_frame *p, char *message)
|
2012-04-15 20:36:43 +00:00
|
|
|
{
|
2012-08-01 08:24:02 +00:00
|
|
|
DEBUGF( "+++++\nFrame from %s to %s of type 0x%02x %s:",
|
2012-08-27 00:34:59 +00:00
|
|
|
alloca_tohex_sid(p->source->sid),
|
|
|
|
alloca_tohex_sid(p->destination->sid),p->type,
|
2012-04-15 20:36:43 +00:00
|
|
|
message?message:"");
|
2012-08-01 08:24:02 +00:00
|
|
|
if (p->payload)
|
2012-08-22 00:51:38 +00:00
|
|
|
dump("payload contents", &p->payload->bytes[0],p->payload->position);
|
2012-04-15 20:36:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-09 05:14:37 +00:00
|
|
|
int overlay_payload_enqueue(struct overlay_frame *p)
|
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-08-27 00:34:59 +00:00
|
|
|
|
|
|
|
if (!p) return WHY("Cannot queue NULL");
|
|
|
|
|
2012-09-19 00:20:29 +00:00
|
|
|
if (p->destination){
|
|
|
|
int r = subscriber_is_reachable(p->destination);
|
|
|
|
if (r == REACHABLE_SELF || r == REACHABLE_NONE)
|
|
|
|
return WHYF("Destination %s is unreachable (%d)", alloca_tohex_sid(p->destination->sid), r);
|
|
|
|
}
|
2012-08-27 00:34:59 +00:00
|
|
|
|
2012-10-09 05:39:33 +00:00
|
|
|
if (p->queue>=OQ_MAX)
|
2012-10-09 05:14:37 +00:00
|
|
|
return WHY("Invalid queue specified");
|
|
|
|
|
|
|
|
overlay_txqueue *queue = &overlay_tx[p->queue];
|
|
|
|
|
2012-08-22 00:51:38 +00:00
|
|
|
if (debug&DEBUG_PACKETTX)
|
|
|
|
DEBUGF("Enqueuing packet for %s* (q[%d]length = %d)",
|
2012-09-07 02:40:48 +00:00
|
|
|
p->destination?alloca_tohex(p->destination->sid, 7): alloca_tohex(p->broadcast_id.id,BROADCAST_LEN),
|
2012-10-09 05:14:37 +00:00
|
|
|
p->queue, queue->length);
|
2012-08-27 00:34:59 +00:00
|
|
|
|
|
|
|
if (p->payload && p->payload->position > p->payload->sizeLimit){
|
|
|
|
// HACK, maybe should be done in each caller
|
|
|
|
// set the size of the payload based on the position written
|
|
|
|
p->payload->sizeLimit=p->payload->position;
|
|
|
|
}
|
|
|
|
|
2012-10-09 05:14:37 +00:00
|
|
|
if (queue->length>=queue->maxLength)
|
|
|
|
return WHYF("Queue #%d congested (size = %d)",p->queue,queue->maxLength);
|
2012-03-22 06:03:25 +00:00
|
|
|
|
2012-09-28 01:58:01 +00:00
|
|
|
if (p->send_copies<=0)
|
|
|
|
p->send_copies=1;
|
|
|
|
else if(p->send_copies>5)
|
|
|
|
return WHY("Too many copies requested");
|
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
if (!p->destination){
|
|
|
|
int i;
|
2012-08-30 00:04:52 +00:00
|
|
|
int drop=1;
|
|
|
|
|
2012-08-31 05:28:40 +00:00
|
|
|
// hook to allow for flooding via olsr
|
|
|
|
olsr_send(p);
|
|
|
|
|
2012-08-30 00:04:52 +00:00
|
|
|
// make sure there is an interface up that allows broadcasts
|
|
|
|
for(i=0;i<OVERLAY_MAX_INTERFACES;i++){
|
|
|
|
if (overlay_interfaces[i].state==INTERFACE_STATE_UP
|
|
|
|
&& overlay_interfaces[i].send_broadcasts){
|
|
|
|
p->broadcast_sent_via[i]=0;
|
|
|
|
drop=0;
|
|
|
|
}else
|
|
|
|
p->broadcast_sent_via[i]=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// just drop it now
|
|
|
|
if (drop)
|
|
|
|
return -1;
|
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
p->sendBroadcast=1;
|
|
|
|
}
|
|
|
|
|
2012-10-09 05:14:37 +00:00
|
|
|
struct overlay_frame *l=queue->last;
|
2011-09-05 03:19:37 +00:00
|
|
|
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
|
|
|
|
2012-10-09 05:14:37 +00:00
|
|
|
queue->last=p;
|
|
|
|
if (!queue->first) queue->first=p;
|
|
|
|
queue->length++;
|
2012-07-12 01:06:41 +00:00
|
|
|
|
2012-10-09 05:14:37 +00:00
|
|
|
overlay_update_queue_schedule(queue, p);
|
2012-07-12 01:11:47 +00:00
|
|
|
|
2012-10-09 05:14:37 +00:00
|
|
|
if (0) dump_queue("after",p->queue);
|
2012-03-22 06:03:25 +00:00
|
|
|
|
2011-09-05 03:19:37 +00:00
|
|
|
return 0;
|
2011-08-08 14:41:46 +00:00
|
|
|
}
|
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
int op_free(struct 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-08-27 00:34:59 +00:00
|
|
|
struct overlay_frame *op_dup(struct overlay_frame *in)
|
2012-04-14 01:26:03 +00:00
|
|
|
{
|
|
|
|
if (!in) return NULL;
|
|
|
|
|
|
|
|
/* clone the frame */
|
2012-08-27 00:34:59 +00:00
|
|
|
struct overlay_frame *out=malloc(sizeof(struct overlay_frame));
|
2012-10-15 05:06:36 +00:00
|
|
|
if (!out) { WHY("malloc() failed"); return NULL; }
|
2012-04-14 01:26:03 +00:00
|
|
|
|
|
|
|
/* copy main data structure */
|
2012-08-27 00:34:59 +00:00
|
|
|
bcopy(in,out,sizeof(struct overlay_frame));
|
2012-03-21 02:27:24 +00:00
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
if (in->payload)
|
|
|
|
out->payload=ob_dup(in->payload);
|
2012-04-14 01:26:03 +00:00
|
|
|
return out;
|
|
|
|
}
|