forked from opentechinstitute/serval-dna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlay_packetformats.c
501 lines (413 loc) · 16.7 KB
/
overlay_packetformats.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
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.
*/
#include "serval.h"
#include "conf.h"
#include "str.h"
#include "strbuf.h"
#include "overlay_buffer.h"
#include "overlay_packet.h"
struct sockaddr_in loopback;
#define PACKET_UNICAST (1<<0)
#define PACKET_INTERFACE (1<<1)
#define PACKET_SEQ (1<<2)
#define SUPPORTED_PACKET_VERSION 1
int overlay_packet_init_header(int packet_version, int encapsulation,
struct decode_context *context, struct overlay_buffer *buff,
struct subscriber *destination,
char unicast, char interface, int seq){
if (packet_version <0 || packet_version > SUPPORTED_PACKET_VERSION)
return WHY("Invalid packet version");
if (encapsulation !=ENCAP_OVERLAY && encapsulation !=ENCAP_SINGLE)
return WHY("Invalid packet encapsulation");
if (ob_append_byte(buff, packet_version))
return -1;
if (ob_append_byte(buff, encapsulation))
return -1;
if (overlay_address_append(context, buff, my_subscriber))
return -1;
context->sender = my_subscriber;
int flags=0;
if (unicast)
flags |= PACKET_UNICAST;
if (interface)
flags |= PACKET_INTERFACE;
if (seq>=0)
flags |= PACKET_SEQ;
ob_append_byte(buff,flags);
if (flags & PACKET_INTERFACE)
ob_append_byte(buff,interface);
if (flags & PACKET_SEQ)
ob_append_byte(buff,seq);
return 0;
}
// a frame destined for one of our local addresses, or broadcast, has arrived. Process it.
int process_incoming_frame(time_ms_t now, struct overlay_interface *interface, struct overlay_frame *f, struct decode_context *context){
IN();
int id = (interface - overlay_interfaces);
switch(f->type)
{
case OF_TYPE_SELFANNOUNCE_ACK:
link_state_legacy_ack(f, now);
break;
// data frames
case OF_TYPE_RHIZOME_ADVERT:
overlay_rhizome_saw_advertisements(id,f,now);
break;
case OF_TYPE_DATA:
case OF_TYPE_DATA_VOICE:
overlay_saw_mdp_containing_frame(f,now);
break;
case OF_TYPE_PLEASEEXPLAIN:
process_explain(f);
break;
default:
if (config.debug.verbose && config.debug.overlayframes)
DEBUGF("Overlay type f->type=0x%x not supported", f->type);
}
RETURN(0);
OUT();
}
// duplicate the frame and queue it
int overlay_forward_payload(struct overlay_frame *f){
IN();
if (f->ttl == 0){
if (config.debug.verbose && config.debug.overlayframes)
DEBUGF("NOT FORWARDING, due to ttl=0");
RETURN(0);
}
if (config.debug.verbose && config.debug.overlayframes)
DEBUGF("Forwarding payload for %s, ttl=%u",
(f->destination?alloca_tohex_sid(f->destination->sid):"broadcast"),
(unsigned)f->ttl);
/* Queue frame for dispatch.
Don't forget to put packet in the correct queue based on type.
(e.g., mesh management, voice, video, ordinary or opportunistic).
But the really important bit is to clone the frame, since the
structure we are looking at here must be left as is and returned
to the caller to do as they please */
struct overlay_frame *qf=op_dup(f);
if (!qf)
RETURN(WHY("Could not clone frame for queuing"));
if (overlay_payload_enqueue(qf)) {
op_free(qf);
RETURN(WHY("failed to enqueue forwarded payload"));
}
RETURN(0);
OUT();
}
// Parse the mdp envelope header
// may return (HEADER_PROCESS|HEADER_FORWARD) || -1
int parseMdpPacketHeader(struct decode_context *context, struct overlay_frame *frame,
struct overlay_buffer *buffer, struct subscriber **nexthop)
{
IN();
int process=1;
int forward=2;
int flags = ob_get(buffer);
if (flags<0)
RETURN(WHY("Unable to read flags"));
if (flags & PAYLOAD_FLAG_SENDER_SAME){
if (!context->sender)
context->invalid_addresses=1;
frame->source = context->sender;
}else{
int ret=overlay_address_parse(context, buffer, &frame->source);
if (ret<0)
RETURN(WHY("Unable to parse payload source"));
if (!frame->source || frame->source->reachable==REACHABLE_SELF){
process=forward=0;
if (config.debug.verbose && config.debug.overlayframes)
DEBUGF("Ignoring my packet (or unparsable source)");
}
}
if (flags & PAYLOAD_FLAG_TO_BROADCAST){
if (!(flags & PAYLOAD_FLAG_ONE_HOP)){
if (overlay_broadcast_parse(buffer, &frame->broadcast_id))
RETURN(WHY("Unable to read broadcast address"));
if (overlay_broadcast_drop_check(&frame->broadcast_id)){
process=forward=0;
if (config.debug.verbose && config.debug.overlayframes)
DEBUGF("Ignoring duplicate broadcast (%s)", alloca_tohex(frame->broadcast_id.id, BROADCAST_LEN));
}
if (link_state_should_forward_broadcast(context->sender)==0){
forward=0;
if (config.debug.verbose && config.debug.overlayframes)
DEBUGF("Not forwarding broadcast (%s), as we aren't a relay in the senders routing table", alloca_tohex(frame->broadcast_id.id, BROADCAST_LEN));
}
}
frame->destination=NULL;
}else{
int ret=overlay_address_parse(context, buffer, &frame->destination);
if (ret<0)
RETURN(WHY("Unable to parse payload destination"));
if (!frame->destination || frame->destination->reachable!=REACHABLE_SELF){
process=0;
if (config.debug.verbose && config.debug.overlayframes)
DEBUGF("Don't process packet not addressed to me");
}
if (!(flags & PAYLOAD_FLAG_ONE_HOP)){
ret=overlay_address_parse(context, buffer, nexthop);
if (ret<0)
RETURN(WHY("Unable to parse payload nexthop"));
if (!(*nexthop) || (*nexthop)->reachable!=REACHABLE_SELF){
forward=0;
if (config.debug.verbose && config.debug.overlayframes)
DEBUGF("Don't forward packet not addressed to me");
}
}
}
if (flags & PAYLOAD_FLAG_ONE_HOP) {
frame->ttl=1;
} else {
int ttl_qos = ob_get(buffer);
if (ttl_qos<0)
RETURN(WHY("Unable to read ttl"));
frame->ttl = ttl_qos & 0x1F;
frame->queue = (ttl_qos >> 5) & 3;
}
if (frame->ttl)
--frame->ttl;
if (frame->ttl == 0) {
forward = 0;
if (config.debug.verbose && config.debug.overlayframes)
DEBUGF("NOT FORWARDING, due to ttl=0");
}
if (flags & PAYLOAD_FLAG_LEGACY_TYPE){
int ftype = ob_get(buffer);
if (ftype == -1)
RETURN(WHY("Unable to read type"));
frame->type = ftype;
}else
frame->type=OF_TYPE_DATA;
if (context->packet_version >= 1){
int seq = ob_get(buffer);
if (seq == -1)
RETURN(WHY("Unable to read packet seq"));
// TODO unicast
if ((flags & PAYLOAD_FLAG_ONE_HOP) || !(flags & PAYLOAD_FLAG_TO_BROADCAST)){
if (link_received_duplicate(context->sender, context->interface, context->sender_interface, seq, 0)){
if (config.debug.verbose && config.debug.overlayframes)
DEBUG("Don't process or forward duplicate payloads");
forward=process=0;
}
}
}
frame->modifiers=flags;
frame->packet_version = context->packet_version;
// if we can't understand one of the addresses, skip processing the payload
if ((forward||process)&&context->invalid_addresses){
if (config.debug.verbose && config.debug.overlayframes)
DEBUG("Don't process or forward with invalid addresses");
forward=process=0;
}
RETURN(forward|process);
OUT();
}
int parseEnvelopeHeader(struct decode_context *context, struct overlay_interface *interface,
struct sockaddr_in *addr, struct overlay_buffer *buffer){
IN();
time_ms_t now = gettime_ms();
context->packet_version = ob_get(buffer);
if (context->packet_version < 0 || context->packet_version > SUPPORTED_PACKET_VERSION)
RETURN(WHY("Packet version not recognised."));
context->encapsulation = ob_get(buffer);
if (context->encapsulation !=ENCAP_OVERLAY && context->encapsulation !=ENCAP_SINGLE)
RETURN(WHY("Invalid packet encapsulation"));
if (overlay_address_parse(context, buffer, &context->sender))
RETURN(WHY("Unable to parse sender"));
int packet_flags = ob_get(buffer);
context->sender_interface = 0;
context->interface = interface;
int sender_seq = -1;
if (packet_flags & PACKET_INTERFACE)
context->sender_interface = ob_get(buffer);
if (packet_flags & PACKET_SEQ)
sender_seq = ob_get(buffer)&0xFF;
if (context->sender){
// ignore packets that have been reflected back to me
if (context->sender->reachable==REACHABLE_SELF){
if (config.debug.verbose && config.debug.overlayframes)
DEBUG("Completely ignore packets I sent");
RETURN(1);
}
if (context->sender->max_packet_version < context->packet_version)
context->sender->max_packet_version = context->packet_version;
// TODO probe unicast links when we detect an address change.
// if this is a dummy announcement for a node that isn't in our routing table
if (context->sender->reachable == REACHABLE_NONE) {
context->sender->interface = interface;
if (addr)
context->sender->address = *addr;
else
bzero(&context->sender->address, sizeof context->sender->address);
context->sender->last_probe = 0;
// assume for the moment, that we can reply with the same packet type
if (packet_flags&PACKET_UNICAST){
set_reachable(context->sender, REACHABLE_UNICAST|REACHABLE_ASSUMED);
}else{
set_reachable(context->sender, REACHABLE_BROADCAST|REACHABLE_ASSUMED);
}
}
/* Note the probe payload must be queued before any SID/SAS request so we can force the packet to have a full sid */
if (addr && (context->sender->last_probe==0 || now - context->sender->last_probe > interface->tick_ms*10))
overlay_send_probe(context->sender, *addr, interface, OQ_MESH_MANAGEMENT);
link_received_packet(context->sender, interface, context->sender_interface, sender_seq, packet_flags & PACKET_UNICAST);
}else{
// send a unicast probe, just incase they never hear our broadcasts.
if (addr)
overlay_send_probe(NULL, *addr, interface, OQ_MESH_MANAGEMENT);
}
if (addr){
if (packet_flags & PACKET_UNICAST)
context->addr=*addr;
else
context->addr=interface->broadcast_address;
}
RETURN(0);
OUT();
}
int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet, size_t len,
int recvttl, struct sockaddr *recvaddr, size_t recvaddrlen)
{
IN();
/*
This function decodes overlay packets which have been assembled for delivery overy IP networks.
IP based wireless networks have a high, but limited rate of packets that can be sent. In order
to increase throughput of small payloads, we ammend many payloads together and have used a scheme
to compress common network identifiers.
A different network type may have very different constraints on the number and size of packets,
and may need a different encoding scheme to use the bandwidth efficiently.
The current structure of an overlay packet is as follows;
Fixed header [0x4F, 0x10]
Version [0x00, 0x01]
Each frame within the packet has the following fields:
Frame type (8-24bits)
TTL (8bits)
Remaining frame size (RFS) (see overlay_payload.c or overlay_buffer.c for explanation of format)
Next hop (variable length due to address abbreviation)
Destination (variable length due to address abbreviation)
Source (variable length due to address abbreviation)
Payload (length = RFS- len(frame type) - len(next hop)
This structure is intended to allow relaying nodes to quickly ignore frames that are
not addressed to them as either the next hop or final destination.
The RFS field uses additional bytes to encode the length of longer frames.
This provides us with a slight space saving for the common case of short frames.
The frame payload itself can be enciphered with the final destination's public key, so
that it is not possible for the relaying 3rd parties to observe the content.
Naturally some information will leak simply based on the size, periodicity and other
characteristics of the traffic, and some 3rd parties may be malevolent, so noone should
assume that this provides complete security.
It would be possible to design a super-paranoid mode where onion routing is used with
concentric shells of encryption so that each hop can only work out the next node to send it
to. However, that would result in rather large frames, which may well betray more information
than the super-paranoid mode would hide.
Note also that it is possible to dispatch frames on a local link which are addressed to
broadcast, but are enciphered. In that situation only the intended recipient can
decode the frame, but at the cost of having all nodes on the local link having to decrypt
frame. Of course the nodes may elect to not decrypt such anonymous frames.
Such frames could even be flooded throughout part of the mesh by having the TTL>1, and
optionally with an anonymous source address to provide some plausible deniability for both
sending and reception if combined with a randomly selected TTL to give the impression of
the source having received the frame from elsewhere.
*/
if (recvaddr&&recvaddr->sa_family!=AF_INET)
RETURN(WHYF("Unexpected protocol family %d",recvaddr->sa_family));
struct overlay_frame f;
struct decode_context context;
bzero(&context, sizeof context);
bzero(&f,sizeof f);
time_ms_t now = gettime_ms();
struct overlay_buffer *b = ob_static(packet, len);
ob_limitsize(b, len);
context.interface = f.interface = interface;
if (recvaddr)
f.recvaddr = *((struct sockaddr_in *)recvaddr);
else
bzero(&f.recvaddr, sizeof f.recvaddr);
if (config.debug.verbose && config.debug.overlayframes)
DEBUG("Received overlay packet");
int ret=parseEnvelopeHeader(&context, interface, (struct sockaddr_in *)recvaddr, b);
if (ret){
ob_free(b);
RETURN(ret);
}
while(ob_remaining(b)>0){
context.invalid_addresses=0;
struct subscriber *nexthop=NULL;
bzero(f.broadcast_id.id, BROADCAST_LEN);
unsigned char *header_start = ob_ptr(b)+ob_position(b);
int header_valid = parseMdpPacketHeader(&context, &f, b, &nexthop);
if (header_valid<0){
ret = WHY("Header is too short");
break;
}
// TODO allow for one byte length?
unsigned int payload_len;
switch (context.encapsulation){
case ENCAP_SINGLE:
payload_len = ob_remaining(b);
break;
default:
case ENCAP_OVERLAY:
payload_len = ob_get_ui16(b);
if (payload_len > ob_remaining(b)){
unsigned char *current = ob_ptr(b)+ob_position(b);
dump("Payload Header", header_start, current - header_start);
ret = WHYF("Invalid payload length (%d)", payload_len);
goto end;
}
break;
}
int next_payload = ob_position(b) + payload_len;
if (config.debug.overlayframes){
DEBUGF("Received payload type %x, len %d", f.type, payload_len);
DEBUGF("Payload from %s", f.source?alloca_tohex_sid(f.source->sid):"NULL");
DEBUGF("Payload to %s", (f.destination?alloca_tohex_sid(f.destination->sid):"broadcast"));
if (!is_all_matching(f.broadcast_id.id, BROADCAST_LEN, 0))
DEBUGF("Broadcast id %s", alloca_tohex(f.broadcast_id.id, BROADCAST_LEN));
if (nexthop)
DEBUGF("Next hop %s", alloca_tohex_sid(nexthop->sid));
}
if (header_valid!=0){
f.payload = ob_slice(b, b->position, payload_len);
if (!f.payload){
// out of memory?
WHY("Unable to slice payload");
break;
}
// mark the entire payload as having valid data
ob_limitsize(f.payload, payload_len);
// forward payloads that are for someone else or everyone
if (header_valid&HEADER_FORWARD)
overlay_forward_payload(&f);
// process payloads that are for me or everyone
if (header_valid&HEADER_PROCESS)
process_incoming_frame(now, interface, &f, &context);
if (f.next_hop == my_subscriber || f.destination == my_subscriber)
link_state_ack_soon(context.sender);
}
if (f.payload){
ob_free(f.payload);
f.payload=NULL;
}
b->position=next_payload;
}
end:
send_please_explain(&context, my_subscriber, context.sender);
ob_free(b);
RETURN(ret);
OUT();
}