-
Notifications
You must be signed in to change notification settings - Fork 2
/
hna.c
3689 lines (2657 loc) · 147 KB
/
hna.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) 2010 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 "avl.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <asm/types.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <endian.h>
#include <sys/ioctl.h>
//#include <net/if.h>
//#include <linux/if_tun.h> /* TUNSETPERSIST, ... */
//#include <linux/if.h> /* ifr_if, ifr_tun */
#include <fcntl.h> /* open(), O_RDWR */
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include "bmx.h"
#include "msg.h"
#include "ip.h"
#include "plugin.h"
#include "hna.h"
#include "tools.h"
#include "metrics.h"
#include "schedule.h"
#define CODE_CATEGORY_NAME "hna"
static AVL_TREE(global_uhna_tree, struct hna_node, key );
static AVL_TREE(local_uhna_tree, struct hna_node, key );
AVL_TREE(tun_in_tree, struct tun_in_node, nameKey); // configured tun_in tunnels
static AVL_TREE(tun_search_tree, struct tun_search_node, nameKey); // configured tun_out names searches
//static AVL_TREE(tun_search_net_tree, struct tun_search_node, tunSearchKey); //REMOVE // configured tun_out networks searches
static AVL_TREE(tun_bit_tree, struct tun_bit_node, tunBitKey); // identified matching bits (peaces) of tun_search and tun_net trees
static AVL_TREE(tun_net_tree, struct tun_net_node, tunNetKey); // rcvd tun_out network advs
static AVL_TREE(tun_out_tree, struct tun_out_node, tunOutKey); // rcvd tun_out advs
static AVL_TREE(tun_catch_tree, struct tun_dev_node, tunCatchKey); // active tun_out tunnels
LIST_SIMPEL(tunXin6_net_adv_list_list, struct tunXin6_net_adv_list_node, list, list);
static const struct tun_net_key ZERO_TUN_NET_KEY = {.ton = NULL};
//static const struct tun_catch_key ZERO_TUN_DEV_KEY = {.srcAf=0};
//static struct net_key tun4_address = ZERO_NET_KEY_INIT;
//char* tun4_dev = NULL;
//static struct net_key tun6_address = ZERO_NET_KEY_INIT;
//char* tun6_dev = NULL;
IDM_T (*hna_configure_niit4to6) (IDM_T del, struct net_key *key) = NULL;
IDM_T (*hna_configure_niit6to4) (IDM_T del, struct net_key *key) = NULL;
IFNAME_T tun_name_prefix = {{DEF_TUN_NAME_PREFIX}};
static int32_t tun_out_delay = DEF_TUN_OUT_DELAY;
static int32_t tun_out_mtu = DEF_TUN_OUT_MTU;
static int32_t tun_dedicated_to = DEF_TUN_OUT_TO;
STATIC_FUNC
void configure_tun_bit(uint8_t del, struct tun_bit_node *tbn, IDM_T asDfltTun);
char* bmx6RouteBits2String(uint64_t bmx6_route_bits)
{
static char r[BMX6_ROUTE_MAX_SUPP+2];
//memset(r, ' ', sizeof(r));
//r[BMX6_ROUTE_MAX] = 0;
uint8_t t, p=0;
for (t = 0; t <= BMX6_ROUTE_MAX_SUPP; t++) {
if (bit_get((uint8_t*) &bmx6_route_bits, sizeof (bmx6_route_bits) * 8, t))
r[p++] = bmx6_rt_dict[t].sys2Char ? bmx6_rt_dict[t].sys2Char : '?';
}
if(p)
r[p] = 0;
else
sprintf(r, "---");
return r;
}
void set_tunXin6_net_adv_list(uint8_t del, struct list_head *adv_list)
{
struct list_node *list_pos, *tmp_pos, *prev_pos = (struct list_node *)&tunXin6_net_adv_list_list;
struct tunXin6_net_adv_list_node *n;
list_for_each_safe(list_pos, tmp_pos, &tunXin6_net_adv_list_list)
{
n = list_entry(list_pos, struct tunXin6_net_adv_list_node, list);
if (adv_list == n->adv_list) {
if ( del ) {
list_del_next(( &tunXin6_net_adv_list_list), prev_pos);
debugFree( n, -300516 );
return;
} else {
cleanup_all(-501440);
}
} else {
prev_pos = &n->list;
}
}
assertion(-501441, (!del));
n = debugMallocReset(sizeof ( struct tunXin6_net_adv_list_node), -300517);
n->adv_list = adv_list;
list_add_tail((&tunXin6_net_adv_list_list), &n->list);
}
STATIC_FUNC
IDM_T configure_route(IDM_T del, struct orig_node *on, struct net_key *key)
{
assertion(-501331, (key->af == AF_CFG));
// update network routes:
if (del) {
return iproute(IP_ROUTE_HNA, DEL, NO, key, BMX_TABLE_HNA, 0, 0, NULL, NULL, DEF_IP_METRIC, NULL);
} else {
struct link_dev_node *lndev = on->curr_rt_lndev;
assertion(-500820, (lndev));
ASSERTION(-500239, (avl_find(&link_dev_tree, &(lndev->key))));
assertion(-500579, (lndev->key.dev->if_llocal_addr));
return iproute(IP_ROUTE_HNA, ADD, NO, key, BMX_TABLE_HNA, 0,
lndev->key.dev->if_llocal_addr->ifa.ifa_index, &(lndev->key.link->link_ip),
(key->af == AF_INET ? (&(self->primary_ip)) : NULL), DEF_IP_METRIC, NULL);
}
}
STATIC_FUNC
uint8_t set_hna_to_key(struct net_key *key, struct description_msg_hna4 *uhna4, struct description_msg_hna6 *uhna6)
{
uint8_t flags;
if (uhna4) {
IPX_T ipX = ip4ToX(uhna4->ip4);
setNet(key, AF_INET, uhna4->prefixlen, &ipX);
flags = uhna4->flags;
} else {
setNet(key, AF_INET6, uhna6->prefixlen, &uhna6->ip6);
flags = uhna6->flags;
}
ip_netmask_validate(&key->ip, key->mask, key->af, YES);
return flags;
}
STATIC_FUNC
int _create_tlv_hna(uint8_t* data, uint16_t max_size, uint16_t pos, struct net_key *net, uint8_t flags)
{
TRACE_FUNCTION_CALL;
int i;
uint16_t msg_size = net->af == AF_INET ? sizeof (struct description_msg_hna4) : sizeof (struct description_msg_hna6);
if ((pos + msg_size) > max_size) {
dbgf_sys(DBGT_ERR, "unable to announce %s! Exceeded %s=%d", netAsStr(net), ARG_UDPD_SIZE, max_size);
return pos;
}
dbgf_track(DBGT_INFO, "%s", netAsStr(net));
assertion(-500610, (!(net->af == AF_INET6 && is_ip_net_equal(&net->ip, &IP6_LINKLOCAL_UC_PREF, IP6_LINKLOCAL_UC_PLEN, AF_INET6))));
// this should be catched during configuration!!
if (net->af == AF_INET) {
struct description_msg_hna4 *msg4 = ((struct description_msg_hna4 *) data);
struct description_msg_hna4 hna4;
memset( &hna4, 0, sizeof(hna4));
hna4.ip4 = ipXto4(net->ip);
hna4.prefixlen = net->mask;
hna4.flags = flags;
for (i = 0; i < pos / msg_size; i++) {
if (!memcmp(&(msg4[i]), &hna4, sizeof (struct description_msg_hna4)))
return pos;
}
msg4[i] = hna4;
} else {
struct description_msg_hna6 *msg6 = ((struct description_msg_hna6 *) data);
struct description_msg_hna6 hna6;
memset( &hna6, 0, sizeof(hna6));
hna6.ip6 = net->ip;
hna6.prefixlen = net->mask;
hna6.flags = flags;
for (i = 0; i < pos / msg_size; i++) {
if (!memcmp(&(msg6[i]), &hna6, sizeof (struct description_msg_hna6)))
return pos;
}
msg6[i] = hna6;
}
return (pos + msg_size);
}
STATIC_FUNC
IFNAME_T tun_out_get_free_name(char *typename, char *postname)
{
assertion(-501446, (strlen(tun_name_prefix.str) + strlen(typename) + 4 <= IFNAMSIZ - 1));
static uint16_t tun_idx = 0;
uint16_t tun_idx_check = tun_idx;
IFNAME_T name;
static char ifNameChars[] = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
memset(&name, 0, sizeof (name));
snprintf(name.str + strlen(name.str), IFNAMSIZ - 1 - strlen(name.str), "%s", tun_name_prefix.str);
assertion(-501447, (IFNAMSIZ - 1 > strlen(name.str)));
snprintf(name.str + strlen(name.str), IFNAMSIZ - 1 - strlen(name.str), "%s", typename);
if (IFNAMSIZ - 1 > strlen(name.str))
snprintf(name.str + strlen(name.str), IFNAMSIZ - 1 - strlen(name.str), "%s", postname);
//check if tun->name is already used:
check_string(name.str, ifNameChars, '_');
if (!kernel_dev_exists(name.str))
return name;
do {
memset(&name, 0, sizeof (name));
snprintf(name.str + strlen(name.str), IFNAMSIZ - 5 - strlen(name.str), "%s", tun_name_prefix.str);
assertion(-501448, (IFNAMSIZ - 5 > strlen(name.str)));
snprintf(name.str + strlen(name.str), IFNAMSIZ - 5 - strlen(name.str), "%s", typename);
if (IFNAMSIZ - 5 > strlen(name.str))
snprintf(name.str + strlen(name.str), IFNAMSIZ - 5 - strlen(name.str), "%s", postname);
assertion(-501449, (IFNAMSIZ - 5 >= strlen(name.str) ));
snprintf(name.str + strlen(name.str), IFNAMSIZ - 1 - strlen(name.str), "%.4X", tun_idx++);
//check if tun->name is already used:
check_string(name.str, ifNameChars, '_');
if (!kernel_dev_exists(name.str))
return name;
} while(tun_idx != tun_idx_check);
assertion(-501450, (0));
memset(&name, 0, sizeof (name));
return name;
}
STATIC_FUNC
IDM_T configure_tunnel_in(uint8_t del, struct tun_in_node *tin, int16_t tun6Id)
{
TRACE_FUNCTION_CALL;
assertion(-501523, IMPLIES(!del, is_ip_set(&tin->remote)));
assertion(-501341, IMPLIES(!del, (is_ip_set(&self->primary_ip))));
assertion(-501311, IMPLIES(tin->upIfIdx, tin->nameKey.str[0]));
assertion(-501342, IMPLIES(tin->upIfIdx, del));
assertion(-501368, IMPLIES(del,((tin->tun6Id >= 0) == (tin->upIfIdx > 0))));
assertion(-501369, IMPLIES(!del,((tun6Id >= 0) && (tin->upIfIdx == 0))));
if (del && tin->upIfIdx) {
IDM_T result = kernel_tun_del(tin->nameKey.str);
assertion(-501451, (result==SUCCESS));
tin->upIfIdx = 0;
tin->tun6Id = -1;
my_description_changed = YES;
} else if (!del && !tin->upIfIdx) {
IPX_T *local = &self->primary_ip;
IPX_T *remote = &tin->remote;
if (!is_ip_set(remote) || is_ip_local(remote) ||
(tin->ingressPrefix46[0].mask && find_overlapping_hna(&tin->ingressPrefix46[0].ip, tin->ingressPrefix46[0].mask, self))) {
dbgf_sys(DBGT_WARN, "FAILED creating tun remoteIp=%s", ip6AsStr(&tin->remote));
return FAILURE;
}
assertion(-501312, (strlen(tin->nameKey.str)));
if ((tin->upIfIdx = kernel_tun_add(tin->nameKey.str, IPPROTO_IP, local, remote)) > 0) {
tin->tun6Id = tun6Id;
if (tin->tunAddr46[1].mask)
kernel_set_addr(ADD, tin->upIfIdx, AF_INET, &tin->tunAddr46[1].ip, 32, NO /*deprecated*/);
if (tin->tunAddr46[0].mask)
kernel_set_addr(ADD, tin->upIfIdx, AF_INET6, &tin->tunAddr46[0].ip, 128, NO /*deprecated*/);
my_description_changed = YES;
}
}
return (XOR(del, tin->upIfIdx)) ? SUCCESS : FAILURE;
}
STATIC_FUNC
void reconfigure_tun_ins(void)
{
struct avl_node *an;
struct tun_in_node *tin;
for (an = NULL; (tin = avl_iterate_item(&tun_in_tree, &an));)
configure_tunnel_in(DEL, tin, -1);
if (primary_phy && primary_phy->if_link) {
struct net_key autoRemotePrefix = bmx6AutoEUI64Ip6(primary_phy->if_link->addr, &autoconf_prefix_cfg);
autoRemotePrefix.ip.s6_addr[6] = DEF_TUN_REMOTE_BYTE6;
int16_t m = 0;
for (an = NULL; (tin = avl_iterate_item(&tun_in_tree, &an));) {
if (!tin->remote_manual) {
tin->remote = autoRemotePrefix.ip;
tin->remote.s6_addr[7] = m;
}
configure_tunnel_in(ADD, tin, m++);
assertion(-501237, (tin->upIfIdx && tin->tun6Id >= 0));
}
}
}
STATIC_FUNC
int create_description_tlv_hna(struct tx_frame_iterator *it)
{
TRACE_FUNCTION_CALL;
assertion(-500765, (it->frame_type == BMX_DSC_TLV_UHNA4 || it->frame_type == BMX_DSC_TLV_UHNA6));
uint8_t *data = tx_iterator_cache_msg_ptr(it);
uint16_t max_size = tx_iterator_cache_data_space_max(it);
uint8_t family = it->frame_type == BMX_DSC_TLV_UHNA4 ? AF_INET : AF_INET6;
uint8_t max_plen = (family == AF_INET ? 32 : 128);
int pos = 0;
struct avl_node *an;
struct dev_node *dev;
struct hna_node *un;
if (!is_ip_set(&self->primary_ip))
return TLV_TX_DATA_IGNORED;
pos = _create_tlv_hna(data, max_size, pos, setNet(NULL, family, max_plen, &self->primary_ip), 0);
for (an = NULL; (dev = avl_iterate_item(&dev_ip_tree, &an));) {
if (dev->active && dev->announce)
pos = _create_tlv_hna(data, max_size, pos, setNet(NULL, family, max_plen, &dev->if_global_addr->ip_addr), 0);
}
if (family == AF_INET6) {
struct tun_in_node *tin;
for (an = NULL; (tin = avl_iterate_item(&tun_in_tree, &an));) {
if (tin->upIfIdx && tin->tun6Id >= 0) {
assertion(-501237, (tin->upIfIdx && tin->tun6Id >= 0));
pos = _create_tlv_hna(data, max_size, pos, setNet(NULL, AF_INET6, 128, &tin->remote), DESC_MSG_HNA_FLAG_NO_ROUTE);
}
}
}
for (an = NULL; (un = avl_iterate_item(&local_uhna_tree, &an));)
pos = _create_tlv_hna(data, max_size, pos, &un->key, 0);
return pos;
}
STATIC_FUNC
void configure_hna(IDM_T del, struct net_key* key, struct orig_node *on, uint8_t flags)
{
TRACE_FUNCTION_CALL;
struct hna_node *un = avl_find_item( &global_uhna_tree, key );
assertion(-500589, (on));
assertion(-500236, ((del && un) != (!del && !un)));
// update uhna_tree:
if ( del ) {
assertion(-500234, (on == un->on));
avl_remove(&global_uhna_tree, &un->key, -300212);
ASSERTION( -500233, (!avl_find( &global_uhna_tree, key)) ); // there should be only one element with this key
if (on == self)
avl_remove(&local_uhna_tree, &un->key, -300213);
} else {
un = debugMalloc( sizeof (struct hna_node), -300090 );
un->key = *key;
un->on = on;
un->flags = flags;
avl_insert(&global_uhna_tree, un, -300149);
if (on == self)
avl_insert(&local_uhna_tree, un, -300150);
}
if (on == self) {
// update throw routes:
/*
* HNAs should not be thrown. They are a promise to be routed!
* The correct solution would be to drop conflicting OGMs
*
if (policy_routing == POLICY_RT_ENABLED && ip_throw_rules_cfg) {
assertion(-501333, (key->af == AF_CFG));
iproute(IP_THROW_MY_HNA, del, NO, key, RT_TABLE_HNA, 0, (key->af == AF_INET6 ? dev_lo_idx : 0), 0, 0, 0, NULL);
iproute(IP_THROW_MY_HNA, del, NO, key, RT_TABLE_TUN, 0, (key->af == AF_INET6 ? dev_lo_idx : 0), 0, 0, 0, NULL);
}
*/
} else if (on->curr_rt_lndev && !(flags & DESC_MSG_HNA_FLAG_NO_ROUTE)) {
configure_route(del, on, key);
if (hna_configure_niit4to6)
(*hna_configure_niit4to6)(del, key);
}
if (del)
debugFree(un, -300089);
}
STATIC_FUNC
struct hna_node * find_orig_hna(struct orig_node *on)
{
struct hna_node *un;
struct avl_node *it = NULL;
while ((un = avl_iterate_item(&global_uhna_tree, &it)) && un->on != on);
return un;
}
struct hna_node * find_overlapping_hna( IPX_T *ipX, uint8_t prefixlen, struct orig_node *except )
{
struct hna_node *un;
struct avl_node *it = NULL;
while ((un = avl_iterate_item(&global_uhna_tree, &it))) {
assertion(-501353, (un->on));
if (un->on != except && is_ip_net_equal(ipX, &un->key.ip, XMIN(prefixlen, un->key.mask), AF_CFG))
return un;
}
return NULL;
}
static struct net_key *hna_net_keys = NULL;
static uint32_t hna_net_key_elements = 0;
STATIC_FUNC
int process_description_tlv_hna(struct rx_frame_iterator *it)
{
TRACE_FUNCTION_CALL;
ASSERTION(-500357, (it->frame_type == BMX_DSC_TLV_UHNA4 || it->frame_type == BMX_DSC_TLV_UHNA6));
assertion(-500588, (it->on));
struct orig_node *on = it->on;
uint8_t op = it->op;
uint8_t family = (it->frame_type == BMX_DSC_TLV_UHNA4 ? AF_INET : AF_INET6);
uint32_t hna_net_curr = 0;
assertion(-600004, (on != self ||
op == TLV_OP_CUSTOM_NIIT6TO4_ADD || op == TLV_OP_CUSTOM_NIIT6TO4_DEL ||
op == TLV_OP_CUSTOM_NIIT4TO6_ADD || op == TLV_OP_CUSTOM_NIIT4TO6_DEL));
if (AF_CFG != family) {
dbg_mute(10, DBGL_CHANGES, DBGT_WARN, "%s NOT supported in this mode", family2Str(family));
return TLV_RX_DATA_IGNORED;
}
if (op == TLV_OP_NEW || op == TLV_OP_DEL) {
struct hna_node *un;
while ((un = find_orig_hna(on)))
configure_hna(DEL, &un->key, on, un->flags);
on->primary_ip = ZERO_IP;
ipXToStr(family, &ZERO_IP, on->primary_ip_str);
if (op == TLV_OP_DEL)
return it->frame_msgs_length;
}
uint16_t msg_size = it->handl->min_msg_size;
uint16_t pos;
for (pos = 0; pos < it->frame_msgs_length; pos += msg_size) {
struct net_key key;
uint8_t flags;
if (it->frame_type == BMX_DSC_TLV_UHNA4)
flags = set_hna_to_key(&key, (struct description_msg_hna4 *) (it->frame_data + pos), NULL);
else
flags = set_hna_to_key(&key, NULL, (struct description_msg_hna6 *) (it->frame_data + pos));
dbgf_track(DBGT_INFO, "%s %s %s %s=%s",
tlv_op_str(op), family2Str(family), globalIdAsString(&on->global_id), ARG_UHNA, netAsStr(&key));
if (op == TLV_OP_TEST) {
struct hna_node *un = NULL;
if (!is_ip_valid(&key.ip, family) ||
is_ip_net_equal(&key.ip, &IP6_LINKLOCAL_UC_PREF, IP6_LINKLOCAL_UC_PLEN, AF_INET6) ||
(un = find_overlapping_hna(&key.ip, key.mask, on))) {
dbgf_sys(DBGT_ERR, "global_id=%s %s=%s blocked (by global_id=%s)",
globalIdAsString(&on->global_id), ARG_UHNA, netAsStr(&key),
un ? globalIdAsString(&un->on->global_id) : "???");
return TLV_RX_DATA_BLOCKED;
}
// check if node announces the same key twice:
uint32_t i;
for (i = 0; i < hna_net_curr; i++) {
if (!memcmp(&hna_net_keys[i], &key, sizeof (key))) {
dbgf_sys(DBGT_ERR, "global_id=%s %s=%s blocked due to duplicate announcement",
globalIdAsString(&on->global_id), ARG_UHNA, netAsStr(&key));
return TLV_RX_DATA_BLOCKED;
}
}
if (hna_net_key_elements < (i + 1))
hna_net_keys = debugRealloc(hna_net_keys, (i + 1) * sizeof (key), -300398);
memcpy(&hna_net_keys[i], &key, sizeof (key));
hna_net_key_elements = (hna_net_curr = (i + 1));
} else if (op == TLV_OP_NEW) {
ASSERTION( -500359, (!avl_find(&global_uhna_tree, &key)));
if (pos == 0) {
on->primary_ip = key.ip;
ipFToStr( &key.ip, on->primary_ip_str);
}
configure_hna(ADD, &key, on, flags);
} else if (op >= TLV_OP_CUSTOM_MIN) {
dbgf_all(DBGT_INFO, "configure_niit... op=%d global_id=%s blocked=%d",
op, globalIdAsString(&on->global_id), on->blocked);
if (!on->blocked && !(flags & DESC_MSG_HNA_FLAG_NO_ROUTE)) {
//ASSERTION(-501314, (avl_find(&global_uhna_tree, &key)));
if (op == TLV_OP_CUSTOM_NIIT6TO4_ADD) {
if (hna_configure_niit6to4)
(*hna_configure_niit6to4)(ADD, &key);
} else if (op == TLV_OP_CUSTOM_NIIT6TO4_DEL) {
if (hna_configure_niit6to4)
(*hna_configure_niit6to4)(DEL, &key);
} else if (op == TLV_OP_CUSTOM_NIIT4TO6_ADD) {
if (hna_configure_niit4to6)
(*hna_configure_niit4to6)(ADD, &key);
} else if (op == TLV_OP_CUSTOM_NIIT4TO6_DEL) {
if (hna_configure_niit4to6)
(*hna_configure_niit4to6)(DEL, &key);
} else if (op == TLV_OP_CUSTOM_HNA_ROUTE_DEL) {
if (hna_configure_niit4to6)
(*hna_configure_niit4to6)(DEL, &key);
configure_route(DEL, on, &key);
} else if (op == TLV_OP_CUSTOM_HNA_ROUTE_ADD) {
configure_route(ADD, on, &key);
if (hna_configure_niit4to6)
(*hna_configure_niit4to6)(ADD, &key);
} else {
assertion(-501315, (NO));
}
}
} else {
assertion( -500369, (NO));
}
}
dbgf((it->frame_msgs_length == pos) ? DBGL_ALL : DBGL_SYS, (it->frame_msgs_length == pos) ? DBGT_INFO : DBGT_ERR,
"processed %d bytes frame_msgs_len=%d msg_size=%d", pos, it->frame_msgs_length, msg_size);
return it->frame_msgs_length;
}
STATIC_FUNC
int32_t opt_uhna(uint8_t cmd, uint8_t _save, struct opt_type *opt, struct opt_parent *patch, struct ctrl_node *cn)
{
TRACE_FUNCTION_CALL;
if ( cmd == OPT_ADJUST || cmd == OPT_CHECK || cmd == OPT_APPLY ) {
struct net_key hna = ZERO_NETCFG_KEY;
struct hna_node *un;
dbgf_all(DBGT_INFO, "af_cfg=%s diff=%d cmd=%s save=%d opt=%s patch=%s",
family2Str(hna.af), patch->diff, opt_cmd2str[cmd], _save, opt->name, patch->val);
if (str2netw(patch->val, &hna.ip, cn, &hna.mask, &hna.af, NO) == FAILURE || !is_ip_valid(&hna.ip, hna.af)) {
dbgf_cn(cn, DBGL_SYS, DBGT_ERR, "invalid prefix: %s", patch->val);
return FAILURE;
}
set_opt_parent_val(patch, netAsStr(&hna));
if (cmd == OPT_CHECK || cmd == OPT_APPLY) {
if (patch->diff != DEL && (un = find_overlapping_hna(&hna.ip, hna.mask, self))) {
dbg_cn(cn, DBGL_CHANGES, DBGT_ERR,
"%s=%s already blocked by global_id=%s !", ARG_UHNA, netAsStr(&hna),
globalIdAsString(&un->on->global_id));
return FAILURE;
}
}
if (cmd == OPT_APPLY) {
configure_hna((patch->diff == DEL ? DEL : ADD), &hna, self, 0);
my_description_changed = YES;
}
} else if ( cmd == OPT_UNREGISTER ) {
struct hna_node * un;
while ((un = avl_first_item(&global_uhna_tree)))
configure_hna(DEL, &un->key, self, un->flags);
}
return SUCCESS;
}
STATIC_FUNC
uint16_t set_tun_out_mtu(char *name, uint16_t orig_mtu, uint16_t def_mtu, uint16_t new_mtu) {
if(new_mtu == def_mtu) {
kernel_set_mtu(name, orig_mtu);
return def_mtu;
} else {
kernel_set_mtu(name, new_mtu);
return new_mtu;
}
}
#define MTU_MAX 1500
struct tun_packet
{
union
{
struct iphdr ip4hdr;
struct ip6_hdr ip6hdr;
uint8_t data[MTU_MAX+1000];
} t;
} __attribute__((packed));
STATIC_FUNC
void tun_out_state_set(struct tun_out_node *ton, IDM_T tdn_state)
{
TRACE_FUNCTION_CALL;
assertion(-500204, (ton));
assertion(-501452, (tdn_state==TDN_STATE_CATCHALL || tdn_state==TDN_STATE_DEDICATED));
assertion(-501453, (IMPLIES(tdn_state==TDN_STATE_CATCHALL, ton->tdnDedicated[0] || ton->tdnDedicated[1])));
assertion(-501453, (IMPLIES(tdn_state==TDN_STATE_DEDICATED, ton->tdnCatchAll[0] || ton->tdnCatchAll[1])));
struct avl_node *used_tnn_it = NULL;
struct tun_net_node *used_tnn;
while ((used_tnn = avl_iterate_item(&ton->tun_net_tree, &used_tnn_it))) {
// if (af == used_tnn->tunNetKey.netKey.af) {
struct avl_node *used_tbn_it = NULL;
struct tun_bit_node *used_tbn;
while ( (used_tbn = avl_iterate_item(&used_tnn->tun_bit_tree, &used_tbn_it))) {
if (used_tbn->active_tdn)
configure_tun_bit(ADD, used_tbn, tdn_state);
}
// }
}
}
STATIC_FUNC
void tun_out_state_catchAll(void *tonp)
{
assertion(-500000, (tun_dedicated_to>0));
struct tun_out_node *ton = tonp;
struct tun_dev_node *tdn = ton->tdnDedicated[0] ? ton->tdnDedicated[0] : ton->tdnDedicated[1];
assertion(-500000, (tdn));
assertion(-500000, IMPLIES(ton->tdnDedicated[0], ton->tdnDedicated[0] == tdn));
assertion(-500000, IMPLIES(ton->tdnDedicated[1], ton->tdnDedicated[1] == tdn));
IDM_T stats_captured = tdn->stats_captured;
unsigned long long tx_packets = tdn->stats.tx_packets;
tdn->stats_captured = kernel_get_ifstats(&tdn->stats, tdn->nameKey.str);
if (stats_captured == SUCCESS && tdn->stats_captured == SUCCESS && tx_packets != tdn->stats.tx_packets) {
task_register(tun_dedicated_to, tun_out_state_catchAll, ton, -300000);
return;
}
tun_out_state_set(tonp, TDN_STATE_CATCHALL);
}
STATIC_FUNC
void tun_out_catchAll_hook(int fd)
{
// pick catched packet, open dedicated tunnel, reroute all related active tun_bit_nodes via dedicated tunnel, and retransmit catched packet
TRACE_FUNCTION_CALL;
dbgf_track(DBGT_INFO, "fd=%d",fd);
static struct tun_packet tp;
int32_t tp_len;
assertion(-501456, (fcntl( fd, F_GETFL, 0 ) & O_NONBLOCK));
while ( ( tp_len = read( fd, &tp, sizeof(tp) ) ) > 0 ) {
uint8_t isv4 = (tp.t.ip4hdr.version==4);
int32_t plen = -1;
if ( tp_len > MTU_MAX ||
(tp.t.ip4hdr.version!=4 && tp.t.ip4hdr.version!=6) ||
( isv4 && tp_len <= (int)sizeof(struct iphdr) ) ||
(!isv4 && tp_len <= (int)sizeof(struct ip6_hdr)) /*||
(tp_len != (plen=ntohs( isv4 ? tp.t.ip4hdr.tot_len : tp.t.ip6hdr.ip6_ctlun.ip6_un1.ip6_un1_plen)))*/ ) {
dbgf_sys(DBGT_ERR, "Rcvd invalid packet len=%d=%d ipVersion=%d !",
tp_len, plen, tp.t.ip4hdr.version);
} else {
uint8_t af = isv4 ? AF_INET : AF_INET6;
IPX_T dst4;
IPX_T *dst;
if (isv4) {
dst4 = ip4ToX(tp.t.ip4hdr.daddr);
dst = &dst4;
} else {
dst = &tp.t.ip6hdr.ip6_dst;
}
dbgf_track(DBGT_INFO, "Rcvd len=%d bytes ipVersion=%d len=%d src=%s dst=%s",
tp_len, tp.t.ip4hdr.version,
ntohs( isv4 ? tp.t.ip4hdr.tot_len : tp.t.ip6hdr.ip6_ctlun.ip6_un1.ip6_un1_plen),
isv4 ? ip4AsStr(tp.t.ip4hdr.saddr) : ip6AsStr(&tp.t.ip6hdr.ip6_src), ipXAsStr(af, dst));
struct tun_out_node *ton = NULL;
struct avl_node *an=NULL;
struct tun_bit_node *tbn;
while( (tbn=avl_iterate_item(&tun_bit_tree, &an)) ) {
if (tbn->active_tdn && tbn->tunBitKey.invRouteKey.af == af) {
uint8_t mask = 128 - tbn->tunBitKey.invRouteKey.mask;
if (is_ip_net_equal(dst, &tbn->tunBitKey.invRouteKey.ip, mask, af)) {
ton = tbn->tunBitKey.keyNodes.tnn->tunNetKey.ton;
if (tbn->active_tdn->tunCatch_fd) {
tun_out_state_set(ton, TDN_STATE_DEDICATED);
} else {
dbgf_track(DBGT_WARN,"tunnel dev=%s to orig=%s already dedicated!",
tbn->active_tdn->nameKey.str, globalIdAsString(&ton->tunOutKey.on->global_id));
}
break;
}
}
}
if (ton) {
//if (af==AF_INET || af==AF_INET6) {
// This should only work with IPv6 and non-local IPv6 src addresses. But it works always!!!!
assertion( -501530, (tbn->active_tdn && tbn->active_tdn==ton->tdnDedicated[isv4] && !ton->tdnDedicated[isv4]->tunCatch_fd));
struct tun_catch_key key = {.afKey=af};
struct tun_dev_node *tdn = avl_next_item(&tun_catch_tree, &key);
if (tdn && tdn->tunCatchKey.afKey == af) {
if (tun_out_delay)
wait_sec_usec(0, tun_out_delay); //delay reschedule to complete proper tunnel-setup (e.g. local address, mtu, ...)
int written = write( tdn->tunCatch_fd, &tp, tp_len );
dbgf(written==tp_len? DBGL_CHANGES: DBGL_SYS, written==tp_len? DBGT_INFO : DBGT_ERR,
"%ssendto dst=%s len=%d (wrote=%d) fd=%d dev=%s (via dev=%s)! %s",
( written != tp_len ) ? "Failed " : "",
isv4 ? ip4AsStr(tp.t.ip4hdr.daddr) : ip6AsStr(&tp.t.ip6hdr.ip6_dst),
tp_len, written, tdn->tunCatch_fd, tdn->nameKey.str,
ton->tdnDedicated[isv4]->nameKey.str,
( written != tp_len ) ? strerror(errno) : "");
} else {
dbgf_sys(DBGT_ERR, "No catchAll available for dst=%s len=%d",
isv4 ? ip4AsStr(tp.t.ip4hdr.daddr) : ip6AsStr(&tp.t.ip6hdr.ip6_dst), tp_len);
}
/*
} else {
// This should always work with own addresses. But it does not with IPv4 !!!
//http://www.pdbuchan.com/rawsock/rawsock.html
// Request raw socket descriptor:
int sockfd = socket(af, SOCK_RAW, IPPROTO_RAW);
if ( sockfd <= 0 ) {
dbgf_sys(DBGT_ERR, "Failed creating RAW socket=%d %s", sockfd, strerror(errno));
}
if (af == AF_INET) {
// For whatever reason this does not work for IPv4 !!!
// Set flag that socket expects provided IPv4 header:
const int optval = 1;
int sockopt = setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &optval, sizeof(optval));
if ( sockopt != 0 ) {
dbgf_sys(DBGT_ERR, "Failed sockopt=%d %s", sockopt, strerror(errno));
}
// Bind socket to interface index:
struct ifreq ifr;
memset (&ifr, 0, sizeof (ifr));
snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), "%s", ton->tdnUP[isv4]->name.str);
ifr.ifr_ifindex = ton->tdnUP[isv4]->ifIdx;
if (setsockopt (sockfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof (ifr)) < 0) {
dbgf_sys(DBGT_ERR, "Failed bind to interface %s", strerror(errno));
}
}
struct sockaddr_storage sast = set_sockaddr_storage(af, dst ,0);
int written = sendto( sockfd, &tp, tp_len, 0, (struct sockaddr *) &sast, sizeof(sast) );
dbgf(written==tp_len? DBGL_CHANGES: DBGL_SYS, written==tp_len? DBGT_INFO : DBGT_ERR,
"%ssendto dst=%s len=%d (wrote=%d) fd=%d dev=%s idx=%d! %s",
( written != tp_len ) ? "Failed " : "",
isv4 ? ip4AsStr(tp.t.ip4hdr.daddr) : ip6AsStr(&tp.t.ip6hdr.ip6_dst),
tp_len, written, sockfd, ton->tdnUP[isv4]->name.str, ton->tdnUP[isv4]->ifIdx,
( written != tp_len ) ? strerror(errno) : "");
if ( sockfd > 0 )
close(sockfd);
}
*/
} else {
dbgf_track(DBGT_WARN, "NO tunnel found for dst=%s len=%d ! Discarding packet!",
isv4 ? ip4AsStr(tp.t.ip4hdr.daddr) : ip6AsStr(&tp.t.ip6hdr.ip6_dst), tp_len );
}
}
}
}
STATIC_FUNC
struct tun_dev_node * tun_dev_out_del(struct tun_bit_node *tbn)
{
TRACE_FUNCTION_CALL;
struct tun_search_node *tsn = tbn->tunBitKey.keyNodes.tsn;
struct tun_net_node *tnn = tbn->tunBitKey.keyNodes.tnn;
struct tun_out_node *ton = tnn->tunNetKey.ton;
uint8_t af = tsn->net.af;
uint8_t isv4 = (af==AF_INET);
struct tun_dev_node *tdn = tbn->active_tdn;
dbgf_track(DBGT_INFO, "tunnel dev=%s", tdn->nameKey.str);
assertion(-501460, (is_ip_set(&ton->localIp)));
assertion(-501461, (ton->tunOutKey.on));
assertion(-501462, (ton->tunOutKey.on != self));
assertion(-501463, (tdn));
assertion(-501464, (tdn->ifIdx));
assertion(-501465, (tdn->orig_mtu));
assertion(-501466, (tdn->nameKey.str[0]));
assertion(-501469, avl_find_item(&tdn->tun_bit_tree[isv4], &tbn->tunBitKey.keyNodes));
if ( tdn->tunCatch_fd ) {
assertion(-501467, (tdn->tunCatchKey.afKey == af));
assertion(-501468, (tdn->tunCatch_fd > 0));
avl_remove(&tdn->tun_bit_tree[isv4], &tbn->tunBitKey.keyNodes, -300526);