-
Notifications
You must be signed in to change notification settings - Fork 2
/
ip.c
4238 lines (2972 loc) · 141 KB
/
ip.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
*
* Contributors:
* Simó Albert i Beltran
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/if_tun.h> /* TUNSETPERSIST, ... */
#include <linux/if.h> /* ifr_if, ifr_tun */
#include <sys/types.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include "bmx.h"
#include "ip.h"
#include "msg.h"
#include "schedule.h"
#include "plugin.h"
#include "tools.h"
#include "metrics.h"
#include "hna.h"
#define CODE_CATEGORY_NAME "ip"
uint8_t __af_cfg = DEF_IP_FAMILY;
struct net_key __ZERO_NETCFG_KEY = {.af = DEF_IP_FAMILY};
const struct net_key ZERO_NET_KEY = ZERO_NET_KEY_INIT;
const struct net_key ZERO_NET4_KEY = ZERO_NET4_KEY_INIT;
const struct net_key ZERO_NET6_KEY = ZERO_NET6_KEY_INIT;
const IFNAME_T ZERO_IFNAME = {{0}};
static const char *_af_cfg_read = NULL;
//TODO: remove me!!??
int dev_lo_idx = 0;
int32_t ip_prio_hna_cfg = DEF_IP_RULE_HNA;
static int32_t ip_prio_tun_cfg = DEF_IP_RULE_TUN;
int32_t ip_table_hna_cfg = DEF_IP_TABLE_HNA;
int32_t ip_table_tun_cfg = DEF_IP_TABLE_TUN;
int32_t ip_prio_rules_cfg = DEF_IP_PRIO_RULES;
int32_t ip_throw_rules_cfg = DEF_IP_THROW_RULES;
int32_t ip_policy_rt_cfg = DEF_IP_POLICY_ROUTING;
int32_t policy_routing = POLICY_RT_UNSET;
static int32_t base_port = DEF_BASE_PORT;
#ifdef WITH_UNUSED
static int32_t Lo_rule = DEF_LO_RULE;
#endif
const IPX_T ZERO_IP = { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
const MAC_T ZERO_MAC = {{0}};
const ADDR_T ZERO_ADDR = {{0}};
//TODO: make this configurable
static struct net_key llocal_prefix_cfg;
static struct net_key global_prefix_cfg;
struct net_key autoconf_prefix_cfg;
struct tun_in_node default_tun_in;
//#define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
//#define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
const IP6_T IP6_LOOPBACK_ADDR = { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } };
//const IP6_T IP6_ALLROUTERS_MC_ADDR = {.s6_addr[0] = 0xFF, .s6_addr[1] = 0x02, .s6_addr[15] = 0x02};
const IP6_T IP6_ALLROUTERS_MC_ADDR = {{{0xFF,0x02,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0x02}}};
//const IP6_T IP6_LINKLOCAL_UC_PREF = {.s6_addr[0] = 0xFE, .s6_addr[1] = 0x80};
const IP6_T IP6_LINKLOCAL_UC_PREF = {{{0xFE,0x80,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}}};
const uint8_t IP6_LINKLOCAL_UC_PLEN = 10;
//const IP6_T IP6_MC_PREF = {.s6_addr[0] = 0xFF};
const IP6_T IP6_MC_PREF = {{{0xFF,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}}};
const uint8_t IP6_MC_PLEN = 8;
// static int nlsock_default = -1;
static int io_sock = 0;
struct rtnl_handle ip_rth = { .fd = -1 };
struct rtnl_handle ip_rth2 = { .fd = -1 };
static IDM_T opt_dev_changed = YES;
struct dev_node *primary_dev = NULL;
struct dev_node *primary_phy = NULL;
//IDM_T niit_enabled = NO;
AVL_TREE(if_link_tree, struct if_link_node, index);
AVL_TREE(dev_ip_tree, struct dev_node, llip_key);
AVL_TREE(dev_name_tree, struct dev_node, ifname_device);
AVL_TREE(tun_name_tree, struct ifname, str);
AVL_TREE(iptrack_tree, struct track_node, k);
static LIST_SIMPEL( throw4_list, struct throw_node, list, list );
static int ifevent_sk = -1;
//static Sha ip_sha;
static void dev_check(void *kernel_ip_config_changed);
static void (*ipexport) (int8_t del, const struct net_key *dst, uint32_t oif_idx, IPX_T *via, uint32_t metric, uint8_t distance) = NULL;
struct sys_route_dict bmx6_rt_dict[BMX6_ROUTE_MAX_SUPP+1];
STATIC_FUNC
int rtnl_open(struct rtnl_handle *rth)
{
unsigned subscriptions = 0;
int protocol = NETLINK_ROUTE;
socklen_t addr_len;
// int sndbuf = 32768;
// int rcvbuf = 1024 * 1024;
memset(rth, 0, sizeof(*rth));
rth->fd = socket(AF_NETLINK, SOCK_RAW, protocol);
if (rth->fd < 0) {
dbgf_sys(DBGT_ERR, "Cannot open netlink socket");
return FAILURE;
}
/*
if ( fcntl( rth->fd, F_SETFL, O_NONBLOCK) < 0 ) {
dbgf_sys(DBGT_ERR, "can't set netlink socket nonblocking : (%s)", strerror(errno));
close(rth->fd);
return FAILURE;
}
if (setsockopt(rth->fd,SOL_SOCKET,SO_SNDBUF,&sndbuf,sizeof(sndbuf)) < 0) {
dbgf_sys(DBGT_ERR, "SO_SNDBUF");
return FAILURE;
}
if (setsockopt(rth->fd,SOL_SOCKET,SO_RCVBUF,&rcvbuf,sizeof(rcvbuf)) < 0) {
dbgf_sys(DBGT_ERR, "SO_RCVBUF");
return FAILURE;
}
*/
memset(&rth->local, 0, sizeof(rth->local));
rth->local.nl_family = AF_NETLINK;
rth->local.nl_groups = subscriptions;
if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) {
dbgf_sys(DBGT_ERR, "Cannot bind netlink socket");
return FAILURE;
}
addr_len = sizeof(rth->local);
if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) {
dbgf_sys(DBGT_ERR, "Cannot getsockname");
return FAILURE;
}
if (addr_len != sizeof(rth->local)) {
dbgf_sys(DBGT_ERR, "Wrong address length %d\n", addr_len);
return FAILURE;
}
if (rth->local.nl_family != AF_NETLINK) {
dbgf_sys(DBGT_ERR, "Wrong address family %d\n", rth->local.nl_family);
return FAILURE;
}
rth->seq = rand_num(UINT32_MAX);
return SUCCESS;
}
STATIC_FUNC
void parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
{
memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
while (RTA_OK(rta, len)) {
if (rta->rta_type <= max)
tb[rta->rta_type] = rta;
rta = RTA_NEXT(rta,len);
}
if (len) {
dbgf_sys(DBGT_ERR, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
}
}
STATIC_FUNC
IDM_T get_if_req(IFNAME_T *dev_name, struct ifreq *if_req, int siocgi_req)
{
memset( if_req, 0, sizeof (struct ifreq) );
if (dev_name)
strncpy(if_req->ifr_name, dev_name->str, IFNAMSIZ - 1);
errno = 0;
if ( ioctl( io_sock, siocgi_req, if_req ) < 0 ) {
if (siocgi_req != SIOCGIWNAME) {
dbgf_sys(DBGT_ERR, "can't get SIOCGI %d of interface %s: %s",
siocgi_req, dev_name->str, strerror(errno));
}
return FAILURE;
}
return SUCCESS;
}
extern unsigned int if_nametoindex (const char *);
uint32_t get_if_index(IFNAME_T *name) {
return if_nametoindex(name->str);
}
STATIC_FUNC
void add_rtattr(struct nlmsghdr *nlh, int rta_type, char *data, uint16_t data_len, uint16_t family)
{
TRACE_FUNCTION_CALL;
IP4_T ip4;
if (family == AF_INET) {
ip4 = ipXto4((*((IPX_T*)data)));
data_len = sizeof (ip4);
data = (char*) & ip4;
}
struct rtattr *rta = (struct rtattr *)(((char *) nlh) + NLMSG_ALIGN(nlh->nlmsg_len));
int len = RTA_LENGTH(data_len);
nlh->nlmsg_len = NLMSG_ALIGN(nlh->nlmsg_len) + RTA_ALIGN(len);
assertion(-50173, (NLMSG_ALIGN(nlh->nlmsg_len) < RT_REQ_BUFFSIZE));
// if this fails then double req buff size !!
rta->rta_type = rta_type;
rta->rta_len = len;
memcpy( RTA_DATA(rta), data, data_len );
}
STATIC_FUNC
uint32_t prio_macro_to_prio(int32_t prio_macro)
{
assertion(-501100, (IMPLIES(prio_macro<0, (prio_macro >= RT_PRIO_MIN && prio_macro <= RT_PRIO_MAX))));
if (policy_routing == POLICY_RT_DISABLED)
return 0;
else if (prio_macro>=0)
return prio_macro;
else if (prio_macro == RT_PRIO_HNA)
return ip_prio_hna_cfg;
else if (prio_macro == RT_PRIO_TUNS)
return ip_prio_tun_cfg;
return 0;
}
STATIC_FUNC
uint32_t table_macro_to_table(int32_t table_macro)
{
assertion(-501101, (IMPLIES(table_macro<0, (table_macro >= BMX_TABLE_MIN && table_macro <= BMX_TABLE_MAX))));
if (policy_routing == POLICY_RT_DISABLED)
return 0;
else if (table_macro>=0)
return table_macro;
else if ( table_macro == BMX_TABLE_HNA )
return ip_table_hna_cfg;
else if ( table_macro == BMX_TABLE_TUN )
return ip_table_tun_cfg;
return 0;
}
STATIC_FUNC
char *del2str(IDM_T del)
{
return ( del ? "DEL" : "ADD");
}
#ifdef WITH_UNUSED
STATIC_FUNC
char *rtn2str(uint8_t rtn)
{
if ( rtn == RTN_UNICAST )
return "RTN_UNICAST";
else if ( rtn == RTN_THROW )
return "RTN_THROW ";
return "RTN_ILLEGAL";
}
STATIC_FUNC
char *rta2str(uint8_t rta)
{
if ( rta == RTA_DST )
return "RTA_DST";
return "RTA_ILLEGAL";
}
#endif
STATIC_FUNC
char *trackt2str(uint8_t cmd)
{
if ( cmd == IP_NOP )
return "TRACK_NOP";
else if (cmd == IP_LINK_DEL)
return "LINK_DEL";
else if (cmd == IP_LINK_GET)
return "LINK_GET";
else if (cmd == IP_ADDR_GET)
return "ADDR_GET";
else if (cmd == IP_ROUTE_GET)
return "ROUTE_GET";
else if ( cmd == IP_ADDRESS_SET )
return "ADDRESS_SET";
else if ( cmd == IP_RULE_FLUSH )
return "RULE_FLUSH";
else if ( cmd == IP_RULE_DEFAULT )
return "RULE_DEFAULT";
else if ( cmd == IP_RULE_TEST )
return "RULE_TEST";
else if (cmd == IP_ROUTE_FLUSH)
return "ROUTE_FLUSH";
else if ( cmd == IP_THROW_MY_HNA )
return "THROW_MY_HNA";
else if ( cmd == IP_THROW_MY_NET )
return "THROW_MY_NET";
else if ( cmd == IP_THROW_MY_TUNS )
return "THROW_MY_TUNS";
else if ( cmd == IP_ROUTE_HOST )
return "ROUTE_HOST";
else if ( cmd == IP_ROUTE_HNA )
return "ROUTE_HNA";
else if(cmd == IP_ROUTE_TUNS )
return "ROUTE_TUNS";
else if ( cmd > IP_ROUTE_TUNS && cmd < IP_ROUTE_MAX ) {
return bmx6_rt_dict[ (cmd - IP_ROUTE_TUNS) ].sys2Name ? bmx6_rt_dict[ (cmd - IP_ROUTE_TUNS) ].sys2Name : "unknown";
}
return "TRACK_ILLEGAL";
}
char *family2Str(uint8_t family)
{
static char b[B64_SIZE];
switch (family) {
case AF_INET:
return "IPv4";
case AF_INET6:
return "IPv6";
default:
sprintf( b, "%d ???", family);
return b;
}
}
void ipXToStr(int family, const IPX_T *addr, char *str)
{
assertion(-500583, (str));
uint32_t *a;
if (!addr && (family == AF_INET6 || family == AF_INET)) {
strcpy(str, "---");
return;
} else if (family == AF_INET) {
a = (uint32_t *)&(addr->s6_addr32[3]);
} else if (family == AF_INET6) {
a = (uint32_t *)&(addr->s6_addr32[0]);
} else {
strcpy(str, "ERROR");
return;
}
inet_ntop(family, a, str, family == AF_INET ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN);
return;
}
void ipFToStr(const IPX_T *addr, char *str)
{
ipXToStr(AF_CFG, addr, str);
}
IPX_T ip4ToX(IP4_T ip4)
{
IPX_T ip = ZERO_IP;
ip.s6_addr32[3] = ip4;
return ip;
}
char *ipXAsStr(int family, const IPX_T *addr)
{
static uint8_t c=0;
static char str[IP2S_ARRAY_LEN][INET6_ADDRSTRLEN];
c = (c+1) % IP2S_ARRAY_LEN;
ipXToStr(family, addr, str[c]);
return str[c];
}
char *ipFAsStr(const IPX_T *addr)
{
return ipXAsStr(AF_CFG, addr);
}
char *ip4AsStr( IP4_T addr )
{
static uint8_t c=0;
static char str[IP2S_ARRAY_LEN][INET_ADDRSTRLEN];
c = (c+1) % IP2S_ARRAY_LEN;
inet_ntop( AF_INET, &addr, str[c], INET_ADDRSTRLEN );
return str[c];
}
char *netAsStr(const struct net_key *net)
{
static uint8_t c=0;
static char str[IP2S_ARRAY_LEN][IPXNET_STR_LEN];
c = (c+1) % IP2S_ARRAY_LEN;
if(net) {
ipXToStr(net->af, &net->ip, str[c]);
sprintf(&((str[c]) [ strlen(str[c])]), "/%d", net->mask);
} else {
sprintf(str[c], "---");
}
return str[c];
}
struct net_key * setNet(struct net_key *netp, uint8_t family, uint8_t prefixlen, IPX_T *ip)
{
static struct net_key net;
netp = netp ? netp : &net;
*netp = ZERO_NET_KEY;
netp->af = family;
netp->mask = prefixlen;
netp->ip = ip ? *ip : ZERO_IP;
return netp;
}
char* macAsStr(const MAC_T* mac)
{
return memAsHexString( mac, sizeof(MAC_T));
}
IDM_T is_mac_equal(const MAC_T *a, const MAC_T *b)
{
return (a->u16[2] == b->u16[2] &&
a->u16[1] == b->u16[1] &&
a->u16[0] == b->u16[0]);
}
IDM_T is_ip_equal(const IPX_T *a, const IPX_T *b)
{
return (a->s6_addr32[3] == b->s6_addr32[3] &&
a->s6_addr32[2] == b->s6_addr32[2] &&
a->s6_addr32[1] == b->s6_addr32[1] &&
a->s6_addr32[0] == b->s6_addr32[0]);
}
IDM_T is_ip_net_equal(const IPX_T *netA, const IPX_T *netB, const uint8_t plen, const uint8_t family)
{
IPX_T aprefix = *netA;
IPX_T bprefix = *netB;
ip_netmask_validate(&aprefix, plen, family, YES /*force*/);
ip_netmask_validate(&bprefix, plen, family, YES /*force*/);
return is_ip_equal(&aprefix, &bprefix);
}
IDM_T is_ip_set(const IPX_T *ip)
{
return (ip && !is_ip_equal(ip, &ZERO_IP));
}
IDM_T is_ip_valid( const IPX_T *ip, const uint8_t family )
{
TRACE_FUNCTION_CALL;
if (!is_ip_set(ip))
return NO;
if (family != (is_zero((void*) ip, sizeof ( IPX_T) - sizeof (IP4_T)) ? AF_INET : AF_INET6))
return NO;
if (family == AF_INET6 ) {
if (!is_ip_equal(ip, &IP6_LOOPBACK_ADDR))
return YES;
} else if (family == AF_INET ) {
if (ipXto4(*ip) != INADDR_LOOPBACK && ipXto4(*ip) != INADDR_NONE)
return YES;
}
return NO;
}
IDM_T is_ip_local(IPX_T *ip)
{
struct if_link_node *iln;
struct avl_node *lan = NULL;
while ((iln = avl_iterate_item(&if_link_tree, &lan))) {
if (iln->flags & IFF_UP)
continue;
struct if_addr_node *ian;
struct avl_node *aan = NULL;
while ((ian = avl_iterate_item(&iln->if_addr_tree, &aan))) {
if (is_ip_equal(&ian->ip_addr, ip))
return YES;
}
}
return NO;
}
IDM_T ip_netmask_validate(IPX_T *ipX, uint8_t mask, uint8_t family, uint8_t force)
{
TRACE_FUNCTION_CALL;
uint8_t nmask = mask;
int i;
IP4_T ip32 = 0, m32 = 0;
if (nmask > (family == AF_INET ? 32 : 128))
goto validate_netmask_error;
if (family == AF_INET)
nmask += (IP6_MAX_PREFIXLEN - IP4_MAX_PREFIXLEN);
for (i = 3; i >= 0 && i >= (nmask / 32); i--) {
if (!(ip32 = ipX->s6_addr32[i]))
continue;
if ( force ) {
if (nmask <= (i * 32))
ipX->s6_addr32[i] = 0;
else
ipX->s6_addr32[i] = (ip32 & (m32 = htonl(0xFFFFFFFF << (32 - (nmask - (i * 32))))));
} else {
if (nmask <= (i * 32))
goto validate_netmask_error;
else if (ip32 != (ip32 & (m32 = htonl(0xFFFFFFFF << (32 - (nmask - (i * 32)))))))
goto validate_netmask_error;
}
}
return SUCCESS;
validate_netmask_error:
dbgf_sys(DBGT_ERR, "inconsistent network prefix %s/%d (force=%d nmask=%d, ip32=%s m32=%s)",
ipXAsStr(family, ipX), mask, force, nmask, ip4AsStr(ip32), ip4AsStr(m32));
return FAILURE;
}
STATIC_FUNC
struct dev_node * dev_get_by_name(char *name)
{
IFNAME_T key = ZERO_IFNAME;
strcpy(key.str, name);
return avl_find_item(&dev_name_tree, &key);
}
int rtnl_rcv( int fd, uint32_t pid, uint32_t seq, uint8_t cmd, uint8_t quiet, void (*func) (struct nlmsghdr *nh, void *data) ,void *data)
{
int max_retries = 10;
uint8_t more_data;
int iteration = 0;
//TODO: see ip/libnetlink.c rtnl_talk() for HOWTO
do {
char buf[RTNL_RCV_MAX];
memset(buf, 0, sizeof(buf));
iteration++;
struct iovec iov = {.iov_base = buf, .iov_len = sizeof (buf)};
struct sockaddr_nl nla = {.nl_family = AF_NETLINK }; //{.nl_family = AF_NETLINK}; //TODO: not sure here, maybe only for cmd==IP_ADDR_GET and IP_LINK_GET
struct msghdr msg = {.msg_name = (void *)&nla, .msg_namelen = sizeof(nla), .msg_iov = &iov, .msg_iovlen = 1};
struct nlmsghdr *nh;
more_data = NO;
errno=0;
int status = recvmsg( fd, &msg, 0 );
int err = errno;
dbgf((err || status <= 0) ? DBGL_SYS : DBGL_CHANGES, (err || status <= 0) ? DBGT_WARN : DBGT_INFO,
"rcvd cmd=%s fd=%d iteration=%d retries=%d status=%d err=%d %s",
trackt2str(cmd), fd, iteration, max_retries, status, err, strerror(err));
if ( status < 0 ) {
if ( (err == EINTR || err == EWOULDBLOCK || err == EAGAIN ) && max_retries-- > 0 ) {
usleep(500);
upd_time( NULL );
more_data = YES;
continue;
} else {
dbgf_sys(DBGT_ERR, "giving up!");
return -501096;
// EXITERROR(-501096, (0));
// return FAILURE;
}
} else if (status == 0) {
dbgf_sys(DBGT_ERR, "netlink EOF");
return -501097;
// EXITERROR(-501097, (0));
// return FAILURE;
}
if (msg.msg_flags & MSG_TRUNC) {
dbgf_track(DBGT_INFO, "MSG_TRUNC");
more_data = YES;
}
for ( nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, (size_t)status); nh = NLMSG_NEXT(nh, status) ) {
if (nla.nl_pid || (pid && nh->nlmsg_pid != pid) || (seq && nh->nlmsg_seq != seq)) {
dbgf_sys(DBGT_ERR, "pid/sqn mismatch: status=%d "
"nl_pid=%d ==0!? nlmsg_pid=%d == local.nl_pid=%d!? "
"nlmsg_seq=%d == ip_rth.dump=%d!?",
status, nla.nl_pid, nh->nlmsg_pid, pid, nh->nlmsg_seq, seq);
assertion(-501495, (0)); //continue;
}
if (nh->nlmsg_flags & NLM_F_MULTI) {
dbgf_all( DBGT_INFO, "NLM_F_MULTI");
more_data = YES;
}
if (nh->nlmsg_type == NLMSG_DONE) {
dbgf_track(DBGT_INFO, "NLMSG_DONE");
more_data = NO;
break;
} else if (nh->nlmsg_type == NLMSG_ERROR && ((struct nlmsgerr*) NLMSG_DATA(nh))->error) {
dbgf(quiet ? DBGL_ALL : DBGL_SYS, quiet ? DBGT_INFO : DBGT_ERR, "%s error=%s",
trackt2str(cmd), strerror(-((struct nlmsgerr*) NLMSG_DATA(nh))->error));
return FAILURE;
}
if (func)
(*func)(nh, data);
}
} while (more_data);
return SUCCESS;
}
STATIC_FUNC
IDM_T rtnl_talk(struct rtnl_handle *iprth, struct nlmsghdr *nlh, uint8_t cmd, uint8_t quiet, void (*func) (struct nlmsghdr *nh, void *data) ,void *data)
{
// DONT USE setNet() here (because return pointer is static)!!!!!!!!!!!!!
assertion(-501494, (!iprth->busy));
iprth->busy = 1;
nlh->nlmsg_pid = My_pid;
nlh->nlmsg_seq = ++(iprth->seq);
errno = 0;
if (send(iprth->fd, nlh, nlh->nlmsg_len, 0) < 0) {
dbgf_sys(DBGT_ERR, "can't send netlink message to kernel: %s", strerror(errno));
iprth->busy = 0;
EXITERROR(-501095, (0));
return FAILURE;
}
int result = rtnl_rcv( iprth->fd, iprth->local.nl_pid, iprth->seq, cmd, quiet, func, data );
ASSERTION(result, (result >= FAILURE));
iprth->busy = 0;
return result;
}
STATIC_FUNC
IDM_T kernel_get_if_config_post(IDM_T purge_all, uint16_t curr_sqn)
{
TRACE_FUNCTION_CALL;
uint16_t changed = 0;
struct if_link_node *iln;
int index = 0;
while ((iln = avl_next_item(&if_link_tree, &index))) {
index = iln->index;
IPX_T addr = ZERO_IP;
struct if_addr_node *ian;
IDM_T addr_changed = 0;
while ((ian = avl_next_item(&iln->if_addr_tree, &addr))) {
addr = ian->ip_addr;
if ( purge_all || curr_sqn != ian->update_sqn || curr_sqn != iln->update_sqn) {
dbgf_track(DBGT_WARN, "addr index %d %s addr %s REMOVED",
iln->index, ian->label.str, ipXAsStr(ian->ifa.ifa_family, &ian->ip_addr));
if (ian->dev) {
ian->dev->hard_conf_changed = YES;
ian->dev->if_llocal_addr = NULL;
ian->dev->if_global_addr = NULL;
}
avl_remove(&iln->if_addr_tree, &addr, -300236);
debugFree(ian, -300237);
changed++;
continue;
} else {
changed += ian->changed;
addr_changed = YES;
}
}
struct dev_node *dev = dev_get_by_name(iln->name.str);
if (purge_all || curr_sqn != iln->update_sqn) {
assertion(-500565, (!iln->if_addr_tree.items));
dbgf_track(DBGT_WARN, "link index %d %s addr %s REMOVED",
iln->index, iln->name.str, memAsHexString(&iln->addr, iln->alen));
avl_remove(&if_link_tree, &iln->index, -300232);
avl_remove(&if_link_tree, &iln->name, -300521);
debugFree(iln, -300230);
changed++;
} else if (iln->changed || (addr_changed && dev && !dev->if_llocal_addr && !dev->if_global_addr)) {
if (dev)
dev->hard_conf_changed = YES;
changed += iln->changed;
dbgf_track(DBGT_WARN, "link=%s dev=%s configuration CHANGED",
iln->name.str, dev ? dev->ifname_label.str : "ERROR");
}
}
if (changed) {
dbgf_track(DBGT_WARN, "network configuration CHANGED");
return YES;
} else {
dbgf_all(DBGT_INFO, "network configuration UNCHANGED");
return NO;
}
}
STATIC_FUNC
void kernel_get_if_addr_config(struct nlmsghdr *nh, void *index_sqnp)
{
TRACE_FUNCTION_CALL;
uint16_t index_sqn = *((uint16_t*) index_sqnp);
struct ifaddrmsg *if_addr = NLMSG_DATA(nh);
int index = if_addr->ifa_index;
int family = if_addr->ifa_family;
struct if_link_node *iln = avl_find_item(&if_link_tree, &index);
if (!iln)
return;
if (family != AF_INET && family != AF_INET6)
return;
if (nh->nlmsg_type != RTM_NEWADDR)
return;
assertion(-501496, (nh->nlmsg_len >= (int) NLMSG_LENGTH(sizeof (*if_addr))));
struct rtattr * rta_tb[IFA_MAX + 1];
parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(if_addr), nh->nlmsg_len - NLMSG_LENGTH(sizeof (*if_addr)));
if (!rta_tb[IFA_LOCAL])
rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
if (!rta_tb[IFA_LOCAL] || !if_addr)
return;
IPX_T ip_addr = ZERO_IP;
uint32_t alen = XMIN(sizeof (ip_addr), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
memcpy(&ip_addr, RTA_DATA(rta_tb[IFA_LOCAL]), alen);
if (family == AF_INET)
ip_addr = ip4ToX(*((IP4_T*) (&ip_addr)));
if (!is_ip_valid(&ip_addr, family)) // specially catch loopback ::1/128
return;
struct if_addr_node *new_ian = NULL;
struct if_addr_node *old_ian = avl_find_item(&iln->if_addr_tree, &ip_addr);
if (old_ian) {
old_ian->changed = 0;
if (old_ian->update_sqn == index_sqn) {
dbgf_sys(DBGT_ERR,
"ifi %d addr %s found several times!",
iln->index, ipXAsStr(old_ian->ifa.ifa_family, &ip_addr));
}
if (nh->nlmsg_len > old_ian->nlmsghdr->nlmsg_len) {
if (old_ian->dev) {
old_ian->dev->hard_conf_changed = YES;
old_ian->dev->if_llocal_addr = NULL;
old_ian->dev->if_global_addr = NULL;
}
avl_remove(&iln->if_addr_tree, &ip_addr, -300239);
dbgf_sys(DBGT_ERR, "new size");
} else if (memcmp(nh, old_ian->nlmsghdr, nh->nlmsg_len)) {
if (nh->nlmsg_len != old_ian->nlmsghdr->nlmsg_len) {
dbgf_sys(DBGT_ERR, "different data and size %d != %d",
nh->nlmsg_len, old_ian->nlmsghdr->nlmsg_len);
}
memcpy(old_ian->nlmsghdr, nh, nh->nlmsg_len);
new_ian = old_ian;
} else {
new_ian = old_ian;
}
}
if (!new_ian) {
new_ian = debugMallocReset(sizeof (struct if_addr_node) +nh->nlmsg_len, -300522);
memcpy(new_ian->nlmsghdr, nh, nh->nlmsg_len);
new_ian->ip_addr = ip_addr;
new_ian->iln = iln;
avl_insert(&iln->if_addr_tree, new_ian, -300520);
}
IFNAME_T label = {{0}};
IPX_T ip_mcast = ZERO_IP;
if (rta_tb[IFA_LABEL])
strcpy(label.str, (char*) RTA_DATA(rta_tb[IFA_LABEL]));
else
label = iln->name;