-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschedule.c
1593 lines (1008 loc) · 44.3 KB
/
schedule.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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2006 BATMAN contributors:
* Axel Neumann
* 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 <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h> /* ifr_if, ifr_tun */
#include <linux/rtnetlink.h>
#include "batman.h"
#include "os.h"
#include "originator.h"
#include "metrics.h"
#include "plugin.h"
#include "schedule.h"
int32_t my_ogi; /* orginator message interval in miliseconds */
int32_t ogi_pwrsave;
static int32_t pref_udpd_size = DEF_UDPD_SIZE;
//static int32_t aggr_p_ogi;
static int32_t aggr_interval;
#ifndef NOPARANOIA
#define DEF_SIM_PARA NO
static int32_t sim_paranoia = DEF_SIM_PARA;
#endif
static SIMPEL_LIST( send_list );
static SIMPEL_LIST( task_list );
static int32_t receive_max_sock = 0;
static fd_set receive_wait_set;
static uint16_t changed_readfds = 1;
static int ifevent_sk = -1;
static struct ext_packet my_pip_extension_packet;
void change_selects( void ) {
changed_readfds++;
}
static void check_selects( void ) {
if( changed_readfds == 0 )
return;
struct list_head *list_pos;
dbgf_all( DBGT_INFO, "%d select fds changed... ", changed_readfds );
changed_readfds = 0;
FD_ZERO(&receive_wait_set);
receive_max_sock = 0;
receive_max_sock = ifevent_sk;
FD_SET(ifevent_sk, &receive_wait_set);
if ( receive_max_sock < unix_sock )
receive_max_sock = unix_sock;
FD_SET(unix_sock, &receive_wait_set);
list_for_each( list_pos, &ctrl_list ) {
struct ctrl_node *cn = list_entry( list_pos, struct ctrl_node, list );
if ( cn->fd > 0 && cn->fd != STDOUT_FILENO ) {
receive_max_sock = MAX( receive_max_sock, cn->fd);
FD_SET( cn->fd, &receive_wait_set );
}
}
list_for_each(list_pos, &if_list) {
struct batman_if *bif = list_entry(list_pos, struct batman_if, list);
if ( bif->if_active && bif->if_linklayer != VAL_DEV_LL_LO ) {
receive_max_sock = MAX( receive_max_sock, bif->if_unicast_sock );
FD_SET(bif->if_unicast_sock, &receive_wait_set);
receive_max_sock = MAX( receive_max_sock, bif->if_netwbrc_sock );
FD_SET(bif->if_netwbrc_sock, &receive_wait_set);
if (bif->if_fullbrc_sock > 0) {
receive_max_sock = MAX( receive_max_sock, bif->if_fullbrc_sock );
FD_SET(bif->if_fullbrc_sock, &receive_wait_set);
}
}
}
list_for_each( list_pos, &cb_fd_list ) {
struct cb_fd_node *cdn = list_entry( list_pos, struct cb_fd_node, list );
receive_max_sock = MAX( receive_max_sock, cdn->fd );
FD_SET( cdn->fd, &receive_wait_set );
}
}
void register_task( uint32_t timeout, void (* task) (void *), void *data ) {
struct list_head *list_pos;
//TODO: allocating and freeing tn and tn->data may be much faster when done by registerig function..
struct task_node *tn = debugMalloc( sizeof( struct task_node ), 109 );
memset( tn, 0, sizeof(struct task_node) );
INIT_LIST_HEAD( &tn->list );
tn->expire = batman_time + timeout;
tn->task = task;
tn->data = data;
struct list_head *prev_list_head = (struct list_head *)&task_list;
struct task_node *tmp_tn = NULL;
list_for_each( list_pos, &task_list ) {
tmp_tn = list_entry( list_pos, struct task_node, list );
if ( GREAT_U32(tmp_tn->expire, tn->expire) ) {
list_add_before( prev_list_head, list_pos, &tn->list );
break;
}
prev_list_head = &tmp_tn->list;
}
if ( ( tmp_tn == NULL ) || ( LSEQ_U32(tmp_tn->expire, tn->expire) ) )
list_add_tail( &tn->list, &task_list );
}
void remove_task( void (* task) (void *), void *data ) {
struct list_head *list_pos, *tmp_pos, *prev_pos = (struct list_head*)&task_list;
list_for_each_safe( list_pos, tmp_pos, &task_list ) {
struct task_node *tn = list_entry( list_pos, struct task_node, list );
if ( tn->task == task && tn->data == data ) {
list_del( prev_pos, list_pos, &task_list );
if ( tn->data )
debugFree( tn->data, 1109 );
debugFree( tn, 1109 );
} else {
prev_pos = &tn->list;
}
}
}
uint32_t whats_next( void ) {
struct list_head *list_pos, *tmp_pos, *prev_pos = (struct list_head*)&task_list;
paranoia( -500175, sim_paranoia );
list_for_each_safe( list_pos, tmp_pos, &task_list ) {
struct task_node *tn = list_entry( list_pos, struct task_node, list );
if ( LSEQ_U32( tn->expire, batman_time ) ) {
list_del( prev_pos, list_pos, &task_list );
(*(tn->task)) (tn->data);
if ( tn->data )
debugFree( tn->data, 1109 );
debugFree( tn, 1109 );
return 0;
} else {
return tn->expire - batman_time;
}
}
return MAX_SELECT_TIMEOUT_MS;
}
static void send_aggregated_ogms( void ) {
prof_start( PROF_send_aggregated_ogms );
struct list_head *if_pos;
uint8_t iftype;
/* send all the aggregated packets (which fit into max packet size) */
/* broadcast via lan interfaces first */
for ( iftype = VAL_DEV_LL_LAN; iftype <= VAL_DEV_LL_WLAN; iftype++ ) {
list_for_each(if_pos, &if_list) {
struct batman_if *bif = list_entry(if_pos, struct batman_if, list);
dbgf_all(DBGT_INFO, "dev: %s, linklayer %d iftype %d len %d min_len %d...",
bif->dev, bif->if_linklayer, iftype, bif->aggregation_len,
(int32_t)sizeof ( struct bat_header));
if (bif->if_linklayer == iftype &&
bif->aggregation_len > (int32_t)sizeof ( struct bat_header)) {
struct bat_header *bat_hdr = (struct bat_header *) bif->aggregation_out;
bat_hdr->version = COMPAT_VERSION;
bat_hdr->link_flags = 0;
bat_hdr->size = (bif->aggregation_len) / 4;
if (bif->aggregation_len > MAX_UDPD_SIZE || (bif->aggregation_len) % 4 != 0) {
dbg(DBGL_SYS, DBGT_ERR, "trying to send strange packet length %d oktets",
bif->aggregation_len);
cleanup_all(-500016);
}
if (send_udp_packet(bif->aggregation_out, bif->aggregation_len,
&bif->if_netwbrc_addr, bif->if_unicast_sock) < 0) {
dbg_mute(60, DBGL_SYS, DBGT_ERR,
"send_aggregated_ogms() cant send via dev %s %s fd %d",
bif->dev, bif->if_ip_str, bif->if_unicast_sock);
}
bif->aggregation_len = sizeof ( struct bat_header);
}
}
}
prof_stop( PROF_send_aggregated_ogms );
return;
}
void debug_send_list( struct ctrl_node *cn ) {
struct list_head *list_pos;
//char str[ADDR_STR_LEN];
dbg_printf( cn, "Outstanding OGM for sending: \n" );
list_for_each( list_pos, &send_list ) {
struct send_node *send_node = list_entry( list_pos, struct send_node, list );
struct bat_packet_ogm *ogm = send_node->ogm;
dbg_printf( cn, "%-15s (seqno %5d ttl %3d) at %u \n",
ipStr( ogm->orig ), ntohs(ogm->ogm_seqno), ogm->ogm_ttl, send_node->send_time );
}
dbg_printf( cn, "\n" );
return;
}
static int open_ifevent_netlink_sk( void ) {
struct sockaddr_nl sa;
int32_t unix_opts;
memset (&sa, 0, sizeof(sa));
sa.nl_family = AF_NETLINK;
sa.nl_groups |= RTMGRP_IPV4_IFADDR;
sa.nl_groups |= RTMGRP_LINK; // (this can result in select storms with buggy wlan devices
if ( ( ifevent_sk = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE) ) < 0 ) {
dbg( DBGL_SYS, DBGT_ERR, "can't create af_netlink socket for reacting on if up/down events: %s",
strerror(errno) );
ifevent_sk = 0;
return -1;
}
unix_opts = fcntl( ifevent_sk, F_GETFL, 0 );
fcntl( ifevent_sk, F_SETFL, unix_opts | O_NONBLOCK );
if ( ( bind( ifevent_sk, (struct sockaddr*)&sa, sizeof(sa) ) ) < 0 ) {
dbg( DBGL_SYS, DBGT_ERR, "can't bind af_netlink socket for reacting on if up/down events: %s",
strerror(errno) );
ifevent_sk = 0;
return -1;
}
change_selects();
return ifevent_sk;
}
static void close_ifevent_netlink_sk( void ) {
if ( ifevent_sk > 0 )
close( ifevent_sk );
ifevent_sk = 0;
}
static void recv_ifevent_netlink_sk( void ) {
char buf[4096]; //test this with a very small value !!
struct iovec iov = { buf, sizeof(buf) };
struct sockaddr_nl sa;
struct msghdr msg = { (void *)&sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
while( recvmsg (ifevent_sk, &msg, 0) > 0 );
//so fare I just want to consume the pending message...
}
void remove_outstanding_ogms( struct batman_if *bif ) {
struct send_node *send_node;
struct list_head *send_pos, *send_temp, *send_prev;
if ( !bif )
return;
send_prev = (struct list_head *)&send_list;
list_for_each_safe( send_pos, send_temp, &send_list ) {
send_node = list_entry( send_pos, struct send_node, list );
if ( send_node->if_outgoing == bif ) {
list_del( send_prev, send_pos, &send_list );
debugFree(send_node, 1502);
} else {
send_prev = send_pos;
}
}
}
static void aggregate_outstanding_ogms( void *unused ) {
prof_start( PROF_send_outstanding_ogms );
struct send_node *send_node;
struct list_head *send_pos, *if_pos, *send_temp, *prev_list_head;
struct batman_if *bif;
uint8_t directlink, unidirectional, cloned, ttl, if_singlehomed;
int16_t aggregated_size = sizeof( struct bat_header );
int dbg_if_out = 0;
#define MAX_DBG_IF_SIZE 200
static char dbg_if_str[ MAX_DBG_IF_SIZE ];
// ensuring that aggreg_interval is really an upper boundary
register_task( aggr_interval - 1 - rand_num( aggr_interval/10 ), aggregate_outstanding_ogms, NULL );
prev_list_head = (struct list_head *)&send_list;
list_for_each_safe( send_pos, send_temp, &send_list ) {
send_node = list_entry( send_pos, struct send_node, list );
if ( GREAT_U32( send_node->send_time, batman_time ) )
break; // for now we are done,
if ( aggregated_size > (int32_t)sizeof(struct bat_header) &&
aggregated_size + send_node->ogm_buff_len > pref_udpd_size )
{
dbgf_all( DBGT_INFO, "max aggregated size %d", aggregated_size );
send_aggregated_ogms();
aggregated_size = sizeof( struct bat_header );
}
send_node->iteration++;
uint8_t send_node_done = YES;
if ( (aggregated_size + send_node->ogm_buff_len > MAX_UDPD_SIZE) ||
(aggregated_size + send_node->ogm_buff_len > pref_udpd_size && send_node->own_if) )
{
if ( aggregated_size <= (int32_t)sizeof( struct bat_header ) ) {
dbg_mute( 30, DBGL_SYS, DBGT_ERR,
"Drop OGM, single packet (own=%d) to large to fit legal packet size"
"scheduled %d, agg_size %d, next_len %d, pref_udpd_size %d !!",
send_node->own_if, send_node->send_time,
aggregated_size, send_node->ogm_buff_len, pref_udpd_size );
}
} else {
if ( send_node->send_bucket == 0 )
send_node->send_bucket = ((int32_t)(rand_num( 100 )));
// keep care to not aggregate more packets than would fit into max packet size
aggregated_size += send_node->ogm_buff_len;
directlink = (send_node->ogm->flags & DIRECTLINK_FLAG );
unidirectional = (send_node->ogm->flags & UNIDIRECTIONAL_FLAG );
cloned = (send_node->ogm->flags & CLONED_FLAG );
ttl = send_node->ogm->ogm_ttl;
if_singlehomed = ( (send_node->own_if && send_node->if_outgoing->if_singlehomed) ? 1 : 0 );
//can't forward packet with IDF: outgoing iface not specified
paranoia( -500015, ( directlink && !send_node->if_outgoing ) );
if ( !send_node->if_outgoing->if_active ) {
dbg_if_out += snprintf( (dbg_if_str + dbg_if_out), (MAX_DBG_IF_SIZE - dbg_if_out), " (down)" );
}
/* rebroadcast only to allow neighbor to detect bidirectional link */
if ( send_node->if_outgoing->if_active &&
send_node->iteration <= send_node->if_outgoing->if_ant_diversity &&
directlink &&
!cloned &&
( unidirectional || ttl == 0 ) )
{
dbg_if_out += snprintf( (dbg_if_str + dbg_if_out), (MAX_DBG_IF_SIZE - dbg_if_out),
" %-12s (NBD)", send_node->if_outgoing->dev );
if ( (send_node->send_bucket + 100) < send_node->if_outgoing->if_send_clones )
send_node_done = NO;
//TODO: send only pure bat_packet_ogm, no extension headers.
memcpy( send_node->if_outgoing->aggregation_out +
send_node->if_outgoing->aggregation_len,
send_node->ogm, send_node->ogm_buff_len );
send_node->if_outgoing->aggregation_len += send_node->ogm_buff_len;
/* (re-) broadcast to propagate existence of path to OG*/
} else if ( !unidirectional && ttl > 0 ) {
list_for_each(if_pos, &if_list) {
struct bat_packet_ogm *ogm;
bif = list_entry(if_pos, struct batman_if, list);
if ( !bif->if_active )
continue;
if ( ( send_node->send_bucket >= bif->if_send_clones ) ||
( if_singlehomed && send_node->if_outgoing != bif ) )
continue;
if (
// power-save mode enabled:
ogi_pwrsave > my_ogi &&
// we alone:
GREAT_U32(batman_time, bif->if_last_link_activity + COMMON_OBSERVATION_WINDOW) &&
(// not yet time for a neighbor-discovery hardbeat?:
send_node->own_if == 0 ||
send_node->if_outgoing != bif ||
LSEQ_U32(batman_time, bif->if_next_pwrsave_hardbeat)
))
continue;
if ( (send_node->send_bucket + 100) < bif->if_send_clones )
send_node_done = NO;
ogm = (struct bat_packet_ogm*) (bif->aggregation_out + bif->aggregation_len);
memcpy( ogm, send_node->ogm, send_node->ogm_buff_len );
if ( send_node->iteration > bif->if_ant_diversity )
ogm->flags |= CLONED_FLAG;
if ( ( directlink ) && ( send_node->if_outgoing == bif ) )
ogm->flags |= DIRECTLINK_FLAG;
else
ogm->flags &= ~DIRECTLINK_FLAG;
bif->aggregation_len += send_node->ogm_buff_len;
dbg_if_out += snprintf( (dbg_if_str + dbg_if_out),
(MAX_DBG_IF_SIZE - dbg_if_out), " %-12s", bif->dev );
if (if_singlehomed && send_node->if_outgoing == bif)
dbg_if_out += snprintf( (dbg_if_str + dbg_if_out),
(MAX_DBG_IF_SIZE - dbg_if_out), " (npIF)" );
}
}
send_node->send_bucket += 100;
dbgf_all( DBGT_INFO,
"OG %-16s, seqno %5d, TTL %2d, IDF %d, UDF %d, CLF %d "
"iter %d len %3d max agg_size %3d IFs %s",
ipStr( send_node->ogm->orig ),
ntohs( send_node->ogm->ogm_seqno ),
ttl, directlink, unidirectional, cloned, send_node->iteration,
send_node->ogm_buff_len, aggregated_size, dbg_if_str );
*dbg_if_str = '\0';
dbg_if_out = 0;
}
// trigger next seqno now where the first one of the current seqno has been send
if ( send_node->own_if && send_node->iteration == 1 ) {
send_node->if_outgoing->if_seqno++;
send_node->if_outgoing->send_own = 1;
}
// remove all the finished packets from send_list
if ( send_node_done ) {
list_del( prev_list_head, send_pos, &send_list );
debugFree(send_node, 1502);
} else {
prev_list_head = &send_node->list;
}
}
if ( aggregated_size > (int32_t)sizeof( struct bat_header ) ) {
send_aggregated_ogms();
dbgf_all( DBGT_INFO, "max aggregated size %d", aggregated_size );
}
list_for_each(if_pos, &if_list) {
bif = list_entry(if_pos, struct batman_if, list);
if ( bif->aggregation_len != sizeof( struct bat_header ) ) {
dbgf( DBGL_SYS, DBGT_ERR,
"finished with dev %s and packet_out_len %d > %d",
bif->dev, bif->aggregation_len, (int)sizeof( struct bat_header ) );
}
// if own OGMs need to be send, schedule next one now
if ( bif->send_own ) {
schedule_own_ogm( bif );
if (GREAT_U32(batman_time, bif->if_next_pwrsave_hardbeat))
bif->if_next_pwrsave_hardbeat = batman_time + ((uint32_t) (ogi_pwrsave));
bif->send_own = 0;
}
// this timestamp may become invalid after U32 wrap-around
if (GREAT_U32(batman_time, bif->if_last_link_activity + (2 * COMMON_OBSERVATION_WINDOW)))
bif->if_last_link_activity = batman_time - COMMON_OBSERVATION_WINDOW;
}
prof_stop( PROF_send_outstanding_ogms );
return;
}
void schedule_rcvd_ogm( uint16_t oCtx, uint16_t neigh_id, struct msg_buff *mb ) {
prof_start( PROF_schedule_rcvd_ogm );
struct send_node *send_packet_tmp = NULL;
struct list_head *list_pos, *prev_list_head;
uint8_t with_unidirectional_flag = 0;
uint8_t directlink = 0;
/* is single hop (direct) neighbour */
if ( oCtx & IS_DIRECT_NEIGH ) {
directlink = 1;
/* it is our best route towards him */
if ( (oCtx & IS_ACCEPTED) && (oCtx & IS_BEST_NEIGH_AND_NOT_BROADCASTED) ) {
if (oCtx & IS_ASOCIAL) {
dbgf_all( DBGT_INFO, "re-brc neighb OGM with direct link and unidirect flag" );
with_unidirectional_flag = 1;
} else {// mark direct link on incoming interface
dbgf_all( DBGT_INFO, "rebroadcast neighbour packet with direct link flag" );
}
/* if an unidirectional direct neighbour sends us a packet or
* if a bidirectional neighbour sends us a packet who is not our best link to him:
* retransmit it with unidirectional flag to tell him that we get his packets */
} else if ( !(oCtx & HAS_CLONED_FLAG) ) {
dbgf_all( DBGT_INFO, "re-brc neighb OGM with direct link and unidirect flag" );
with_unidirectional_flag = 1;
} else {
dbgf_all( DBGT_INFO, "drop OGM: no reason to re-brc!" );
prof_stop( PROF_schedule_rcvd_ogm );
return;
}
} else if (oCtx & IS_ASOCIAL) {
dbgf_all( DBGT_INFO, "drop OGM, asocial devices re-brc almost nothing :-(" );
prof_stop( PROF_schedule_rcvd_ogm );
return;
/* multihop originator */
} else if ( (oCtx & IS_ACCEPTED) && (oCtx & IS_BEST_NEIGH_AND_NOT_BROADCASTED) ) {
dbgf_all( DBGT_INFO, "re-brc OGM" );
} else {
dbgf_all( DBGT_INFO, "drop multihop OGM, not accepted or not via best link !");
prof_stop( PROF_schedule_rcvd_ogm );
return;
}
if ( !( ( (mb->bp.ogm)->ogm_ttl == 1 && directlink) || (mb->bp.ogm)->ogm_ttl > 1 ) ){
dbgf_all( DBGT_INFO, "ttl exceeded" );
prof_stop( PROF_schedule_rcvd_ogm );
return;
}
uint16_t i, snd_ext_total_len = 0;
for( i=0; i<=EXT_TYPE_MAX; i++ )
snd_ext_total_len += mb->snd_ext_len[i];
struct send_node *sn = debugMalloc( sizeof(struct send_node) + sizeof(struct bat_packet_ogm) + snd_ext_total_len, 504 );
memset( sn, 0, sizeof( struct send_node ) );
INIT_LIST_HEAD( &sn->list );
sn->ogm_buff_len = sizeof(struct bat_packet_ogm) + snd_ext_total_len;
sn->ogm = (struct bat_packet_ogm*)sn->_attached_ogm_buff;
memcpy( sn->ogm, mb->bp.ogm, sizeof(struct bat_packet_ogm) );
/* primary-interface-extension messages do not need to be rebroadcastes */
/* other extension messages only if not unidirectional and ttl > 1 */
uint16_t p = sizeof(struct bat_packet_ogm);
for( i=0; (snd_ext_total_len && i<=EXT_TYPE_MAX); i++ ) {
if ( mb->snd_ext_len[i] ) {
memcpy( &(((unsigned char*)(sn->ogm))[p]),
(unsigned char *)(mb->snd_ext_array[i]),
mb->snd_ext_len[i] );
p += mb->snd_ext_len[i];
}
}
if ( p != sn->ogm_buff_len ) {
dbgf( DBGL_SYS, DBGT_ERR, "incorrect msg lengths %d != %d", p, sn->ogm_buff_len );
}
sn->ogm->ogm_ttl--;
sn->ogm->prev_hop_id = neigh_id;
sn->ogm->bat_size = (sn->ogm_buff_len)>>2;
sn->send_time = batman_time;
sn->own_if = 0;
sn->if_outgoing = mb->iif;
sn->ogm->flags = 0x00;
if ( with_unidirectional_flag )
sn->ogm->flags |= UNIDIRECTIONAL_FLAG;
if ( directlink )
sn->ogm->flags |= DIRECTLINK_FLAG;
if ( oCtx & HAS_CLONED_FLAG )
sn->ogm->flags |= CLONED_FLAG;
/* change sequence number to network order */
sn->ogm->ogm_seqno = htons(sn->ogm->ogm_seqno);
prev_list_head = (struct list_head *)&send_list;
list_for_each( list_pos, &send_list ) {
send_packet_tmp = list_entry( list_pos, struct send_node, list );
if ( GREAT_U32(send_packet_tmp->send_time, sn->send_time) ) {
list_add_before( prev_list_head, list_pos, &sn->list );
break;
}
prev_list_head = &send_packet_tmp->list;
}
if ( ( send_packet_tmp == NULL ) || ( LSEQ_U32(send_packet_tmp->send_time, sn->send_time) ) )
list_add_tail( &sn->list, &send_list );
prof_stop( PROF_schedule_rcvd_ogm );
}
static void strip_packet( struct msg_buff *mb, unsigned char *pos, int32_t udp_len ) {
prof_start( PROF_strip_packet );
uint16_t left_p, ext_type, done_p, ext_p;
unsigned char *ext_a;
while ( udp_len >= (int32_t)sizeof( struct bat_packet_common ) &&
udp_len >= ((struct bat_packet_common *)pos)->bat_size<<2 ) {
if ( ((struct bat_packet_common *)pos)->bat_type == BAT_TYPE_OGM ) {
((struct bat_packet_ogm *)pos)->ogm_seqno =
ntohs( ((struct bat_packet_ogm *)pos)->ogm_seqno );
mb->bp.ogm = (struct bat_packet_ogm *)pos;
/* process optional extension messages */
left_p = udp_len - sizeof(struct bat_packet_ogm);
done_p = 0;
memset( mb->rcv_ext_len, 0, sizeof( mb->rcv_ext_len ) );
memset( mb->snd_ext_len, 0, sizeof( mb->snd_ext_len ) );
ext_type = 0;
ext_a = (pos + sizeof(struct bat_packet_ogm) + done_p );
ext_p = 0;
while ( done_p < left_p &&
done_p < ((((struct bat_packet_common *)pos)->bat_size)<<2) &&
((struct ext_packet*)(ext_a))->EXT_FIELD_MSG == YES &&
ext_type <= EXT_TYPE_MAX )
{
while( (ext_p + done_p) < left_p &&
((struct ext_packet*)(ext_a+ext_p))->EXT_FIELD_MSG == YES ) {
if ( ((struct ext_packet*)(ext_a+ext_p))->EXT_FIELD_TYPE == ext_type ) {
if ( ext_attribute[ext_type] & EXT_ATTR_TLV )
ext_p+= 4*((struct ext_packet*)(ext_a+ext_p))->EXT_FIELD_LEN_4B;
else
ext_p+= sizeof(struct ext_packet);
} else if (((struct ext_packet*)(ext_a+ext_p))->EXT_FIELD_TYPE > ext_type ) {
break;
} else {
dbg_mute( 75, DBGL_SYS, DBGT_ERR,
"Drop packet! Rcvd incompatible extension message order: "
"via NB %s OG? %s size? %i, ext_type %d",
mb->neigh_str, ipStr(mb->bp.ogm->orig ),
udp_len, ((struct ext_packet*)(ext_a+ext_p))->EXT_FIELD_TYPE );
prof_stop( PROF_strip_packet );
return;
}
}
done_p = done_p + ext_p;
if ( ext_p ) {
mb->rcv_ext_array[ext_type] = (struct ext_packet*)ext_a;
mb->rcv_ext_len[ext_type] = ext_p;
if ( ext_attribute[ext_type] & EXT_ATTR_KEEP ) {
mb->snd_ext_array[ext_type] = (struct ext_packet*)ext_a;
mb->snd_ext_len[ext_type] = ext_p;
}
}
ext_a = pos + sizeof(struct bat_packet_ogm) + done_p;
ext_p = 0;
ext_type++;
}
if ((int32_t) (sizeof (struct bat_packet_ogm) + done_p) !=
((((struct bat_packet_common *) pos)->bat_size) << 2)) {
udp_len = udp_len - ((((struct bat_packet_common *)pos)->bat_size)<<2);
pos = pos + ((((struct bat_packet_common *)pos)->bat_size)<<2);
dbg_mute( 60, DBGL_SYS, DBGT_ERR,
"Drop packet! Rcvd corrupted packet size via NB %s: "
"processed bytes: %d , indicated bytes %d, flags. %X, remaining bytes %d",
mb->neigh_str,
(int)(sizeof(struct bat_packet_ogm) + done_p),
((((struct bat_packet_common *)pos)->bat_size)<<2),
((struct bat_packet_ogm *)pos)->flags, udp_len );
prof_stop( PROF_strip_packet );
return;
}
dbgf_all( DBGT_INFO,
"rcvd OGM: flags. %X, remaining bytes %d", (mb->bp.ogm)->flags, udp_len );
/* prepare for next ogm and attached extension messages */
udp_len = udp_len - ((((struct bat_packet_common *)pos)->bat_size)<<2);
pos = pos + ((((struct bat_packet_common *)pos)->bat_size)<<2);
process_ogm( mb );
} else {
mb->bp.bpc = (struct bat_packet_common *)pos;
if ( cb_packet_hooks( ((struct bat_packet_common *)pos)->bat_type, mb ) == 0 ) {
dbg_mute( 47, DBGL_CHANGES, DBGT_WARN,
"Drop single unkown bat_type via NB %s, bat_type %X, size %i, "
"OG? %s, remaining len %d. Maybe you need an update",
mb->neigh_str,
((struct bat_packet_common *)pos)->bat_type,
(((struct bat_packet_common *)pos)->bat_size)<<2,
ipStr(((struct bat_packet_ogm *)pos)->orig), udp_len );
}
udp_len = udp_len - ((((struct bat_packet_common *)pos)->bat_size)<<2) ;
pos = pos + ((((struct bat_packet_common *)pos)->bat_size)<<2) ;
}
continue;
}
prof_stop( PROF_strip_packet );
}
static void process_packet( struct msg_buff *mb, unsigned char *pos, uint32_t rcvd_neighbor) {
prof_start( PROF_process_packet );
int32_t check_len, check_done, udp_len;
unsigned char *check_pos;
if ( mb->total_length < (int32_t)(sizeof(struct bat_header) + sizeof(struct bat_packet_common)) ) {
dbg_mute( 35, DBGL_SYS, DBGT_ERR, "Invalid packet length from %s, len %d",
ipStr(rcvd_neighbor), mb->total_length);
prof_stop( PROF_process_packet );
return;
}
// immediately drop my own packets
if ( rcvd_neighbor == mb->iif->if_addr ) {
dbgf_all( DBGT_INFO, "Drop packet: received my own broadcast iif %s", mb->iif->dev );
prof_stop( PROF_process_packet );
return;
}
addr_to_str( rcvd_neighbor, mb->neigh_str );
// immediately drop invalid packets...
// we acceppt longer packets than specified by pos->size to allow padding for equal packet sizes
if ( ((((struct bat_header *)pos)->size)<<2) < (int32_t)(sizeof(struct bat_header) + sizeof(struct bat_packet_common)) ||
(((struct bat_header *)pos)->version) != COMPAT_VERSION ||
((((struct bat_header *)pos)->size)<<2) > mb->total_length )
{
if ( mb->total_length >= (int32_t)(sizeof(struct bat_header) /*+ sizeof(struct bat_packet_common) */) ) {
dbg_mute( 60, DBGL_SYS, DBGT_WARN,
"Drop packet: rcvd incompatible batman packet via NB %s "
"(version? %i, reserved? %X, size? %i), "
"rcvd udp_len %d My version is %d",
mb->neigh_str,
((struct bat_header *)pos)->version,
((struct bat_header *)pos)->reserved,
((struct bat_header *)pos)->size,
mb->total_length, COMPAT_VERSION );
} else {
dbg_mute( 40, DBGL_SYS, DBGT_ERR, "Rcvd to small packet via NB %s, rcvd udp_len %i",
mb->neigh_str, mb->total_length );
}
prof_stop( PROF_process_packet );
return;
}
mb->neigh = rcvd_neighbor;
dbgf_all( DBGT_INFO, "version? %i, "
"reserved? %X, size? %i, rcvd udp_len %d via NB %s %s %s",
((struct bat_header *)pos)->version,