-
Notifications
You must be signed in to change notification settings - Fork 0
/
recv.c
447 lines (368 loc) · 10.5 KB
/
recv.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
/*
* Copyright (C) 2012-2015 B.A.T.M.A.N. contributors:
*
* Simon Wunderlich
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* 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 <errno.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "alfred.h"
#include "batadv_query.h"
#include "hash.h"
#include "list.h"
#include "packet.h"
static int finish_alfred_push_data(struct globals *globals,
struct ether_addr mac,
struct alfred_push_data_v0 *push)
{
int len, data_len;
bool new_entry_created;
struct alfred_data *data;
struct dataset *dataset;
uint8_t *pos;
len = ntohs(push->header.length);
len -= sizeof(*push) - sizeof(push->header);
pos = (uint8_t *)push->data;
while (len >= (int)sizeof(*data)) {
data = (struct alfred_data *)pos;
data_len = ntohs(data->header.length);
/* check if enough data is available */
if ((int)(data_len + sizeof(*data)) > len)
break;
new_entry_created = false;
dataset = hash_find(globals->data_hash, data);
if (!dataset) {
dataset = malloc(sizeof(*dataset));
if (!dataset)
goto err;
dataset->buf = NULL;
dataset->data_source = SOURCE_SYNCED;
memcpy(&dataset->data, data, sizeof(*data));
if (hash_add(globals->data_hash, dataset)) {
free(dataset);
goto err;
}
new_entry_created = true;
}
/* don't overwrite our own data */
if (dataset->data_source == SOURCE_LOCAL)
goto skip_data;
clock_gettime(CLOCK_MONOTONIC, &dataset->last_seen);
/* check that data was changed */
if (new_entry_created ||
dataset->data.header.length != data_len ||
memcmp(dataset->buf, data->data, data_len) != 0)
changed_data_type(globals, data->header.type);
/* free old buffer */
if (dataset->buf) {
free(dataset->buf);
dataset->data.header.length = 0;
}
dataset->buf = malloc(data_len);
/* that's not good */
if (!dataset->buf)
goto err;
dataset->data.header.length = data_len;
dataset->data.header.version = data->header.version;
memcpy(dataset->buf, data->data, data_len);
/* if the sender is also the the source of the dataset, we
* got a first hand dataset. */
if (memcmp(&mac, data->source, ETH_ALEN) == 0)
dataset->data_source = SOURCE_FIRST_HAND;
else
dataset->data_source = SOURCE_SYNCED;
skip_data:
pos += (sizeof(*data) + data_len);
len -= (sizeof(*data) + data_len);
}
return 0;
err:
return -1;
}
struct transaction_head *
transaction_add(struct globals *globals, struct ether_addr mac, uint16_t id)
{
struct transaction_head *head;
head = malloc(sizeof(*head));
if (!head)
return NULL;
head->server_addr = mac;
head->id = id;
head->requested_type = 0;
head->finished = 0;
head->num_packet = 0;
head->client_socket = -1;
clock_gettime(CLOCK_MONOTONIC, &head->last_rx_time);
INIT_LIST_HEAD(&head->packet_list);
if (hash_add(globals->transaction_hash, head)) {
free(head);
return NULL;
}
return head;
}
struct transaction_head *transaction_clean(struct globals *globals,
struct transaction_head *head)
{
struct transaction_packet *transaction_packet, *safe;
list_for_each_entry_safe(transaction_packet, safe, &head->packet_list,
list) {
list_del(&transaction_packet->list);
free(transaction_packet->push);
free(transaction_packet);
}
hash_remove(globals->transaction_hash, head);
return head;
}
struct transaction_head *
transaction_clean_hash(struct globals *globals, struct transaction_head *search)
{
struct transaction_head *head;
head = hash_find(globals->transaction_hash, search);
if (!head)
return head;
return transaction_clean(globals, head);
}
static int process_alfred_push_data(struct globals *globals,
struct in6_addr *source,
struct alfred_push_data_v0 *push)
{
int len;
struct ether_addr mac;
int ret;
struct transaction_head search, *head;
struct transaction_packet *transaction_packet;
int found;
ret = ipv6_to_mac(source, &mac);
if (ret < 0)
goto err;
len = ntohs(push->header.length);
if (len < (int)(sizeof(*push) - sizeof(push->header)))
goto err;
search.server_addr = mac;
search.id = ntohs(push->tx.id);
head = hash_find(globals->transaction_hash, &search);
if (!head) {
/* slave must create the transactions to be able to correctly
* wait for it */
if (globals->opmode != OPMODE_MASTER)
goto err;
head = transaction_add(globals, mac, ntohs(push->tx.id));
if (!head)
goto err;
}
clock_gettime(CLOCK_MONOTONIC, &head->last_rx_time);
/* this transaction was already finished/dropped */
if (head->finished != 0)
return -1;
found = 0;
list_for_each_entry(transaction_packet, &head->packet_list, list) {
if (transaction_packet->push->tx.seqno == push->tx.seqno) {
found = 1;
break;
}
}
/* it seems the packet was duplicated */
if (found)
return 0;
transaction_packet = malloc(sizeof(*transaction_packet));
if (!transaction_packet)
goto err;
transaction_packet->push = malloc(len + sizeof(push->header));
if (!transaction_packet->push) {
free(transaction_packet);
goto err;
}
memcpy(transaction_packet->push, push, len + sizeof(push->header));
list_add_tail(&transaction_packet->list, &head->packet_list);
head->num_packet++;
return 0;
err:
return -1;
}
static int
process_alfred_announce_master(struct globals *globals,
struct interface *interface,
struct in6_addr *source,
struct alfred_announce_master_v0 *announce)
{
struct server *server;
struct ether_addr *macaddr;
struct ether_addr mac;
int ret;
int len;
len = ntohs(announce->header.length);
ret = ipv6_to_mac(source, &mac);
if (ret < 0)
return -1;
if (announce->header.version != ALFRED_VERSION)
return -1;
if (len != (sizeof(*announce) - sizeof(announce->header)))
return -1;
server = hash_find(interface->server_hash, &mac);
if (!server) {
server = malloc(sizeof(*server));
if (!server)
return -1;
memcpy(&server->hwaddr, &mac, ETH_ALEN);
memcpy(&server->address, source, sizeof(*source));
if (hash_add(interface->server_hash, server)) {
free(server);
return -1;
}
}
clock_gettime(CLOCK_MONOTONIC, &server->last_seen);
if (strcmp(globals->mesh_iface, "none") != 0) {
macaddr = translate_mac(globals->mesh_iface,
(struct ether_addr *)&server->hwaddr);
if (macaddr)
server->tq = get_tq(globals->mesh_iface, macaddr);
else
server->tq = 0;
} else {
server->tq = 255;
}
if (globals->opmode == OPMODE_SLAVE)
set_best_server(globals);
return 0;
}
static int process_alfred_request(struct globals *globals,
struct interface *interface,
struct in6_addr *source,
struct alfred_request_v0 *request)
{
int len;
len = ntohs(request->header.length);
if (request->header.version != ALFRED_VERSION)
return -1;
if (len != (sizeof(*request) - sizeof(request->header)))
return -1;
push_data(globals, interface, source, SOURCE_SYNCED,
request->requested_type, request->tx_id);
return 0;
}
static int process_alfred_status_txend(struct globals *globals,
struct in6_addr *source,
struct alfred_status_v0 *request)
{
struct transaction_head search, *head;
struct transaction_packet *transaction_packet, *safe;
struct ether_addr mac;
int len, ret;
len = ntohs(request->header.length);
if (request->header.version != ALFRED_VERSION)
return -1;
if (len != (sizeof(*request) - sizeof(request->header)))
return -1;
ret = ipv6_to_mac(source, &mac);
if (ret < 0)
return -1;
search.server_addr = mac;
search.id = ntohs(request->tx.id);
head = hash_find(globals->transaction_hash, &search);
if (!head)
return -1;
/* this transaction was already finished/dropped */
if (head->finished != 0)
return -1;
/* missing packets -> cleanup everything */
if (head->num_packet != ntohs(request->tx.seqno))
head->finished = -1;
else
head->finished = 1;
list_for_each_entry_safe(transaction_packet, safe, &head->packet_list,
list) {
if (head->finished == 1)
finish_alfred_push_data(globals, mac,
transaction_packet->push);
list_del(&transaction_packet->list);
free(transaction_packet->push);
free(transaction_packet);
}
head = transaction_clean_hash(globals, &search);
if (!head)
return -1;
if (head->client_socket < 0)
free(head);
else
unix_sock_req_data_finish(globals, head);
return 0;
}
int recv_alfred_packet(struct globals *globals, struct interface *interface,
int recv_sock)
{
uint8_t buf[MAX_PAYLOAD];
ssize_t length;
struct alfred_tlv *packet;
struct sockaddr_in6 source;
socklen_t sourcelen;
if (interface->netsock < 0)
return -1;
sourcelen = sizeof(source);
length = recvfrom(recv_sock, buf, sizeof(buf), 0,
(struct sockaddr *)&source, &sourcelen);
if (length <= 0) {
perror("read from network socket failed");
return -1;
}
packet = (struct alfred_tlv *)buf;
/* drop packets not sent over link-local ipv6 */
if (!is_ipv6_eui64(&source.sin6_addr))
return -1;
/* drop packets from ourselves */
if (netsock_own_address(globals, &source.sin6_addr))
return -1;
/* drop truncated packets */
if (length < (int)sizeof(*packet) ||
length < (int)(ntohs(packet->length) + sizeof(*packet)))
return -1;
/* drop incompatible packet */
if (packet->version != ALFRED_VERSION)
return -1;
switch (packet->type) {
case ALFRED_PUSH_DATA:
process_alfred_push_data(globals, &source.sin6_addr,
(struct alfred_push_data_v0 *)packet);
break;
case ALFRED_ANNOUNCE_MASTER:
process_alfred_announce_master(globals, interface,
&source.sin6_addr,
(struct alfred_announce_master_v0 *)packet);
break;
case ALFRED_REQUEST:
process_alfred_request(globals, interface, &source.sin6_addr,
(struct alfred_request_v0 *)packet);
break;
case ALFRED_STATUS_TXEND:
process_alfred_status_txend(globals, &source.sin6_addr,
(struct alfred_status_v0 *)packet);
break;
default:
/* unknown packet type */
return -1;
}
return 0;
}