forked from jech/babeld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
babeld.c
1325 lines (1180 loc) · 37.9 KB
/
babeld.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) 2007, 2008 by Juliusz Chroboczek
Copyright (c) 2010 by Vincent Gross
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <time.h>
#include <signal.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include "babeld.h"
#include "util.h"
#include "net.h"
#include "kernel.h"
#include "interface.h"
#include "source.h"
#include "neighbour.h"
#include "route.h"
#include "xroute.h"
#include "message.h"
#include "resend.h"
#include "configuration.h"
#include "local.h"
#include "rule.h"
#include "version.h"
#ifdef HAVE_MBEDTLS
#include <mbedtls/ssl.h>
#include "dtls.h"
#endif
struct timeval now;
unsigned char myid[8];
int have_id = 0;
int debug = 0;
int link_detect = 0;
int all_wireless = 0;
int has_ipv6_subtrees = 0;
int default_wireless_hello_interval = -1;
int default_wired_hello_interval = -1;
int resend_delay = -1;
int random_id = 0;
int do_daemonise = 0;
int skip_kernel_setup = 0;
const char *logfile = NULL,
*pidfile = "/var/run/babeld.pid",
*state_file = "/var/lib/babel-state";
unsigned char *receive_buffer = NULL;
int receive_buffer_size = 0;
const unsigned char zeroes[16] = {0};
const unsigned char ones[16] =
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
int protocol_port;
unsigned char protocol_group[16];
int protocol_socket = -1;
int kernel_socket = -1;
#ifdef HAVE_MBEDTLS
int dtls_protocol_port;
int dtls_protocol_socket;
#endif
static int kernel_routes_changed = 0;
static int kernel_rules_changed = 0;
static int kernel_link_changed = 0;
static int kernel_addr_changed = 0;
struct timeval check_neighbours_timeout, check_interfaces_timeout;
static volatile sig_atomic_t exiting = 0, dumping = 0, reopening = 0;
static int accept_local_connections(void);
static void init_signals(void);
static void dump_tables(FILE *out);
static int
kernel_route_notify(struct kernel_route *route, void *closure)
{
kernel_routes_changed = 1;
return -1;
}
static int
kernel_addr_notify(struct kernel_addr *addr, void *closure)
{
kernel_addr_changed = 1;
return -1;
}
static int
kernel_link_notify(struct kernel_link *link, void *closure)
{
struct interface *ifp;
FOR_ALL_INTERFACES(ifp) {
if(strcmp(ifp->name, link->ifname) == 0) {
kernel_link_changed = 1;
return -1;
}
}
return 0;
}
static int
kernel_rule_notify(struct kernel_rule *rule, void *closure)
{
int i;
if(martian_prefix(rule->src, rule->src_plen))
return 0;
i = rule->priority - src_table_prio;
if(i < 0 || SRC_TABLE_NUM <= i)
return 0;
kernel_rules_changed = 1;
return -1;
}
int
main(int argc, char **argv)
{
struct sockaddr_in6 sin6;
int rc, fd, i, opt;
time_t expiry_time, source_expiry_time, kernel_dump_time;
const char **config_files = NULL;
int num_config_files = 0;
void *vrc;
unsigned int seed;
struct interface *ifp;
int is_unicast;
gettime(&now);
rc = read_random_bytes(&seed, sizeof(seed));
if(rc < 0) {
perror("read(random)");
seed = 42;
}
seed ^= (now.tv_sec ^ now.tv_usec);
srandom(seed);
parse_address("ff02:0:0:0:0:0:1:6", protocol_group, NULL);
protocol_port = 6696;
#if HAVE_MBEDTLS
dtls_protocol_port = 50000;
#endif
change_smoothing_half_life(4);
has_ipv6_subtrees = kernel_has_ipv6_subtrees();
while(1) {
opt = getopt(argc, argv,
"m:p:h:H:i:k:A:srS:d:g:G:lwz:M:t:T:c:C:DL:I:V");
if(opt < 0)
break;
switch(opt) {
case 'm':
rc = parse_address(optarg, protocol_group, NULL);
if(rc < 0)
goto usage;
if(protocol_group[0] != 0xff) {
fprintf(stderr,
"%s is not a multicast address\n", optarg);
goto usage;
}
if(protocol_group[1] != 2) {
fprintf(stderr,
"Warning: %s is not a link-local multicast address\n",
optarg);
}
break;
case 'p':
protocol_port = parse_nat(optarg);
if(protocol_port <= 0 || protocol_port > 0xFFFF)
goto usage;
break;
case 'h':
default_wireless_hello_interval = parse_thousands(optarg);
if(default_wireless_hello_interval <= 0 ||
default_wireless_hello_interval > 0xFFFF * 10)
goto usage;
break;
case 'H':
default_wired_hello_interval = parse_thousands(optarg);
if(default_wired_hello_interval <= 0 ||
default_wired_hello_interval > 0xFFFF * 10)
goto usage;
break;
case 'k':
kernel_metric = parse_nat(optarg);
if(kernel_metric < 0 || kernel_metric > 0xFFFF)
goto usage;
break;
case 'A':
allow_duplicates = parse_nat(optarg);
if(allow_duplicates < 0 || allow_duplicates > 0xFFFF)
goto usage;
break;
case 's':
split_horizon = 0;
break;
case 'r':
random_id = 1;
break;
case 'S':
state_file = optarg;
break;
case 'd':
debug = parse_nat(optarg);
if(debug < 0)
goto usage;
break;
case 'g':
case 'G':
if(opt == 'g')
local_server_write = 0;
else
local_server_write = 1;
if(optarg[0] == '/') {
local_server_port = -1;
free(local_server_path);
local_server_path = strdup(optarg);
} else {
local_server_port = parse_nat(optarg);
free(local_server_path);
local_server_path = NULL;
if(local_server_port <= 0 || local_server_port > 0xFFFF)
goto usage;
}
break;
case 'l':
link_detect = 1;
break;
case 'w':
all_wireless = 1;
break;
case 'z':
{
char *comma;
diversity_kind = (int)strtol(optarg, &comma, 0);
if(*comma == '\0')
diversity_factor = 128;
else if(*comma == ',')
diversity_factor = parse_nat(comma + 1);
else
goto usage;
if(diversity_factor <= 0 || diversity_factor > 256)
goto usage;
}
break;
case 'M': {
int l = parse_nat(optarg);
if(l < 0 || l > 3600)
goto usage;
change_smoothing_half_life(l);
break;
}
case 't':
export_table = parse_nat(optarg);
if(export_table < 0 || export_table > 0xFFFF)
goto usage;
break;
case 'T':
if(add_import_table(parse_nat(optarg)))
goto usage;
break;
case 'c':
config_files = realloc(config_files,
(num_config_files + 1) * sizeof(char*));
if(config_files == NULL) {
fprintf(stderr, "Couldn't allocate config file.\n");
exit(1);
}
config_files[num_config_files++] = optarg;
break;
case 'C':
rc = parse_config_from_string(optarg, strlen(optarg), NULL);
if(rc != CONFIG_ACTION_DONE) {
fprintf(stderr,
"Couldn't parse configuration from command line.\n");
exit(1);
}
break;
case 'D':
do_daemonise = 1;
break;
case 'L':
logfile = optarg;
break;
case 'I':
pidfile = optarg;
break;
case 'V':
fprintf(stderr, "%s\n", BABELD_VERSION);
exit(0);
break;
default:
goto usage;
}
}
if(num_config_files == 0) {
if(access("/etc/babeld.conf", F_OK) >= 0) {
config_files = malloc(sizeof(char*));
if(config_files == NULL) {
fprintf(stderr, "Couldn't allocate config file.\n");
exit(1);
}
config_files[num_config_files++] = "/etc/babeld.conf";
}
}
for(i = 0; i < num_config_files; i++) {
int line;
rc = parse_config_from_file(config_files[i], &line);
if(rc < 0) {
fprintf(stderr,
"Couldn't parse configuration from file %s "
"(error at line %d).\n",
config_files[i], line);
exit(1);
}
}
free(config_files);
if(default_wireless_hello_interval <= 0)
default_wireless_hello_interval = 4000;
default_wireless_hello_interval = MAX(default_wireless_hello_interval, 5);
if(default_wired_hello_interval <= 0)
default_wired_hello_interval = 4000;
default_wired_hello_interval = MAX(default_wired_hello_interval, 5);
resend_delay = 2000;
resend_delay = MIN(resend_delay, default_wireless_hello_interval / 2);
resend_delay = MIN(resend_delay, default_wired_hello_interval / 2);
resend_delay = MAX(resend_delay, 20);
if(do_daemonise) {
if(logfile == NULL)
logfile = "/var/log/babeld.log";
}
rc = reopen_logfile();
if(rc < 0) {
perror("reopen_logfile()");
exit(1);
}
fd = open("/dev/null", O_RDONLY);
if(fd < 0) {
perror("open(null)");
exit(1);
}
rc = dup2(fd, 0);
if(rc < 0) {
perror("dup2(null, 0)");
exit(1);
}
close(fd);
if(do_daemonise) {
rc = daemonise();
if(rc < 0) {
perror("daemonise");
exit(1);
}
}
if(pidfile && pidfile[0] != '\0') {
int pfd, len;
char buf[100];
len = snprintf(buf, 100, "%lu", (unsigned long)getpid());
if(len < 0 || len >= 100) {
perror("snprintf(getpid)");
exit(1);
}
pfd = open(pidfile, O_WRONLY | O_CREAT | O_EXCL, 0644);
if(pfd < 0) {
char buf[40];
snprintf(buf, 40, "creat(%s)", pidfile);
buf[39] = '\0';
perror(buf);
exit(1);
}
rc = write(pfd, buf, len);
if(rc < len) {
perror("write(pidfile)");
goto fail_pid;
}
close(pfd);
}
rc = kernel_setup(1);
if(rc < 0) {
fprintf(stderr, "kernel_setup failed.\n");
goto fail_pid;
}
rc = kernel_setup_socket(1);
if(rc < 0) {
fprintf(stderr, "kernel_setup_socket failed.\n");
kernel_setup(0);
goto fail_pid;
}
rc = finalise_config();
if(rc < 0) {
fprintf(stderr, "Couldn't finalise configuration.\n");
goto fail;
}
for(i = optind; i < argc; i++) {
vrc = add_interface(argv[i], NULL);
if(vrc == NULL)
goto fail;
}
if(interfaces == NULL) {
fprintf(stderr, "Eek... asked to run on no interfaces!\n");
goto fail;
}
if(!have_id && !random_id) {
/* We use all available interfaces here, since this increases the
chances of getting a stable router-id in case the set of Babel
interfaces changes. */
for(i = 1; i < 256; i++) {
char buf[IF_NAMESIZE], *ifname;
unsigned char eui[8];
ifname = if_indextoname(i, buf);
if(ifname == NULL)
continue;
rc = if_eui64(ifname, i, eui);
if(rc < 0)
continue;
memcpy(myid, eui, 8);
have_id = 1;
break;
}
}
if(!have_id) {
if(!random_id)
fprintf(stderr,
"Warning: couldn't find router id -- "
"using random value.\n");
rc = read_random_bytes(myid, 8);
if(rc < 0) {
perror("read(random)");
goto fail;
}
/* Clear group and global bits */
myid[0] &= ~3;
}
myseqno = (random() & 0xFFFF);
fd = open(state_file, O_RDONLY);
if(fd < 0 && errno != ENOENT)
perror("open(babel-state)");
rc = unlink(state_file);
if(fd >= 0 && rc < 0) {
perror("unlink(babel-state)");
/* If we couldn't unlink it, it's probably stale. */
close(fd);
fd = -1;
}
if(fd >= 0) {
char buf[100];
int s;
rc = read(fd, buf, 99);
if(rc < 0) {
perror("read(babel-state)");
} else {
buf[rc] = '\0';
rc = sscanf(buf, "%d\n", &s);
if(rc == 1 && s >= 0 && s <= 0xFFFF) {
myseqno = seqno_plus(s, 1);
} else {
fprintf(stderr, "Couldn't parse babel-state.\n");
}
}
close(fd);
fd = -1;
}
protocol_socket = babel_socket(protocol_port);
if(protocol_socket < 0) {
perror("Couldn't create link local socket");
goto fail;
}
#ifdef HAVE_MBEDTLS
dtls_protocol_socket = babel_socket(dtls_protocol_port);
if(dtls_protocol_socket < 0) {
perror("Couldn't create link local dtls socket");
goto fail;
}
#endif
if(local_server_port >= 0) {
local_server_socket = tcp_server_socket(local_server_port, 1);
if(local_server_socket < 0) {
perror("local_server_socket");
goto fail;
}
} else if(local_server_path) {
local_server_socket = unix_server_socket(local_server_path);
if(local_server_socket < 0) {
perror("local_server_socket");
goto fail;
}
}
init_signals();
rc = resize_receive_buffer(8192);
if(rc < 0)
goto fail;
if(receive_buffer == NULL)
goto fail;
#ifdef HAVE_MBEDTLS
rc = dtls_init();
if(rc)
goto fail;
#endif
check_interfaces();
rc = check_xroutes(0);
if(rc < 0)
fprintf(stderr, "Warning: couldn't check exported routes.\n");
rc = check_rules();
if(rc < 0)
fprintf(stderr, "Warning: couldn't check rules.\n");
kernel_routes_changed = 0;
kernel_rules_changed = 0;
kernel_link_changed = 0;
kernel_addr_changed = 0;
kernel_dump_time = now.tv_sec + roughly(30);
schedule_neighbours_check(5000, 1);
schedule_interfaces_check(30000, 1);
expiry_time = now.tv_sec + roughly(30);
source_expiry_time = now.tv_sec + roughly(300);
/* Make some noise so that others notice us, and send retractions in
case we were restarted recently */
FOR_ALL_INTERFACES(ifp) {
if(!if_up(ifp))
continue;
/* Apply jitter before we send the first message. */
usleep(roughly(10000));
gettime(&now);
send_hello(ifp);
send_wildcard_retraction(ifp);
flushupdates(ifp);
flushbuf(&ifp->buf, ifp);
}
FOR_ALL_INTERFACES(ifp) {
if(!if_up(ifp))
continue;
usleep(roughly(10000));
gettime(&now);
send_hello(ifp);
send_wildcard_retraction(ifp);
send_self_update(ifp);
send_multicast_request(ifp, NULL, 0, NULL, 0);
flushupdates(ifp);
flushbuf(&ifp->buf, ifp);
}
debugf("Entering main loop.\n");
while(1) {
struct timeval tv;
fd_set readfds;
struct neighbour *neigh;
gettime(&now);
tv = check_neighbours_timeout;
timeval_min(&tv, &check_interfaces_timeout);
timeval_min_sec(&tv, expiry_time);
timeval_min_sec(&tv, source_expiry_time);
timeval_min_sec(&tv, kernel_dump_time);
timeval_min(&tv, &resend_time);
FOR_ALL_INTERFACES(ifp) {
if(!if_up(ifp))
continue;
timeval_min(&tv, &ifp->buf.timeout);
timeval_min(&tv, &ifp->hello_timeout);
timeval_min(&tv, &ifp->update_timeout);
timeval_min(&tv, &ifp->update_flush_timeout);
}
FOR_ALL_NEIGHBOURS(neigh) {
#ifdef HAVE_MBEDTLS
struct dtls *dtls = neigh->buf.dtls;
if(dtls && (dtls->timer_status == 0 || dtls->timer_status == 1)) {
printf("dtls->timer_status == %d\n", dtls->timer_status);
if(dtls->timer_status == 0)
timeval_min(&tv, &dtls->int_time);
else
timeval_min(&tv, &dtls->fin_time);
}
#endif
timeval_min(&tv, &neigh->buf.timeout);
}
FD_ZERO(&readfds);
if(timeval_compare(&tv, &now) > 0) {
int maxfd = 0;
timeval_minus(&tv, &tv, &now);
FD_SET(protocol_socket, &readfds);
maxfd = MAX(maxfd, protocol_socket);
#ifdef HAVE_MBEDTLS
FD_SET(dtls_protocol_socket, &readfds);
maxfd = MAX(maxfd, dtls_protocol_socket);
FOR_ALL_NEIGHBOURS(neigh) {
struct dtls *dtls = neigh->buf.dtls;
if(dtls && dtls->fd != -1) {
FD_SET(dtls->fd, &readfds);
maxfd = MAX(maxfd, dtls->fd);
}
}
#endif
if(kernel_socket < 0) kernel_setup_socket(1);
if(kernel_socket >= 0) {
FD_SET(kernel_socket, &readfds);
maxfd = MAX(maxfd, kernel_socket);
}
if(local_server_socket >= 0 &&
num_local_sockets < MAX_LOCAL_SOCKETS) {
FD_SET(local_server_socket, &readfds);
maxfd = MAX(maxfd, local_server_socket);
}
for(i = 0; i < num_local_sockets; i++) {
FD_SET(local_sockets[i].fd, &readfds);
maxfd = MAX(maxfd, local_sockets[i].fd);
}
rc = select(maxfd + 1, &readfds, NULL, NULL, &tv);
if(rc < 0) {
if(errno != EINTR) {
perror("select");
sleep(1);
}
rc = 0;
FD_ZERO(&readfds);
}
}
gettime(&now);
if(exiting)
break;
if(kernel_socket >= 0 && FD_ISSET(kernel_socket, &readfds)) {
struct kernel_filter filter = {0};
filter.route = kernel_route_notify;
filter.addr = kernel_addr_notify;
filter.link = kernel_link_notify;
filter.rule = kernel_rule_notify;
kernel_callback(&filter);
}
if(FD_ISSET(protocol_socket, &readfds)) {
rc = babel_recv(protocol_socket,
receive_buffer, receive_buffer_size,
(struct sockaddr*)&sin6, sizeof(sin6),
&is_unicast);
if(rc < 0) {
if(errno != EAGAIN && errno != EINTR) {
perror("recv");
sleep(1);
}
} else {
FOR_ALL_INTERFACES(ifp) {
if(!if_up(ifp))
continue;
if(ifp->ifindex == sin6.sin6_scope_id) {
printf("Received unprotected packet size:%d\n", rc);
#ifdef HAVE_MBEDTLS
/* We allow DTLS to be enabled per interface.
If a packet is received on an interface
where DTLS hasn’t been enabled, the packet
is considered secure. */
if(ifp->flags & IF_DTLS) {
if(!is_unicast)
parse_packet((unsigned char*)&sin6.sin6_addr,
ifp, receive_buffer, rc, 1);
} else {
parse_packet((unsigned char*)&sin6.sin6_addr, ifp,
receive_buffer, rc, 0);
}
#else
parse_packet((unsigned char*)&sin6.sin6_addr, ifp,
receive_buffer, rc, 0);
#endif
VALGRIND_MAKE_MEM_UNDEFINED(receive_buffer,
receive_buffer_size);
break;
}
}
}
}
#ifdef HAVE_MBEDTLS
#define dtls_handle_packet(fd) \
do { \
rc = babel_recv((fd), receive_buffer, receive_buffer_size, \
(struct sockaddr*)&sin6, sizeof(sin6), \
&is_unicast); \
if(rc < 0) { \
if(errno != EAGAIN && errno != EINTR) { \
perror("recv"); \
sleep(1); \
} \
} else { \
FOR_ALL_INTERFACES(ifp) { \
if(!if_up(ifp)) \
continue; \
if(ifp->ifindex == sin6.sin6_scope_id) { \
printf("Received protected packet size:%d" \
" is_unicast:%d\n", rc, is_unicast); \
if(ifp->flags & IF_DTLS && is_unicast) { \
dtls_parse_packet(&sin6, ifp, \
receive_buffer, rc); \
} \
VALGRIND_MAKE_MEM_UNDEFINED(receive_buffer, \
receive_buffer_size); \
break; \
} \
} \
} \
} while(0) \
if(FD_ISSET(dtls_protocol_socket, &readfds)) {
dtls_handle_packet(dtls_protocol_socket);
}
FOR_ALL_NEIGHBOURS(neigh) {
struct dtls *dtls = neigh->buf.dtls;
if(dtls && dtls->fd != -1 && FD_ISSET(dtls->fd, &readfds)) {
dtls_handle_packet(dtls->fd);
}
}
#undef dtls_handle_packet
#endif
if(local_server_socket >= 0 && FD_ISSET(local_server_socket, &readfds))
accept_local_connections();
i = 0;
while(i < num_local_sockets) {
if(FD_ISSET(local_sockets[i].fd, &readfds)) {
rc = local_read(&local_sockets[i]);
if(rc <= 0) {
if(rc < 0) {
if(errno == EINTR || errno == EAGAIN)
continue;
perror("read(local_socket)");
}
local_socket_destroy(i);
}
}
i++;
}
if(reopening) {
kernel_dump_time = now.tv_sec;
check_neighbours_timeout = now;
expiry_time = now.tv_sec;
rc = reopen_logfile();
if(rc < 0) {
perror("reopen_logfile");
break;
}
reopening = 0;
}
if(kernel_link_changed || kernel_addr_changed) {
check_interfaces();
kernel_link_changed = 0;
}
if(kernel_routes_changed || kernel_addr_changed ||
kernel_rules_changed || now.tv_sec >= kernel_dump_time) {
rc = check_xroutes(1);
if(rc < 0)
fprintf(stderr, "Warning: couldn't check exported routes.\n");
rc = check_rules();
if(rc < 0)
fprintf(stderr, "Warning: couldn't check rules.\n");
kernel_routes_changed = kernel_rules_changed =
kernel_addr_changed = 0;
if(kernel_socket >= 0)
kernel_dump_time = now.tv_sec + roughly(300);
else
kernel_dump_time = now.tv_sec + roughly(30);
}
if(timeval_compare(&check_neighbours_timeout, &now) < 0) {
int msecs;
msecs = check_neighbours();
/* Multiply by 3/2 to allow neighbours to expire. */
msecs = MAX(3 * msecs / 2, 10);
schedule_neighbours_check(msecs, 1);
}
if(timeval_compare(&check_interfaces_timeout, &now) < 0) {
check_interfaces();
schedule_interfaces_check(30000, 1);
}
if(now.tv_sec >= expiry_time) {
expire_routes();
expire_resend();
expiry_time = now.tv_sec + roughly(30);
}
if(now.tv_sec >= source_expiry_time) {
expire_sources();
source_expiry_time = now.tv_sec + roughly(300);
}
FOR_ALL_INTERFACES(ifp) {
if(!if_up(ifp))
continue;
if(timeval_compare(&now, &ifp->hello_timeout) >= 0)
send_hello(ifp);
if(timeval_compare(&now, &ifp->update_timeout) >= 0)
send_update(ifp, 0, NULL, 0, NULL, 0);
if(timeval_compare(&now, &ifp->update_flush_timeout) >= 0)
flushupdates(ifp);
}
if(resend_time.tv_sec != 0) {
if(timeval_compare(&now, &resend_time) >= 0)
do_resend();
}
FOR_ALL_INTERFACES(ifp) {
if(!if_up(ifp))
continue;
if(ifp->buf.timeout.tv_sec != 0) {
if(timeval_compare(&now, &ifp->buf.timeout) >= 0) {
flushupdates(ifp);
flushbuf(&ifp->buf, ifp);
}
}
}
FOR_ALL_NEIGHBOURS(neigh) {
#ifdef HAVE_MBEDTLS
if(neigh->ifp->flags & IF_DTLS) {
struct dtls *dtls = neigh->buf.dtls;
if(dtls && (dtls->timer_status == 0 || dtls->timer_status == 1)) {
if(timeval_compare(&now, &dtls->fin_time) >= 0) {
dtls->timer_status = 2;
printf("DTLS: timer 2 expired\n");
rc = dtls_handshake(neigh);
if(rc) {
fprintf(stderr, "DTLS: timer handshake failed?\n");
}
} else if(timeval_compare(&now, &dtls->int_time) >= 0) {
dtls->timer_status = 1;
}
}
/* Do we want to drop this packet if the dtls state is
* not ready? */
if(neigh->buf.timeout.tv_sec != 0) {
if(timeval_compare(&now, &neigh->buf.timeout) >= 0) {
if(dtls)
dtls_flushbuf(&neigh->buf, neigh->ifp);
else
dtls_dropbuf(&neigh->buf);
}
}
} else
#endif
if(neigh->buf.timeout.tv_sec != 0) {
if(timeval_compare(&now, &neigh->buf.timeout) >= 0) {
flushbuf(&neigh->buf, neigh->ifp);
}
}
}
if(UNLIKELY(debug || dumping)) {
dump_tables(stdout);
dumping = 0;
}
}
debugf("Exiting...\n");
usleep(roughly(10000));
gettime(&now);
/* We need to flush so interface_updown won't try to reinstall. */
flush_all_routes();
FOR_ALL_INTERFACES(ifp) {
if(!if_up(ifp))
continue;
send_wildcard_retraction(ifp);
/* Make sure that we expire quickly from our neighbours'
association caches. */
send_multicast_hello(ifp, 10, 1);
flushbuf(&ifp->buf, ifp);
usleep(roughly(1000));
gettime(&now);
}
FOR_ALL_INTERFACES(ifp) {
if(!if_up(ifp))
continue;
/* Make sure they got it. */
send_wildcard_retraction(ifp);
send_multicast_hello(ifp, 1, 1);
flushbuf(&ifp->buf, ifp);
usleep(roughly(10000));
gettime(&now);
interface_updown(ifp, 0);
}
release_tables();
kernel_setup_socket(0);
kernel_setup(0);
#ifdef HAVE_MBEDTLS
dtls_free();
#endif
fd = open(state_file, O_WRONLY | O_TRUNC | O_CREAT, 0644);
if(fd < 0) {
perror("creat(babel-state)");
unlink(state_file);
} else {
char buf[10];
rc = snprintf(buf, 10, "%d\n", (int)myseqno);
if(rc < 0 || rc >= 10) {
fprintf(stderr, "write(babel-state): overflow.\n");
unlink(state_file);
} else {
rc = write(fd, buf, rc);
if(rc < 0) {
perror("write(babel-state)");
unlink(state_file);
}
fsync(fd);
}
close(fd);
}
if(local_server_socket >= 0 && local_server_path) {
unlink(local_server_path);
free(local_server_path);
}
if(pidfile)