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-12-11 05:29:46 +00:00
|
|
|
#include "conf.h"
|
2012-11-07 06:12:45 +00:00
|
|
|
#include "str.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
|
|
|
|
2013-05-15 06:26:43 +00:00
|
|
|
static int overlay_frame_build_header(struct decode_context *context, struct overlay_buffer *buff,
|
|
|
|
int queue, int type, int modifiers, int ttl, int previous_seq,
|
2012-11-23 05:55:32 +00:00
|
|
|
struct broadcast *broadcast, struct subscriber *next_hop,
|
2013-04-23 06:32:39 +00:00
|
|
|
struct subscriber *destination, struct subscriber *source)
|
|
|
|
{
|
|
|
|
if (ttl < 0 || ttl > PAYLOAD_TTL_MAX)
|
|
|
|
return WHYF("invalid ttl=%d", ttl);
|
|
|
|
|
2012-11-26 04:22:49 +00:00
|
|
|
int flags = modifiers & (PAYLOAD_FLAG_CIPHERED | PAYLOAD_FLAG_SIGNED);
|
2012-11-23 05:55:32 +00:00
|
|
|
|
2012-11-26 04:22:49 +00:00
|
|
|
if (ttl==1 && !broadcast)
|
|
|
|
flags |= PAYLOAD_FLAG_ONE_HOP;
|
|
|
|
if (destination && destination==next_hop)
|
|
|
|
flags |= PAYLOAD_FLAG_ONE_HOP;
|
2012-11-23 05:55:32 +00:00
|
|
|
|
2012-11-26 04:22:49 +00:00
|
|
|
if (source == context->sender)
|
|
|
|
flags |= PAYLOAD_FLAG_SENDER_SAME;
|
2012-11-23 05:55:32 +00:00
|
|
|
|
2012-11-26 04:22:49 +00:00
|
|
|
if (!destination)
|
|
|
|
flags |= PAYLOAD_FLAG_TO_BROADCAST;
|
2012-11-23 05:55:32 +00:00
|
|
|
|
2012-11-27 00:54:03 +00:00
|
|
|
if (type!=OF_TYPE_DATA)
|
|
|
|
flags |= PAYLOAD_FLAG_LEGACY_TYPE;
|
2013-05-15 06:26:43 +00:00
|
|
|
if (previous_seq>=0)
|
|
|
|
flags |= PAYLOAD_FLAG_DUPLICATE;
|
2012-11-27 00:54:03 +00:00
|
|
|
|
2012-11-26 04:22:49 +00:00
|
|
|
if (ob_append_byte(buff, flags)) return -1;
|
|
|
|
|
|
|
|
if (!(flags & PAYLOAD_FLAG_SENDER_SAME)){
|
|
|
|
if (overlay_address_append(context, buff, source)) return -1;
|
2012-11-23 05:55:32 +00:00
|
|
|
}
|
|
|
|
|
2012-11-26 04:22:49 +00:00
|
|
|
if (flags & PAYLOAD_FLAG_TO_BROADCAST){
|
|
|
|
if (!(flags & PAYLOAD_FLAG_ONE_HOP)){
|
2012-11-26 04:58:13 +00:00
|
|
|
if (overlay_broadcast_append(buff, broadcast)) return -1;
|
2012-11-26 04:22:49 +00:00
|
|
|
}
|
2012-11-23 05:55:32 +00:00
|
|
|
}else{
|
2012-11-26 04:22:49 +00:00
|
|
|
if (overlay_address_append(context, buff, destination)) return -1;
|
|
|
|
if (!(flags & PAYLOAD_FLAG_ONE_HOP)){
|
|
|
|
if (overlay_address_append(context, buff, next_hop)) return -1;
|
|
|
|
}
|
2012-11-23 05:55:32 +00:00
|
|
|
}
|
|
|
|
|
2012-11-26 04:22:49 +00:00
|
|
|
if (!(flags & PAYLOAD_FLAG_ONE_HOP)){
|
|
|
|
if (ob_append_byte(buff, ttl | ((queue&3)<<5))) return -1;
|
|
|
|
}
|
2012-11-27 00:54:03 +00:00
|
|
|
|
|
|
|
if (flags & PAYLOAD_FLAG_LEGACY_TYPE){
|
|
|
|
if (ob_append_byte(buff, type)) return -1;
|
|
|
|
}
|
2013-05-15 06:26:43 +00:00
|
|
|
|
|
|
|
if (flags & PAYLOAD_FLAG_DUPLICATE){
|
|
|
|
if (ob_append_byte(buff, previous_seq)) return -1;
|
|
|
|
}
|
2012-11-23 05:55:32 +00:00
|
|
|
|
|
|
|
return 0;
|
2012-11-21 02:22:39 +00:00
|
|
|
}
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2012-11-22 22:34:42 +00:00
|
|
|
int overlay_frame_append_payload(struct decode_context *context, overlay_interface *interface,
|
2012-12-03 03:14:31 +00:00
|
|
|
struct overlay_frame *p, 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-07-12 01:06:41 +00:00
|
|
|
ob_checkpoint(b);
|
|
|
|
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.packetconstruction){
|
2012-11-21 02:22:39 +00:00
|
|
|
DEBUGF( "+++++\nFrame from %s to %s of type 0x%02x %s:",
|
|
|
|
alloca_tohex_sid(p->source->sid),
|
|
|
|
alloca_tohex_sid(p->destination->sid),p->type,
|
|
|
|
"append_payload stuffing into packet");
|
|
|
|
if (p->payload)
|
|
|
|
dump("payload contents", &p->payload->bytes[0],p->payload->position);
|
|
|
|
}
|
|
|
|
|
2012-12-19 02:06:28 +00:00
|
|
|
struct broadcast *broadcast=NULL;
|
|
|
|
if ((!p->destination) && !is_all_matching(p->broadcast_id.id,BROADCAST_LEN,0)){
|
|
|
|
broadcast = &p->broadcast_id;
|
|
|
|
}
|
2013-05-20 03:53:35 +00:00
|
|
|
int i = interface - overlay_interfaces;
|
2013-02-07 04:46:07 +00:00
|
|
|
if (overlay_frame_build_header(context, b,
|
2013-05-20 03:53:35 +00:00
|
|
|
p->queue, p->type, p->modifiers, p->ttl, p->interface_sent_sequence[i],
|
2012-12-19 02:06:28 +00:00
|
|
|
broadcast, p->next_hop,
|
2012-11-23 05:55:32 +00:00
|
|
|
p->destination, p->source))
|
|
|
|
goto cleanup;
|
2012-07-12 01:06:41 +00:00
|
|
|
|
2013-02-07 04:46:07 +00:00
|
|
|
if (ob_append_ui16(b, ob_position(p->payload)))
|
|
|
|
goto cleanup;
|
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
if (ob_append_bytes(b, ob_ptr(p->payload), ob_position(p->payload))) {
|
2012-07-12 01:06:41 +00:00
|
|
|
WHY("could not append payload");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-02-07 04:46:07 +00:00
|
|
|
|
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_rewind(b);
|
|
|
|
return -1;
|
2011-08-08 14:41:46 +00:00
|
|
|
}
|
2013-02-14 03:48:56 +00:00
|
|
|
|
|
|
|
int single_packet_encapsulation(struct overlay_buffer *b, struct overlay_frame *frame){
|
|
|
|
overlay_interface *interface=frame->interface;
|
|
|
|
int interface_number = interface - overlay_interfaces;
|
|
|
|
struct decode_context context;
|
|
|
|
bzero(&context, sizeof(struct decode_context));
|
|
|
|
|
|
|
|
if (frame->source_full)
|
|
|
|
my_subscriber->send_full=1;
|
2013-05-03 06:53:23 +00:00
|
|
|
int seq = interface->sequence_number++;
|
|
|
|
if (overlay_packet_init_header(ENCAP_SINGLE, &context, b, NULL, 0, interface_number, seq))
|
2013-02-14 03:48:56 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
struct broadcast *broadcast=NULL;
|
|
|
|
if ((!frame->destination) && !is_all_matching(frame->broadcast_id.id,BROADCAST_LEN,0))
|
|
|
|
broadcast = &frame->broadcast_id;
|
|
|
|
|
|
|
|
if (overlay_frame_build_header(&context, b,
|
|
|
|
frame->queue, frame->type,
|
2013-05-15 06:26:43 +00:00
|
|
|
frame->modifiers, frame->ttl, -1,
|
2013-02-14 03:48:56 +00:00
|
|
|
broadcast, frame->next_hop,
|
|
|
|
frame->destination, frame->source))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (ob_append_buffer(b, frame->payload))
|
|
|
|
return -1;
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|