forked from ptpt52/natflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
natflow_user.c
3876 lines (3499 loc) · 111 KB
/
natflow_user.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
/*
* Author: Chen Minqiang <[email protected]>
* Date : Wed, 27 Jun 2018 22:13:17 +0800
*/
#include <linux/ctype.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/seq_file.h>
#include <linux/device.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/if_arp.h>
#include <linux/init.h>
#include <linux/ip.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/netdevice.h>
#include <linux/inetdevice.h>
#include <linux/skbuff.h>
#include <linux/string.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/version.h>
#include <linux/spinlock.h>
#include <linux/seqlock.h>
#include <linux/netdevice.h>
#include <linux/bitops.h>
#include <linux/timer.h>
#include <linux/vmalloc.h>
#include <linux/highmem.h>
#include <linux/netfilter.h>
#include <linux/netfilter_bridge.h>
#include <net/netfilter/nf_conntrack.h>
#include <net/netfilter/nf_conntrack_core.h>
#include <net/netfilter/nf_conntrack_helper.h>
#include <net/netfilter/nf_conntrack_extend.h>
#include <net/netfilter/nf_conntrack_acct.h>
#include <linux/if_pppox.h>
#include <linux/ppp_defs.h>
#include "natflow.h"
#include "natflow_common.h"
#include "natflow_user.h"
#include "natflow_zone.h"
struct userinfo {
struct list_head list;
unsigned int timeout;
union {
__be32 ip;
union nf_inet_addr ipv6;
};
uint8_t macaddr[ETH_ALEN];
uint8_t l2num:1; /* 0: ipv4, 1: ipv6 */
uint8_t auth_type:7;
uint8_t auth_status;
uint16_t auth_rule_id;
unsigned long long rx_packets;
unsigned long long rx_bytes;
unsigned long long tx_packets;
unsigned long long tx_bytes;
unsigned int rx_speed_packets;
unsigned int rx_speed_bytes;
unsigned int tx_speed_packets;
unsigned int tx_speed_bytes;
};
struct userinfo_event {
spinlock_t lock;
struct list_head head;
wait_queue_head_t wait;
#define USERINFO_EVENT_STOPPED 0
#define USERINFO_EVENT_RUNNING 1
unsigned int stage;
};
static struct userinfo_event userinfo_event_store;
static inline void userinfo_event_store_init(void)
{
spin_lock_init(&userinfo_event_store.lock);
INIT_LIST_HEAD(&userinfo_event_store.head);
init_waitqueue_head(&userinfo_event_store.wait);
userinfo_event_store.stage = USERINFO_EVENT_STOPPED;
}
static inline void userinfo_event_store_exit(void)
{
struct userinfo *user_i, *n;
list_for_each_entry_safe(user_i, n, &userinfo_event_store.head, list) {
list_del(&user_i->list);
kfree(user_i);
}
}
static int natflow_user_major = 0;
static int natflow_user_minor = 0;
static struct cdev natflow_user_cdev;
const char *natflow_user_dev_name = "natflow_user_ctl";
static struct class *natflow_user_class;
static struct device *natflow_user_dev;
static uint16_t auth_conf_magic = 0;
static inline void auth_conf_update_magic(int init)
{
if (init) {
auth_conf_magic = jiffies;
} else {
auth_conf_magic++;
}
}
static struct auth_conf *auth_conf = NULL;
static inline int auth_rule_add_one(struct auth_rule_t *rule)
{
if (auth_conf->num < MAX_AUTH) {
memcpy(&auth_conf->auth[auth_conf->num], rule, sizeof(struct auth_rule_t));
auth_conf->num++;
return 0;
}
return -ENOMEM;
}
static int disabled = 1;
void natflow_user_disabled_set(int v)
{
disabled = v;
}
int natflow_user_disabled_get(void)
{
return disabled;
}
static int qos_major = 0;
static int qos_minor = 0;
static struct cdev qos_cdev;
const char *qos_dev_name = "qos_ctl";
static struct class *qos_class;
static struct device *qos_dev;
#define QOS_TOKEN_CTRL_GROUP_MAX 64
static struct {
struct token_ctrl rx;
struct token_ctrl tx;
} qos_token_ctrl[QOS_TOKEN_CTRL_GROUP_MAX];
static struct qos_rule {
union {
unsigned char name[16];
__be32 ip;
struct {
__be32 ip;
__be32 mask;
} ipcidr;
} user;
union {
unsigned char name[16];
__be16 port;
} user_port;
union {
unsigned char name[16];
__be32 ip;
struct {
__be32 ip;
__be32 mask;
} ipcidr;
} remote;
union {
unsigned char name[16];
__be16 port;
} remote_port;
unsigned short proto;
#define USER_TYPE_IP BIT(0)
#define USER_TYPE_IPCIDR BIT(1)
#define USER_TYPE_SET BIT(2)
#define USER_TYPE_MASK (USER_TYPE_IP|USER_TYPE_IPCIDR|USER_TYPE_SET)
#define USER_PORT_TYPE_PORT BIT(3)
#define USER_PORT_TYPE_SET BIT(4)
#define USER_PORT_TYPE_MASK (USER_PORT_TYPE_PORT|USER_PORT_TYPE_SET)
#define REMOTE_TYPE_IP BIT(5)
#define REMOTE_TYPE_IPCIDR BIT(6)
#define REMOTE_TYPE_SET BIT(7)
#define REMOTE_TYPE_MASK (REMOTE_TYPE_IP|REMOTE_TYPE_IPCIDR|REMOTE_TYPE_SET)
#define REMOTE_PORT_TYPE_PORT BIT(8)
#define REMOTE_PORT_TYPE_SET BIT(9)
#define REMOTE_PORT_TYPE_MASK (REMOTE_PORT_TYPE_PORT|REMOTE_PORT_TYPE_SET)
unsigned short flag;
unsigned int rxbytes;
unsigned int txbytes;
} qos_token_ctrl_rule[QOS_TOKEN_CTRL_GROUP_MAX];
static unsigned int tc_classid_mode = 0;
static int qos_token_ctrl_num = 0;
static void qos_token_ctrl_init(void)
{
int i;
memset(&qos_token_ctrl, 0, sizeof(*qos_token_ctrl) * QOS_TOKEN_CTRL_GROUP_MAX);
memset(&qos_token_ctrl_rule, 0, sizeof(*qos_token_ctrl_rule) * QOS_TOKEN_CTRL_GROUP_MAX);
for (i = 0; i < QOS_TOKEN_CTRL_GROUP_MAX; i++) {
spin_lock_init(&qos_token_ctrl[i].rx.lock);
spin_lock_init(&qos_token_ctrl[i].tx.lock);
}
}
static int natflow_token_ctrl(struct sk_buff *skb, struct token_ctrl *tc)
{
int feed_jiffies = 0;
unsigned int current_jiffies = (unsigned int)jiffies;
int ret = 0;
int len = skb->len;
struct iphdr *iph = ip_hdr(skb);
void *l4 = (void *)iph + iph->ihl * 4;
switch (iph->protocol) {
case IPPROTO_TCP:
len -= iph->ihl * 4 + TCPH(l4)->doff * 4;
break;
case IPPROTO_UDP:
len -= iph->ihl * 4 + sizeof(struct udphdr);
break;
}
if (len <= 0) return 0;
if (tc->tokens_per_jiffy == 0) return 0;
spin_lock_bh(&tc->lock);
if (tc->tokens > 0) {
tc->tokens -= len;
ret = 0;
goto out;
}
current_jiffies = jiffies;
feed_jiffies = uintmindiff(current_jiffies, tc->jiffies);
if (feed_jiffies > HZ) {
feed_jiffies = HZ/8 + 1;
}
ret = tc->tokens + (int)(tc->tokens_per_jiffy * feed_jiffies);
if (feed_jiffies <= HZ) {
tc->tokens = ret;
} else {
if (ret <= 0) {
tc->tokens = ret;
} else {
tc->tokens = 0;
}
}
if (tc->tokens >= 0) {
tc->tokens -= len;
ret = 0;
}
tc->jiffies = current_jiffies;
out:
spin_unlock_bh(&tc->lock);
return ret;
}
int rx_token_ctrl(struct sk_buff *skb, struct fakeuser_data_t *fud, natflow_t *nf)
{
int ret = 0;
if (tc_classid_mode && nf && nf->qos_id) {
skb->mark = nf->qos_id * 2 - 1;
return 0;
}
if (nf && nf->qos_id && nf->qos_id <= qos_token_ctrl_num) {
ret = natflow_token_ctrl(skb, &qos_token_ctrl[nf->qos_id - 1].rx);
}
if (fud->tc.rx.tokens_per_jiffy == 0 || ret < 0) {
return ret;
}
return natflow_token_ctrl(skb, &fud->tc.rx);
}
int tx_token_ctrl(struct sk_buff *skb, struct fakeuser_data_t *fud, natflow_t *nf)
{
int ret = 0;
if (tc_classid_mode && nf && nf->qos_id) {
skb->mark = nf->qos_id * 2;
return 0;
}
if (nf && nf->qos_id && nf->qos_id <= qos_token_ctrl_num) {
ret = natflow_token_ctrl(skb, &qos_token_ctrl[nf->qos_id - 1].tx);
}
if (fud->tc.tx.tokens_per_jiffy == 0 || ret < 0) {
return ret;
}
return natflow_token_ctrl(skb, &fud->tc.tx);
}
static unsigned int auth_open_weixin_reply = 0;
static unsigned short https_redirect_port = __constant_htons(443);
static unsigned int https_redirect_en = 0;
/*XXX: default redirect_ip 10.10.10.10 */
unsigned int redirect_ip = __constant_htonl((10<<24)|(10<<16)|(10<<8)|(10<<0));
/* user timeout 1800s */
static unsigned int natflow_user_timeout = 1800;
#define NATFLOW_USER_TIMEOUT 300
static DEFINE_PER_CPU(struct sk_buff *, natflow_user_uskbs);
#define NATFLOW_USKB_SIZE (sizeof(struct iphdr) + sizeof(struct ipv6hdr) + sizeof(struct udphdr))
#define NATFLOW_FAKEUSER_DADDR __constant_htonl(0x7fffffff)
static inline struct sk_buff *uskb_of_this_cpu(void)
{
struct sk_buff **ptr = this_cpu_ptr(&natflow_user_uskbs);
if (!*ptr) {
*ptr = __alloc_skb(NATFLOW_USKB_SIZE, GFP_ATOMIC, 0, numa_node_id());
}
return *ptr;
}
void natflow_user_timeout_touch(natflow_fakeuser_t *nfu)
{
struct fakeuser_data_t *fud;
fud = natflow_fakeuser_data(nfu);
if (fud->auth_type != AUTH_TYPE_UNKNOWN) {
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0)
unsigned long newtimeout = jiffies + natflow_user_timeout * HZ;
if (newtimeout - nfu->timeout.expires > HZ) {
mod_timer_pending(&nfu->timeout, newtimeout);
}
#else
nfu->timeout = jiffies + natflow_user_timeout * HZ;
#endif
} else {
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0)
unsigned long newtimeout = jiffies + NATFLOW_USER_TIMEOUT * HZ;
if (newtimeout - nfu->timeout.expires > HZ) {
mod_timer_pending(&nfu->timeout, newtimeout);
}
#else
nfu->timeout = jiffies + NATFLOW_USER_TIMEOUT * HZ;
#endif
}
}
natflow_fakeuser_t *natflow_user_get(struct nf_conn *ct)
{
natflow_fakeuser_t *user = NULL;
if (ct->master) {
if ((IPS_NATFLOW_USER & ct->master->status)) {
if (unlikely(nf_ct_is_dying(ct->master))) {
return NULL;
}
user = ct->master;
} else if (ct->master->master && (IPS_NATFLOW_USER & ct->master->master->status)) {
if (unlikely(nf_ct_is_dying(ct->master->master))) {
return NULL;
}
user = ct->master->master;
}
}
return user;
}
static natflow_fakeuser_t *natflow_user_find_get(__be32 ip)
{
natflow_fakeuser_t *user = NULL;
struct nf_conntrack_tuple tuple;
struct nf_conntrack_tuple_hash *h;
memset(&tuple, 0, sizeof(tuple));
tuple.src.u3.ip = ip;
tuple.src.u.udp.port = __constant_htons(0);
tuple.dst.u3.ip = NATFLOW_FAKEUSER_DADDR;
tuple.dst.u.udp.port = __constant_htons(65535);
tuple.src.l3num = PF_INET;
tuple.dst.protonum = IPPROTO_UDP;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0)
h = nf_conntrack_find_get(&init_net, NF_CT_DEFAULT_ZONE, &tuple);
#else
h = nf_conntrack_find_get(&init_net, &nf_ct_zone_dflt, &tuple);
#endif
if (h) {
struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
if (!(IPS_NATFLOW_USER & ct->status) || NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL) {
nf_ct_put(ct);
} else {
user = ct;
}
}
return user;
}
static natflow_fakeuser_t *natflow_user_find_get6(union nf_inet_addr *u3)
{
natflow_fakeuser_t *user = NULL;
struct nf_conntrack_tuple tuple;
struct nf_conntrack_tuple_hash *h;
memset(&tuple, 0, sizeof(tuple));
tuple.src.u3 = *u3;
tuple.src.u.udp.port = __constant_htons(0);
memset(&tuple.dst.u3, 0, sizeof(tuple.dst.u3));
tuple.dst.u3.in6.s6_addr16[0] = 0xffff; //NATFLOW_FAKEUSER_DADDR=ffff::
tuple.dst.u.udp.port = __constant_htons(65535);
tuple.src.l3num = AF_INET6;
tuple.dst.protonum = IPPROTO_UDP;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0)
h = nf_conntrack_find_get(&init_net, NF_CT_DEFAULT_ZONE, &tuple);
#else
h = nf_conntrack_find_get(&init_net, &nf_ct_zone_dflt, &tuple);
#endif
if (h) {
struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
if (!(IPS_NATFLOW_USER & ct->status) || NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL) {
nf_ct_put(ct);
} else {
user = ct;
}
}
return user;
}
static void natflow_user_release_put(natflow_fakeuser_t *user)
{
nf_ct_put(user);
}
static natflow_fakeuser_t *natflow_user_lookup_in(struct nf_conn *ct, int dir)
{
natflow_fakeuser_t *user = NULL;
user = natflow_user_get(ct);
if (user)
return user;
/* FIXME: cannot use ct->master for user since destroy_gre_conntrack() may double free gre helper in master */
if (unlikely(nf_ct_protonum(ct) == IPPROTO_GRE)) {
return NULL;
}
if (!nf_ct_is_confirmed(ct) && !user && (!ct->master || !ct->master->master)) {
if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num == AF_INET) {
user = natflow_user_find_get(ct->tuplehash[dir].tuple.src.u3.ip);
} else {
user = natflow_user_find_get6(&ct->tuplehash[dir].tuple.src.u3);
}
if (user) {
natflow_user_timeout_touch(user);
if (ct->master && !(IPS_NATFLOW_USER & ct->master->status)) {
if (!test_and_set_bit(IPS_NATFLOW_MASTER_BIT, &ct->master->status)) {
nf_conntrack_get(&user->ct_general);
ct->master->master = user;
}
} else {
if (!test_and_set_bit(IPS_NATFLOW_MASTER_BIT, &ct->status)) {
nf_conntrack_get(&user->ct_general);
ct->master = user;
}
}
natflow_user_release_put(user);
} else {
return NULL;
}
}
return natflow_user_get(ct);
}
static natflow_fakeuser_t *natflow_user_in(struct nf_conn *ct, int dir)
{
natflow_fakeuser_t *user = NULL;
user = natflow_user_get(ct);
if (user)
return user;
/* FIXME: cannot use ct->master for user since destroy_gre_conntrack() may double free gre helper in master */
if (unlikely(nf_ct_protonum(ct) == IPPROTO_GRE)) {
return NULL;
}
if (!nf_ct_is_confirmed(ct) && !user && (!ct->master || !ct->master->master)) {
struct nf_ct_ext *new = NULL;
unsigned int newoff = 0;
int ret;
struct sk_buff *uskb;
struct iphdr *iph;
struct udphdr *udph;
enum ip_conntrack_info ctinfo;
uskb = uskb_of_this_cpu();
if (uskb == NULL) {
return NULL;
}
skb_reset_transport_header(uskb);
skb_reset_network_header(uskb);
skb_reset_mac_len(uskb);
if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num == AF_INET) {
uskb->protocol = __constant_htons(ETH_P_IP);
uskb->len = (sizeof(struct iphdr) + sizeof(struct udphdr));
skb_set_tail_pointer(uskb, uskb->len);
uskb->pkt_type = PACKET_HOST;
uskb->transport_header = uskb->network_header + sizeof(struct iphdr);
iph = ip_hdr(uskb);
iph->version = 4;
iph->ihl = 5;
iph->saddr = ct->tuplehash[dir].tuple.src.u3.ip;
iph->daddr = NATFLOW_FAKEUSER_DADDR;
iph->tos = 0;
iph->tot_len = htons(uskb->len);
iph->ttl=255;
iph->protocol = IPPROTO_UDP;
iph->id = __constant_htons(0xDEAD);
iph->frag_off = 0;
iph->check = 0;
iph->check = ip_fast_csum(iph, iph->ihl);
udph = (struct udphdr *)((char *)iph + sizeof(struct iphdr));
udph->source = __constant_htons(0);
udph->dest = __constant_htons(65535);
udph->len = __constant_htons(sizeof(struct udphdr));
udph->check = 0;
ret = nf_conntrack_in_compat(&init_net, PF_INET, NF_INET_PRE_ROUTING, uskb);
if (ret != NF_ACCEPT) {
return NULL;
}
user = nf_ct_get(uskb, &ctinfo);
if (!user) {
NATFLOW_ERROR("fakeuser create for ct[%pI4:%u->%pI4:%u %pI4:%u<-%pI4:%u] failed, ctinfo=%x\n",
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip, ntohs(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.all),
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.ip, ntohs(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.all),
&ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip, ntohs(ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all),
&ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip, ntohs(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all), (unsigned int)ctinfo);
return NULL;
}
if (!user->ext) {
NATFLOW_ERROR("fakeuser create for ct[%pI4:%u->%pI4:%u %pI4:%u<-%pI4:%u] failed, user->ext is NULL\n",
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip, ntohs(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.all),
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.ip, ntohs(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.all),
&ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip, ntohs(ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all),
&ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip, ntohs(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all));
skb_nfct_reset(uskb);
return NULL;
}
if (!nf_ct_is_confirmed(user) && !(IPS_NATFLOW_USER & user->status) && !test_and_set_bit(IPS_NATFLOW_USER_BIT, &user->status)) {
struct fakeuser_data_t *fud;
newoff = ALIGN(user->ext->len, __ALIGN_64BITS);
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 5, 0)
new = __krealloc(user->ext, newoff + sizeof(struct fakeuser_data_t), GFP_ATOMIC);
#else
new = krealloc(user->ext, newoff + sizeof(struct fakeuser_data_t), GFP_ATOMIC);
#endif
if (!new) {
NATFLOW_ERROR("fakeuser create for ct[%pI4:%u->%pI4:%u %pI4:%u<-%pI4:%u] failed, realloc user->ext failed\n",
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip, ntohs(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.all),
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.ip, ntohs(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.all),
&ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip, ntohs(ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all),
&ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip, ntohs(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all));
skb_nfct_reset(uskb);
return NULL;
}
if (user->ext != new) {
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0)
kfree_rcu(user->ext, rcu);
user->ext = new;
#elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 5, 0)
kfree_rcu(user->ext, rcu);
rcu_assign_pointer(user->ext, new);
#else
user->ext = new;
#endif
}
new->len = newoff;
memset((void *)new + newoff, 0, sizeof(struct fakeuser_data_t));
fud = (struct fakeuser_data_t *)((void *)new + newoff);
spin_lock_init(&fud->tc.rx.lock);
spin_lock_init(&fud->tc.tx.lock);
}
} else {
//FIXME ipv6
uskb->protocol = __constant_htons(ETH_P_IPV6);
uskb->len = (sizeof(struct ipv6hdr) + sizeof(struct udphdr));
skb_set_tail_pointer(uskb, uskb->len);
uskb->pkt_type = PACKET_HOST;
uskb->transport_header = uskb->network_header + sizeof(struct ipv6hdr);
iph = ip_hdr(uskb);
IPV6H->version = 6;
IPV6H->priority = 0;
IPV6H->flow_lbl[2] = IPV6H->flow_lbl[1] = IPV6H->flow_lbl[0] = 0;
IPV6H->payload_len = htons(sizeof(struct udphdr));
IPV6H->nexthdr = IPPROTO_UDP;
IPV6H->hop_limit = 255;
IPV6H->saddr = ct->tuplehash[dir].tuple.src.u3.in6;
memset(&IPV6H->daddr, 0x0, sizeof(IPV6H->daddr));
IPV6H->daddr.s6_addr16[0] = 0xffff; //NATFLOW_FAKEUSER_DADDR=ffff::
udph = (struct udphdr *)((char *)iph + sizeof(struct ipv6hdr));
udph->source = __constant_htons(0);
udph->dest = __constant_htons(65535);
udph->len = __constant_htons(sizeof(struct udphdr));
udph->check = 0;
ret = nf_conntrack_in_compat(&init_net, AF_INET6, NF_INET_PRE_ROUTING, uskb);
if (ret != NF_ACCEPT) {
return NULL;
}
user = nf_ct_get(uskb, &ctinfo);
if (!user) {
NATFLOW_ERROR("fakeuser create for ct[%pI6:%u->%pI6:%u %pI6:%u<-%pI6:%u] failed, ctinfo=%x\n",
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.in6, ntohs(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.all),
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.in6, ntohs(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.all),
&ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.in6, ntohs(ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all),
&ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.in6, ntohs(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all), (unsigned int)ctinfo);
return NULL;
}
if (!user->ext) {
NATFLOW_ERROR("fakeuser create for ct[%pI6:%u->%pI6:%u %pI6:%u<-%pI6:%u] failed, user->ext is NULL\n",
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.in6, ntohs(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.all),
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.in6, ntohs(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.all),
&ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.in6, ntohs(ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all),
&ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.in6, ntohs(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all));
skb_nfct_reset(uskb);
return NULL;
}
if (!nf_ct_is_confirmed(user) && !(IPS_NATFLOW_USER & user->status) && !test_and_set_bit(IPS_NATFLOW_USER_BIT, &user->status)) {
struct fakeuser_data_t *fud;
newoff = ALIGN(user->ext->len, __ALIGN_64BITS);
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 5, 0)
new = __krealloc(user->ext, newoff + sizeof(struct fakeuser_data_t), GFP_ATOMIC);
#else
new = krealloc(user->ext, newoff + sizeof(struct fakeuser_data_t), GFP_ATOMIC);
#endif
if (!new) {
NATFLOW_ERROR("fakeuser create for ct[%pI6:%u->%pI6:%u %pI6:%u<-%pI6:%u] failed, realloc user->ext failed\n",
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.in6, ntohs(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.all),
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.in6, ntohs(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.all),
&ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.in6, ntohs(ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all),
&ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.in6, ntohs(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all));
skb_nfct_reset(uskb);
return NULL;
}
if (user->ext != new) {
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0)
kfree_rcu(user->ext, rcu);
user->ext = new;
#elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 5, 0)
kfree_rcu(user->ext, rcu);
rcu_assign_pointer(user->ext, new);
#else
user->ext = new;
#endif
}
new->len = newoff;
memset((void *)new + newoff, 0, sizeof(struct fakeuser_data_t));
fud = (struct fakeuser_data_t *)((void *)new + newoff);
spin_lock_init(&fud->tc.rx.lock);
spin_lock_init(&fud->tc.tx.lock);
}
}
#ifdef CONFIG_NF_CONNTRACK_EVENTS
do {
struct nf_conntrack_ecache *e = nf_ct_ecache_find(user);
if (e) {
user->ext->offset[NF_CT_EXT_ECACHE] = 0;
}
} while (0);
#endif
ret = nf_conntrack_confirm(uskb);
if (ret != NF_ACCEPT) {
skb_nfct_reset(uskb);
return NULL;
}
/* XXX: in race loser_ct may be replaced in nf_conntrack_confirm,
* here reload ct(user) from uskb, and it can't be NULL
*/
user = nf_ct_get(uskb, &ctinfo);
if (!user || nf_ct_is_dying(user)) {
skb_nfct_reset(uskb);
return NULL;
}
natflow_user_timeout_touch(user);
if (ct->master && !(IPS_NATFLOW_USER & ct->master->status)) {
if (!test_and_set_bit(IPS_NATFLOW_MASTER_BIT, &ct->master->status)) {
nf_conntrack_get(&user->ct_general);
ct->master->master = user;
}
} else {
if (!test_and_set_bit(IPS_NATFLOW_MASTER_BIT, &ct->status)) {
nf_conntrack_get(&user->ct_general);
ct->master = user;
}
}
skb_nfct_reset(uskb);
}
return natflow_user_get(ct);
}
static inline void natflow_auth_reply_payload_fin(const char *payload, int payload_len, struct sk_buff *oskb, const struct net_device *dev, int pppoe_hdr)
{
struct sk_buff *nskb;
struct ethhdr *neth, *oeth;
struct iphdr *niph, *oiph;
struct tcphdr *otcph, *ntcph;
int len;
unsigned int csum;
int offset, header_len;
char *data;
int pppoe_len = 0;
if (pppoe_hdr) {
pppoe_len = PPPOE_SES_HLEN;
skb_push(oskb, PPPOE_SES_HLEN);
oskb->protocol = __constant_htons(ETH_P_PPP_SES);
}
oeth = (struct ethhdr *)skb_mac_header(oskb);
oiph = ip_hdr(oskb);
otcph = (struct tcphdr *)((void *)oiph + oiph->ihl*4);
offset = pppoe_len + sizeof(struct iphdr) + sizeof(struct tcphdr) + payload_len - oskb->len;
header_len = offset < 0 ? 0 : offset;
nskb = skb_copy_expand(oskb, skb_headroom(oskb), header_len, GFP_ATOMIC);
if (!nskb) {
NATFLOW_ERROR("alloc_skb fail\n");
goto out;
}
if (offset <= 0) {
if (pskb_trim(nskb, nskb->len + offset)) {
NATFLOW_ERROR("pskb_trim fail: len=%d, offset=%d\n", nskb->len, offset);
consume_skb(nskb);
goto out;
}
} else {
nskb->len += offset;
nskb->tail += offset;
}
//setup mac header
neth = eth_hdr(nskb);
niph = ip_hdr(nskb);
if ((char *)niph - (char *)neth >= ETH_HLEN) {
memcpy(neth->h_dest, oeth->h_source, ETH_ALEN);
memcpy(neth->h_source, oeth->h_dest, ETH_ALEN);
//neth->h_proto = htons(ETH_P_IP);
}
//setup ip header
memset(niph, 0, sizeof(struct iphdr));
niph->saddr = oiph->daddr;
niph->daddr = oiph->saddr;
niph->version = oiph->version;
niph->ihl = 5;
niph->tos = 0;
niph->tot_len = htons(nskb->len - pppoe_len);
niph->ttl = 0x80;
niph->protocol = oiph->protocol;
niph->id = __constant_htons(0xDEAD);
niph->frag_off = 0x0;
ip_send_check(niph);
//setup payload
data = (char *)ip_hdr(nskb) + sizeof(struct iphdr) + sizeof(struct tcphdr);
memcpy(data, payload, payload_len);
//setup tcp header
ntcph = (struct tcphdr *)((char *)ip_hdr(nskb) + sizeof(struct iphdr));
memset(ntcph, 0, sizeof(struct tcphdr));
ntcph->source = otcph->dest;
ntcph->dest = otcph->source;
ntcph->seq = otcph->ack_seq;
ntcph->ack_seq = htonl(ntohl(otcph->seq) + ntohs(oiph->tot_len) - (oiph->ihl<<2) - (otcph->doff<<2));
ntcph->doff = 5;
ntcph->ack = 1;
ntcph->psh = 1;
ntcph->fin = 1;
ntcph->window = 65535;
//sum check
len = ntohs(niph->tot_len) - (niph->ihl<<2);
csum = csum_partial((char*)ntcph, len, 0);
ntcph->check = tcp_v4_check(len, niph->saddr, niph->daddr, csum);
//ready to send out
skb_push(nskb, (char *)niph - (char *)neth - pppoe_len);
if (pppoe_hdr) {
struct pppoe_hdr *ph = (struct pppoe_hdr *)((void *)eth_hdr(nskb) + ETH_HLEN);
ph->length = htons(ntohs(ip_hdr(nskb)->tot_len) + 2);
}
nskb->dev = (struct net_device *)dev;
nskb->ip_summed = CHECKSUM_UNNECESSARY;
dev_queue_xmit(nskb);
out:
if (pppoe_hdr) {
oskb->protocol = __constant_htons(ETH_P_IP);
skb_pull(oskb, PPPOE_SES_HLEN);
}
}
static void natflow_auth_http_302(const struct net_device *dev, struct sk_buff *skb, natflow_fakeuser_t *user, int pppoe_hdr)
{
struct fakeuser_data_t *fud = natflow_fakeuser_data(user);
const char *http_header_fmt = ""
"HTTP/1.1 302 Moved Temporarily\r\n"
"Connection: close\r\n"
"Cache-Control: no-cache\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Location: %s\r\n"
"Content-Length: %u\r\n"
"\r\n";
const char *http_data_fmt = ""
"<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\r\n"
"<TITLE>302 Moved</TITLE></HEAD><BODY>\r\n"
"<H1>302 Moved</H1>\r\n"
"The document has moved\r\n"
"<A HREF=\"%s\">here</A>.\r\n"
"</BODY></HTML>\r\n";
int n = 0;
struct {
char location[128];
char data[384];
char header[384];
char payload[0];
} *http = kmalloc(2048, GFP_ATOMIC);
if (!http)
return;
snprintf(http->location, sizeof(http->location), "http://%pI4/index.html?ip=%pI4&mac=%02X-%02X-%02X-%02X-%02X-%02X&rid=%u&_t=%lu",
&redirect_ip, &user->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip,
fud->macaddr[0], fud->macaddr[1], fud->macaddr[2],
fud->macaddr[3], fud->macaddr[4], fud->macaddr[5],
fud->auth_rule_id, jiffies);
http->location[sizeof(http->location) - 1] = 0;
n = snprintf(http->data, sizeof(http->data), http_data_fmt, http->location);
http->data[sizeof(http->data) - 1] = 0;
snprintf(http->header, sizeof(http->header), http_header_fmt, http->location, n);
http->header[sizeof(http->header) - 1] = 0;
n = sprintf(http->payload, "%s%s", http->header, http->data);
natflow_auth_reply_payload_fin(http->payload, n, skb, dev, pppoe_hdr);
kfree(http);
}
static inline void natflow_auth_open_weixin_reply(const struct net_device *dev, struct sk_buff *skb, int pppoe_hdr)
{
const char *http_header_fmt = ""
"HTTP/1.1 200 OK\r\n"
"Connection: close\r\n"
"Cache-Control: no-cache\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Content-Length: %u\r\n"
"\r\n";
const char *http_data_fmt = ""
"<!DOCTYPE html>\r\n"
"<html class='no-js'>\r\n"
"<head>\r\n"
"<meta charset='utf-8'>\r\n"
"<meta name='viewport' content='initial-scale=1.0, maximum-scale=1.0, user-scalable=no'>\r\n"
"<script type='text/javascript' src='http://%pI4/admin/js/guanzhu.js?%u'></script>\r\n"
"</head>\r\n"
"<body>\r\n"
"</body>\r\n"
"</html>\r\n";
int n = 0;
struct {
char data[384];
char header[384];
char payload[0];
} *http = kmalloc(2048, GFP_ATOMIC);
if (!http)
return;
n = snprintf(http->data, sizeof(http->data), http_data_fmt, &redirect_ip, jiffies);
http->data[sizeof(http->data) - 1] = 0;
snprintf(http->header, sizeof(http->header), http_header_fmt, n);
http->header[sizeof(http->header) - 1] = 0;
n = sprintf(http->payload, "%s%s", http->header, http->data);
natflow_auth_reply_payload_fin(http->payload, n, skb, dev, pppoe_hdr);
kfree(http);
}
static inline void natflow_auth_convert_tcprst(struct sk_buff *skb)
{
int offset = 0;
int len;
struct iphdr *iph;
struct tcphdr *tcph;
iph = ip_hdr(skb);
if (iph->protocol != IPPROTO_TCP)
return;
if (skb->len < ntohs(iph->tot_len))
return;
tcph = (struct tcphdr *)((void *)iph + iph->ihl * 4);
offset = iph->ihl * 4 + sizeof(struct tcphdr) - skb->len;
if (offset > 0)
return;
if (pskb_trim(skb, skb->len + offset))
return;
tcph->res1 = 0;
tcph->doff = 5;
tcph->syn = 0;
tcph->rst = 1;
tcph->psh = 0;
tcph->ack = 0;
tcph->fin = 0;
tcph->urg = 0;
tcph->ece = 0;
tcph->cwr = 0;
tcph->window = __constant_htons(0);
tcph->check = 0;
tcph->urg_ptr = 0;
iph->tot_len = htons(skb->len);
iph->id = __constant_htons(0xDEAD);
iph->frag_off = 0;
len = ntohs(iph->tot_len);
if (skb->ip_summed == CHECKSUM_PARTIAL) {
iph->check = 0;
iph->check = ip_fast_csum(iph, iph->ihl);
tcph->check = 0;
tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len - iph->ihl * 4, IPPROTO_TCP, 0);
skb->csum_start = (unsigned char *)tcph - skb->head;
skb->csum_offset = offsetof(struct tcphdr, check);
} else {
iph->check = 0;
iph->check = ip_fast_csum(iph, iph->ihl);
skb->csum = 0;
tcph->check = 0;
skb->csum = skb_checksum(skb, iph->ihl * 4, len - iph->ihl * 4, 0);
tcph->check = csum_tcpudp_magic(iph->saddr, iph->daddr, len - iph->ihl * 4, iph->protocol, skb->csum);
}
}
static inline void natflow_auth_tcp_reply_finack(const struct net_device *dev, struct sk_buff *oskb, int pppoe_hdr)
{
struct sk_buff *nskb;
struct ethhdr *neth, *oeth;
struct iphdr *niph, *oiph;
struct tcphdr *otcph, *ntcph;
int len;
unsigned int csum;
int offset, header_len;
int pppoe_len = 0;
if (pppoe_hdr) {
pppoe_len = PPPOE_SES_HLEN;
skb_push(oskb, PPPOE_SES_HLEN);
oskb->protocol = __constant_htons(ETH_P_PPP_SES);
}
oeth = (struct ethhdr *)skb_mac_header(oskb);
oiph = ip_hdr(oskb);
otcph = (struct tcphdr *)((void *)oiph + oiph->ihl*4);
offset = pppoe_len + sizeof(struct iphdr) + sizeof(struct tcphdr) - oskb->len;