-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathxt_NAT.c
1725 lines (1458 loc) · 63.3 KB
/
xt_NAT.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
#include <linux/module.h>
#include <linux/timer.h>
#include <linux/skbuff.h>
#include <linux/spinlock.h>
#include <linux/jhash.h>
#include <linux/vmalloc.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/icmp.h>
#include <linux/version.h>
#include <linux/netfilter/x_tables.h>
#include <linux/inet.h>
#include <linux/proc_fs.h>
#include <net/tcp.h>
#include "compat.h"
#include "xt_NAT.h"
#define FLAG_REPLIED (1 << 0) /* 000001 */
#define FLAG_TCP_FIN (1 << 1) /* 000010 */
#define TCP_SYN_ACK 0x12
#define TCP_FIN_RST 0x05
static LIST_HEAD(usock_list);
static int sndbuf = 1310720;
static int engine_id = 0;
static unsigned int pdu_data_records = 0;
static unsigned int pdu_seq = 0;
struct netflow5_pdu pdu;
static DEFINE_SPINLOCK(nfsend_lock);
static atomic64_t sessions_active = ATOMIC_INIT(0);
static atomic64_t users_active = ATOMIC_INIT(0);
static atomic64_t sessions_tried = ATOMIC_INIT(0);
static atomic64_t sessions_created = ATOMIC_INIT(0);
static atomic64_t dnat_dropped = ATOMIC_INIT(0);
static atomic64_t frags = ATOMIC_INIT(0);
static atomic64_t related_icmp = ATOMIC_INIT(0);
static char nat_pool_buf[128] = "127.0.0.1-127.0.0.1";
static char *nat_pool = nat_pool_buf;
module_param(nat_pool, charp, 0444);
MODULE_PARM_DESC(nat_pool, "NAT pool range (addr_start-addr_end), default = 127.0.0.1-127.0.0.1");
static int nat_hash_size = 256 * 1024;
module_param(nat_hash_size, int, 0444);
MODULE_PARM_DESC(nat_hash_size, "nat hash size, default = 256k");
static int users_hash_size = 4096;
module_param(users_hash_size, int, 0444);
MODULE_PARM_DESC(users_hash_size, "users hash size, default = 4k");
static char nf_dest_buf[128] = "";
static char *nf_dest = nf_dest_buf;
module_param(nf_dest, charp, 0444);
MODULE_PARM_DESC(nf_dest, "Netflow v5 collectors (addr1:port1[,addr2:port2]), default = none");
u_int32_t nat_htable_vector = 0;
u_int32_t users_htable_vector = 0;
static spinlock_t *create_session_lock;
static DEFINE_SPINLOCK(sessions_timer_lock);
static DEFINE_SPINLOCK(users_timer_lock);
static struct timer_list sessions_cleanup_timer, users_cleanup_timer, nf_send_timer;
struct proc_dir_entry *proc_net_nat;
struct netflow_sock {
struct list_head list;
struct socket *sock;
struct sockaddr_storage addr; // destination
};
struct xt_nat_htable {
uint8_t use;
spinlock_t lock;
struct hlist_head session;
};
struct nat_htable_ent {
struct rcu_head rcu;
struct hlist_node list_node;
uint8_t proto;
uint32_t addr;
uint16_t port;
struct nat_session *data;
};
struct nat_session {
uint32_t in_addr;
uint16_t in_port;
uint16_t out_port;
int16_t timeout;
uint8_t flags;
};
struct xt_users_htable {
uint8_t use;
spinlock_t lock;
struct hlist_head user;
};
struct user_htable_ent {
struct rcu_head rcu;
struct hlist_node list_node;
uint32_t addr;
uint16_t tcp_count;
uint16_t udp_count;
uint16_t other_count;
uint8_t idle;
};
struct xt_users_htable *ht_users;
static u_int32_t nat_pool_start;
static u_int32_t nat_pool_end;
struct xt_nat_htable *ht_inner, *ht_outer;
static char *print_sockaddr(const struct sockaddr_storage *ss)
{
static char buf[64];
snprintf(buf, sizeof(buf), "%pISpc", ss);
return buf;
}
static inline long timer_end(struct timespec start_time)
{
struct timespec end_time;
getrawmonotonic(&end_time);
return(end_time.tv_nsec - start_time.tv_nsec);
}
static inline struct timespec timer_start(void)
{
struct timespec start_time;
getrawmonotonic(&start_time);
return start_time;
}
static inline u_int32_t
get_pool_size(void)
{
return ntohl(nat_pool_end)-ntohl(nat_pool_start)+1;
}
static inline u_int32_t
get_nat_addr(const u_int32_t addr)
{
return htonl(ntohl(nat_pool_start)+reciprocal_scale(jhash_1word(addr, 0), get_pool_size()));
}
static inline u_int32_t
get_hash_nat_ent(const uint8_t proto, const u_int32_t addr, const uint16_t port)
{
return reciprocal_scale(jhash_3words((u32)proto, addr, (u32)port, 0), nat_hash_size);
}
static inline u_int32_t
get_hash_user_ent(const u_int32_t addr)
{
return reciprocal_scale(jhash_1word(addr, 0), users_hash_size);
}
static inline u_int32_t pool_table_create(void)
{
unsigned int sz; /* (bytes) */
unsigned int pool_size;
int i;
pool_size = get_pool_size();
sz = sizeof(spinlock_t) * pool_size;
create_session_lock = kzalloc(sz, GFP_KERNEL);
if (create_session_lock == NULL)
return -ENOMEM;
for (i = 0; i < pool_size; i++) {
spin_lock_init(&create_session_lock[i]);
}
printk(KERN_INFO "xt_NAT DEBUG: nat pool table mem: %d\n", sz);
return 0;
}
void pool_table_remove(void)
{
kfree(create_session_lock);
printk(KERN_INFO "xt_NAT pool_table_remove DEBUG: removed\n");
}
static int users_htable_create(void)
{
unsigned int sz; /* (bytes) */
int i;
sz = sizeof(struct xt_users_htable) * users_hash_size;
ht_users = kzalloc(sz, GFP_KERNEL);
if (ht_users == NULL)
return -ENOMEM;
for (i = 0; i < users_hash_size; i++) {
spin_lock_init(&ht_users[i].lock);
INIT_HLIST_HEAD(&ht_users[i].user);
ht_users[i].use = 0;
}
printk(KERN_INFO "xt_NAT DEBUG: users htable mem: %d\n", sz);
return 0;
}
void users_htable_remove(void)
{
struct user_htable_ent *user;
struct hlist_head *head;
struct hlist_node *next;
int i;
for (i = 0; i < users_hash_size; i++) {
spin_lock_bh(&ht_users[i].lock);
head = &ht_users[i].user;
hlist_for_each_entry_safe(user, next, head, list_node) {
hlist_del_rcu(&user->list_node);
ht_users[i].use--;
kfree_rcu(user, rcu);
}
if (ht_users[i].use != 0) {
printk(KERN_WARNING "xt_NAT users_htable_remove ERROR: bad use value: %d in element %d\n", ht_users[i].use, i);
}
spin_unlock_bh(&ht_users[i].lock);
}
kfree(ht_users);
printk(KERN_INFO "xt_NAT users_htable_remove DONE\n");
return;
}
void nat_htable_remove(void)
{
struct nat_htable_ent *session;
struct hlist_head *head;
struct hlist_node *next;
unsigned int i;
void *p;
for (i = 0; i < nat_hash_size; i++) {
spin_lock_bh(&ht_inner[i].lock);
head = &ht_inner[i].session;
hlist_for_each_entry_safe(session, next, head, list_node) {
hlist_del_rcu(&session->list_node);
ht_inner[i].use--;
kfree_rcu(session, rcu);
}
if (ht_inner[i].use != 0) {
printk(KERN_WARNING "xt_NAT nat_htable_remove inner ERROR: bad use value: %d in element %d\n", ht_inner[i].use, i);
}
spin_unlock_bh(&ht_inner[i].lock);
}
for (i = 0; i < nat_hash_size; i++) {
spin_lock_bh(&ht_outer[i].lock);
head = &ht_outer[i].session;
hlist_for_each_entry_safe(session, next, head, list_node) {
hlist_del_rcu(&session->list_node);
ht_outer[i].use--;
p = session->data;
kfree_rcu(session, rcu);
kfree(p);
}
if (ht_outer[i].use != 0) {
printk(KERN_WARNING "xt_NAT nat_htable_remove outer ERROR: bad use value: %d in element %d\n", ht_outer[i].use, i);
}
spin_unlock_bh(&ht_outer[i].lock);
}
printk(KERN_INFO "xt_NAT nat_htable_remove DONE\n");
return;
}
static int nat_htable_create(void)
{
unsigned int sz; /* (bytes) */
int i;
sz = sizeof(struct xt_nat_htable) * nat_hash_size;
ht_inner = kzalloc(sz, GFP_KERNEL);
if (ht_inner == NULL)
return -ENOMEM;
for (i = 0; i < nat_hash_size; i++) {
spin_lock_init(&ht_inner[i].lock);
INIT_HLIST_HEAD(&ht_inner[i].session);
ht_inner[i].use = 0;
}
printk(KERN_INFO "xt_NAT DEBUG: sessions htable inner mem: %d\n", sz);
ht_outer = kzalloc(sz, GFP_KERNEL);
if (ht_outer == NULL)
return -ENOMEM;
for (i = 0; i < nat_hash_size; i++) {
spin_lock_init(&ht_outer[i].lock);
INIT_HLIST_HEAD(&ht_outer[i].session);
ht_outer[i].use = 0;
}
printk(KERN_INFO "xt_NAT DEBUG: sessions htable outer mem: %d\n", sz);
return 0;
}
struct nat_htable_ent *lookup_session(struct xt_nat_htable *ht, const uint8_t proto, const u_int32_t addr, const uint16_t port)
{
struct nat_htable_ent *session;
struct hlist_head *head;
unsigned int hash;
hash = get_hash_nat_ent(proto, addr, port);
if (ht[hash].use == 0)
return NULL;
head = &ht[hash].session;
hlist_for_each_entry_rcu(session, head, list_node) {
if (session->addr == addr && session->port == port && session->proto == proto && session->data->timeout > 0) {
return session;
} else {
//printk(KERN_DEBUG "xt_NAT lookup_session miss: %d - %pI4:%d\n", session->proto, &session->addr, ntohs(session->port));
}
}
return NULL;
}
static uint16_t search_free_l4_port(const uint8_t proto, const u_int32_t nataddr, const uint16_t userport)
{
uint16_t i, freeport;
for(i = 0; i < 64512; i++) {
freeport = ntohs(userport) + i;
if (freeport < 1024) {
freeport += 1024;
}
//printk(KERN_DEBUG "xt_NAT search_free_l4_port: check nat port = %d\n", freeport);
if(!lookup_session(ht_outer, proto, nataddr, htons(freeport))) {
return htons(freeport);
}
}
return 0;
}
static int check_user_limits(const u_int8_t proto, const u_int32_t addr)
{
struct user_htable_ent *user;
struct hlist_head *head;
unsigned int hash, is_found, ret;
unsigned int sessions, session_limit;
hash = get_hash_user_ent(addr);
rcu_read_lock_bh();
head = &ht_users[hash].user;
is_found=0;
hlist_for_each_entry_rcu(user, head, list_node) {
if (user->addr == addr && user->idle < 15) {
//printk(KERN_DEBUG "xt_NAT check_user_limits hit: %pI4\n", &user->addr);
if (proto == IPPROTO_TCP) {
sessions = user->tcp_count;
session_limit = 4096;
} else if (proto == IPPROTO_UDP) {
sessions = user->udp_count;
session_limit = 4096;
} else {
sessions = user->other_count;
session_limit = 4096;
}
is_found=1;
break;
} else {
//printk(KERN_DEBUG "xt_NAT check_user_limits miss: %pI4\n", &user->addr);
}
}
ret=1;
if (is_found==1) {
//printk(KERN_DEBUG "xt_NAT check_user_limits: sessions = %d of %d\n", sessions, session_limit);
if (sessions < session_limit) {
ret=1;
} else {
ret=0;
}
} else {
//printk(KERN_DEBUG "xt_NAT check_user_limits is not found: %pI4\n", &addr);
ret=1;
}
rcu_read_unlock_bh();
return ret;
}
void update_user_limits(const u_int8_t proto, const u_int32_t addr, const int8_t operation)
{
struct user_htable_ent *user;
struct hlist_head *head;
unsigned int hash, is_found;
unsigned int sz;
//u_int32_t nataddr;
hash = get_hash_user_ent(addr);
spin_lock_bh(&ht_users[hash].lock);
head = &ht_users[hash].user;
is_found=0;
hlist_for_each_entry(user, head, list_node) {
if (user->addr == addr && user->idle < 15) {
//printk(KERN_DEBUG "xt_NAT check_user_limits hit: %pI4\n", &user->addr);
is_found=1;
break;
} else {
//printk(KERN_DEBUG "xt_NAT check_user_limits miss: %pI4\n", &user->addr);
}
}
if (likely(is_found==1)) {
user->idle = 0;
if (proto == IPPROTO_TCP) {
user->tcp_count += operation;
} else if (proto == IPPROTO_UDP) {
user->udp_count += operation;
} else {
user->other_count += operation;
}
} else {
//printk(KERN_DEBUG "xt_NAT update_user_limits is not found: %pI4\n", &addr);
//printk(KERN_DEBUG "xt_NAT update_user_limits: add user_session entry to htable\n");
sz = sizeof(struct user_htable_ent);
user = kzalloc(sz, GFP_ATOMIC);
if (user == NULL) {
printk(KERN_WARNING "xt_NAT update_user_limits ERROR: Cannot allocate memory for user_session\n");
spin_unlock_bh(&ht_users[hash].lock);
return;
}
user->addr = addr;
user->tcp_count = 0;
user->udp_count = 0;
user->other_count = 0;
user->idle = 0;
if (proto == IPPROTO_TCP) {
user->tcp_count += operation;
} else if (proto == IPPROTO_UDP) {
user->udp_count += operation;
} else {
user->other_count += operation;
}
hlist_add_head_rcu(&user->list_node, &ht_users[hash].user);
ht_users[hash].use++;
atomic64_inc(&users_active);
//nataddr = get_nat_addr(user->addr);
//printk(KERN_DEBUG "xt_NAT NEW: %pI4 -> %pI4\n", &user->addr, &nataddr);
}
spin_unlock_bh(&ht_users[hash].lock);
return;
}
/* socket code */
static void sk_error_report(struct sock *sk)
{
/* clear connection refused errors if any */
sk->sk_err = 0;
return;
}
static struct socket *usock_open_sock(const struct sockaddr_storage *addr, void *user_data)
{
struct socket *sock;
int error;
if ((error = sock_create_kern(addr->ss_family, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
printk(KERN_WARNING "xt_NAT NEL: sock_create_kern error %d\n", -error);
return NULL;
}
sock->sk->sk_allocation = GFP_ATOMIC;
sock->sk->sk_prot->unhash(sock->sk); /* hidden from input */
sock->sk->sk_error_report = &sk_error_report; /* clear ECONNREFUSED */
sock->sk->sk_user_data = user_data; /* usock */
if (sndbuf < SOCK_MIN_SNDBUF)
sndbuf = SOCK_MIN_SNDBUF;
if (sndbuf)
sock->sk->sk_sndbuf = sndbuf;
else
sndbuf = sock->sk->sk_sndbuf;
error = sock->ops->connect(sock, (struct sockaddr *)addr, sizeof(*addr), 0);
if (error < 0) {
printk(KERN_WARNING "xt_NAT NEL: error connecting UDP socket %d,"
" don't worry, will try reconnect later.\n", -error);
/* ENETUNREACH when no interfaces */
sock_release(sock);
return NULL;
}
return sock;
}
static void netflow_sendmsg(void *buffer, const int len)
{
struct msghdr msg = { .msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL };
struct kvec iov = { buffer, len };
struct netflow_sock *usock;
int ret;
//printk(KERN_DEBUG "xt_NAT NEL: Netflow exporting function\n");
list_for_each_entry(usock, &usock_list, list) {
//printk(KERN_DEBUG "xt_NAT NEL: Exporting PDU to collector N\n");
if (!usock->sock)
usock->sock = usock_open_sock(&usock->addr, usock);
if (!usock->sock)
continue;
ret = kernel_sendmsg(usock->sock, &msg, &iov, 1, (size_t)len);
if (ret == -EINVAL) {
if (usock->sock)
sock_release(usock->sock);
usock->sock = NULL;
} else if (ret == -EAGAIN) {
printk(KERN_WARNING "xt_NAT NEL: increase sndbuf!\n");
}
}
}
static void netflow_export_pdu_v5(void)
{
struct timeval tv;
int pdusize;
//printk(KERN_DEBUG "xt_NAT NEL: Forming PDU seq %d, %d records\n", pdu_seq, pdu_data_records);
if (!pdu_data_records)
return;
pdu.version = htons(5);
pdu.nr_records = htons(pdu_data_records);
pdu.ts_uptime = htonl(jiffies_to_msecs(jiffies));
do_gettimeofday(&tv);
pdu.ts_usecs = htonl(tv.tv_sec);
pdu.ts_unsecs = htonl(tv.tv_usec);
pdu.seq = htonl(pdu_seq);
//pdu.v5.eng_type = 0;
pdu.eng_id = (__u8)engine_id;
pdusize = NETFLOW5_HEADER_SIZE + sizeof(struct netflow5_record) * pdu_data_records;
netflow_sendmsg(&pdu, pdusize);
pdu_seq += pdu_data_records;
pdu_data_records = 0;
}
static void netflow_export_flow_v5(const uint8_t proto, const u_int32_t useraddr, const uint16_t userport, const u_int32_t nataddr, const uint16_t natport, const int flags)
{
struct netflow5_record *rec;
spin_lock_bh(&nfsend_lock);
rec = &pdu.flow[pdu_data_records++];
/* make V5 flow record */
rec->s_addr = useraddr;
rec->d_addr = nataddr;
rec->nexthop = nataddr;
rec->i_ifc = 0;
rec->o_ifc = 0;
rec->nr_packets = 0;
rec->nr_octets = 0;
rec->first_ms = htonl(jiffies_to_msecs(jiffies));
rec->last_ms = htonl(jiffies_to_msecs(jiffies));
rec->s_port = userport;
rec->d_port = natport;
//rec->reserved = 0; /* pdu is always zeroized for v5 in netflow_switch_version */
if (flags == 0) {
rec->tcp_flags = TCP_SYN_ACK;
} else {
rec->tcp_flags = TCP_FIN_RST;
}
rec->protocol = proto;
rec->tos = 0;
rec->s_as = userport;
rec->d_as = natport;
rec->s_mask = 0;
rec->d_mask = 0;
//rec->padding = 0;
//printk(KERN_DEBUG "xt_NAT NEL: Add flow %pI4:%d (outside %pI4:%d) to PDU\n", &useraddr, ntohs(userport), &nataddr, ntohs(natport));
if (pdu_data_records == NETFLOW5_RECORDS_MAX)
netflow_export_pdu_v5();
spin_unlock_bh(&nfsend_lock);
}
struct nat_htable_ent *create_nat_session(const uint8_t proto, const u_int32_t useraddr, const uint16_t userport, const u_int32_t nataddr)
{
unsigned int hash;
struct nat_htable_ent *session, *session2;
struct nat_session *data_session;
uint16_t natport;
unsigned int sz;
unsigned int nataddr_id;
atomic64_inc(&sessions_tried);
if (unlikely(check_user_limits(proto, useraddr) == 0)) {
printk(KERN_NOTICE "xt_NAT: %pI4 exceed max allowed sessions\n", &useraddr);
return NULL;
}
nataddr_id = ntohl(nataddr) - ntohl(nat_pool_start);
//printk(KERN_DEBUG "xt_NAT create_nat_session: nataddr_id = %u (%u - %u)\n", nataddr_id, ntohl(nataddr), ntohl(nat_pool_start));
spin_lock_bh(&create_session_lock[nataddr_id]);
rcu_read_lock_bh();
session = lookup_session(ht_inner, proto, useraddr, userport);
if(unlikely(session)) {
//printk(KERN_DEBUG "xt_NAT create_nat_session WARN: Race Condition found\n");
spin_unlock_bh(&create_session_lock[nataddr_id]);
return lookup_session(ht_outer, proto, nataddr, session->data->out_port); //тут без потери, но с нюансами внутри nat_tg
}
rcu_read_unlock_bh();
if (likely(proto == IPPROTO_TCP || proto == IPPROTO_UDP || proto == IPPROTO_ICMP)) {
rcu_read_lock_bh();
natport = search_free_l4_port(proto, nataddr, userport);
rcu_read_unlock_bh();
if (natport == 0) {
printk(KERN_WARNING "xt_NAT create_nat_session ERROR: Not found free nat port for %d %pI4:%u -> %pI4:XXXX\n", proto, &useraddr, userport, &nataddr);
spin_unlock_bh(&create_session_lock[nataddr_id]);
return NULL;
}
} else {
natport = userport;
}
sz = sizeof(struct nat_session);
data_session = kzalloc(sz, GFP_ATOMIC);
if (unlikely(data_session == NULL)) {
printk(KERN_WARNING "xt_NAT create_nat_session ERROR: Cannot allocate memory for data_session\n");
spin_unlock_bh(&create_session_lock[nataddr_id]);
return NULL;
}
sz = sizeof(struct nat_htable_ent);
session = kzalloc(sz, GFP_ATOMIC);
if (unlikely(session == NULL)) {
printk(KERN_WARNING "xt_NAT ERROR: Cannot allocate memory for ht_inner session\n");
kfree(data_session);
spin_unlock_bh(&create_session_lock[nataddr_id]);
return NULL;
}
sz = sizeof(struct nat_htable_ent);
session2 = kzalloc(sz, GFP_ATOMIC);
if (unlikely(session2 == NULL)) {
printk(KERN_WARNING "xt_NAT ERROR: Cannot allocate memory for ht_outer session\n");
kfree(data_session);
kfree(session);
spin_unlock_bh(&create_session_lock[nataddr_id]);
return NULL;
}
data_session->in_addr = useraddr;
data_session->in_port = userport;
data_session->out_port = natport;
//data_session->timeout = 600;
data_session->timeout = 30;
data_session->flags = 0;
session->proto = proto;
session->addr = useraddr;
session->port = userport;
session->data = data_session;
session2->proto = proto;
session2->addr = nataddr;
session2->port = natport;
session2->data = data_session;
hash = get_hash_nat_ent(proto, useraddr, userport);
spin_lock_bh(&ht_inner[hash].lock);
hlist_add_head_rcu(&session->list_node, &ht_inner[hash].session);
ht_inner[hash].use++;
spin_unlock_bh(&ht_inner[hash].lock);
hash = get_hash_nat_ent(proto, nataddr, natport);
spin_lock_bh(&ht_outer[hash].lock);
hlist_add_head_rcu(&session2->list_node, &ht_outer[hash].session);
ht_outer[hash].use++;
spin_unlock_bh(&ht_outer[hash].lock);
spin_unlock_bh(&create_session_lock[nataddr_id]);
update_user_limits(proto, useraddr, 1);
netflow_export_flow_v5(proto, useraddr, userport, nataddr, natport, 0);
atomic64_inc(&sessions_created);
atomic64_inc(&sessions_active);
//printk(KERN_DEBUG "xt_NAT NEW SESSION: %d %pI4:%u -> %pI4:%u\n", session2->proto, &session2->data->in_addr, ntohs(session2->data->in_port), &session2->addr, ntohs(session2->port));
rcu_read_lock_bh();
return lookup_session(ht_outer, proto, nataddr, natport);
}
static unsigned int
nat_tg(struct sk_buff *skb, const struct xt_action_param *par)
{
struct iphdr *ip;
struct tcphdr *tcp;
struct udphdr *udp;
struct icmphdr *icmp;
struct nat_htable_ent *session;
uint32_t nat_addr;
uint16_t nat_port;
skb_frag_t *frag;
const struct xt_nat_tginfo *info = par->targinfo;
if (unlikely(skb->protocol != htons(ETH_P_IP))) {
printk(KERN_DEBUG "xt_NAT DEBUG: Drop not IP packet\n");
return NF_DROP;
}
if (unlikely(ip_hdrlen(skb) != sizeof(struct iphdr))) {
printk(KERN_DEBUG "xt_NAT DEBUG: Drop truncated IP packet\n");
return NF_DROP;
}
ip = (struct iphdr *)skb_network_header(skb);
if (unlikely(ip->frag_off & htons(IP_OFFSET))) {
printk(KERN_DEBUG "xt_NAT DEBUG: Drop fragmented IP packet\n");
return NF_DROP;
}
if (unlikely(ip->version != 4)) {
printk(KERN_DEBUG "xt_NAT DEBUG: Drop not IPv4 IP packet\n");
return NF_DROP;
}
if (info->variant == XTNAT_SNAT) {
nat_addr = get_nat_addr(ip->saddr);
//printk(KERN_DEBUG "xt_NAT SNAT: tg = SNAT, outer NAT IP = %pI4", &nat_addr);
//printk(KERN_DEBUG "xt_NAT SNAT: check IPv4 packet with src ip = %pI4 and dst ip = %pI4\n", &ip->saddr, &ip->daddr);
if (ip->protocol == IPPROTO_TCP) {
if (unlikely(skb->len < ip_hdrlen(skb) + sizeof(struct tcphdr))) {
printk(KERN_DEBUG "xt_NAT SNAT: Drop truncated TCP packet\n");
return NF_DROP;
}
skb_set_transport_header(skb, ip->ihl * 4);
tcp = (struct tcphdr *)skb_transport_header(skb);
skb_reset_transport_header(skb);
//printk(KERN_DEBUG "xt_NAT SNAT: TCP packet with src port = %d\n", ntohs(tcp->source));
rcu_read_lock_bh();
session = lookup_session(ht_inner, ip->protocol, ip->saddr, tcp->source);
if (session) {
//printk(KERN_DEBUG "xt_NAT SNAT: found session for src ip = %pI4 and src port = %d and nat port = %d\n", &ip->saddr, ntohs(tcp->source), ntohs(session->data->out_port));
csum_replace4(&ip->check, ip->saddr, nat_addr);
inet_proto_csum_replace4(&tcp->check, skb, ip->saddr, nat_addr, true);
inet_proto_csum_replace2(&tcp->check, skb, tcp->source, session->data->out_port, true);
ip->saddr = nat_addr;
tcp->source = session->data->out_port;
/* if (session->data->flags & FLAG_TCP_CLOSED) {
session->data->timeout=5;
} else if (tcp->rst || tcp->fin) {
session->data->flags |= FLAG_TCP_CLOSED;
session->data->timeout=5;
} else
*/
if (tcp->fin || tcp->rst) {
session->data->timeout=10;
session->data->flags |= FLAG_TCP_FIN;
} else if (session->data->flags & FLAG_TCP_FIN) {
session->data->timeout=10;
session->data->flags &= ~FLAG_TCP_FIN;
} else if ((session->data->flags & FLAG_REPLIED) == 0) {
session->data->timeout=30;
} else {
session->data->timeout=300;
}
/*
if ((session->data->flags & FLAG_REPLIED) == 0) {
session->data->timeout=30;
} else {
session->data->timeout=300;
}
*/
rcu_read_unlock_bh();
} else {
rcu_read_unlock_bh();
//printk(KERN_DEBUG "xt_NAT SNAT: NOT found session for src ip = %pI4 and src port = %d\n", &ip->saddr, ntohs(tcp->source));
/* if (!tcp->syn) {
//printk(KERN_DEBUG "xt_NAT SNAT: SYN flag is not set. Dropping packet\n");
return NF_DROP;
}
*/
session = create_nat_session(ip->protocol, ip->saddr, tcp->source, nat_addr);
if (session == NULL) {
printk(KERN_NOTICE "xt_NAT SNAT: Cannot create new session. Dropping packet\n");
return NF_DROP;
}
csum_replace4(&ip->check, ip->saddr, session->addr);
inet_proto_csum_replace4(&tcp->check, skb, ip->saddr, session->addr, true);
inet_proto_csum_replace2(&tcp->check, skb, session->data->in_port, session->data->out_port, true);
ip->saddr = session->addr;
tcp->source = session->data->out_port;
rcu_read_unlock_bh();
//return NF_ACCEPT;
}
} else if (ip->protocol == IPPROTO_UDP) {
if (unlikely(skb->len < ip_hdrlen(skb) + sizeof(struct udphdr))) {
printk(KERN_DEBUG "xt_NAT SNAT: Drop truncated UDP packet\n");
return NF_DROP;
}
skb_set_transport_header(skb, ip->ihl * 4);
udp = (struct udphdr *)skb_transport_header(skb);
skb_reset_transport_header(skb);
//printk(KERN_DEBUG "xt_NAT SNAT: UDP packet with src port = %d\n", ntohs(udp->source));
rcu_read_lock_bh();
session = lookup_session(ht_inner, ip->protocol, ip->saddr, udp->source);
if (session) {
//printk(KERN_DEBUG "xt_NAT SNAT: found session for src ip = %pI4 and src port = %d and nat port = %d\n", &ip->saddr, ntohs(udp->source), ntohs(session->data->out_port));
csum_replace4(&ip->check, ip->saddr, nat_addr);
if (udp->check) {
inet_proto_csum_replace4(&udp->check, skb, ip->saddr, nat_addr, true);
inet_proto_csum_replace2(&udp->check, skb, udp->source, session->data->out_port, true);
}
ip->saddr = nat_addr;
udp->source = session->data->out_port;
if ((session->data->flags & FLAG_REPLIED) == 0) {
session->data->timeout=30;
} else {
session->data->timeout=300;
}
rcu_read_unlock_bh();
} else {
rcu_read_unlock_bh();
//printk(KERN_DEBUG "xt_NAT SNAT: NOT found session for src ip = %pI4 and src port = %d\n", &ip->saddr, ntohs(udp->source));
session = create_nat_session(ip->protocol, ip->saddr, udp->source, nat_addr);
if (session == NULL) {
printk(KERN_NOTICE "xt_NAT SNAT: Cannot create new session. Dropping packet\n");
return NF_DROP;
}
csum_replace4(&ip->check, ip->saddr, session->addr);
if (udp->check) {
inet_proto_csum_replace4(&udp->check, skb, ip->saddr, session->addr, true);
inet_proto_csum_replace2(&udp->check, skb, session->data->in_port, session->data->out_port, true);
}
ip->saddr = session->addr;
udp->source = session->data->out_port;
rcu_read_unlock_bh();
//return NF_ACCEPT;
}
} else if (ip->protocol == IPPROTO_ICMP) {
if (unlikely(skb->len < ip_hdrlen(skb) + sizeof(struct icmphdr))) {
printk(KERN_DEBUG "xt_NAT SNAT: Drop truncated ICMP packet\n");
return NF_DROP;
}
skb_set_transport_header(skb, ip->ihl * 4);
icmp = (struct icmphdr *)skb_transport_header(skb);
skb_reset_transport_header(skb);
//printk(KERN_DEBUG "xt_NAT SNAT: ICMP packet with type = %d and code = %d\n", icmp->type, icmp->code);
nat_port = 0;
if (icmp->type == 0 || icmp->type == 8) {
nat_port = icmp->un.echo.id;
} else if (icmp->type == 3 || icmp->type == 4 || icmp->type == 5 || icmp->type == 11 || icmp->type == 12 || icmp->type == 31) {
}
rcu_read_lock_bh();
session = lookup_session(ht_inner, ip->protocol, ip->saddr, nat_port);
if (session) {
//printk(KERN_DEBUG "xt_NAT SNAT: found session for src ip = %pI4 and icmp id = %d\n", &ip->saddr, ntohs(nat_port));
csum_replace4(&ip->check, ip->saddr, nat_addr);
ip->saddr = nat_addr;
if (icmp->type == 0 || icmp->type == 8) {
inet_proto_csum_replace2(&icmp->checksum, skb, nat_port, session->data->out_port, true);
icmp->un.echo.id = session->data->out_port;
}
if ((session->data->flags & FLAG_REPLIED) == 0) {
session->data->timeout=30;
} else {
session->data->timeout=30;
}
rcu_read_unlock_bh();
} else {
rcu_read_unlock_bh();
//printk(KERN_DEBUG "xt_NAT SNAT: NOT found session for src ip = %pI4 and icmp id = %d\n",&ip->saddr, ntohs(nat_port));
session = create_nat_session(ip->protocol, ip->saddr, nat_port, nat_addr);
if (session == NULL) {
printk(KERN_NOTICE "xt_NAT SNAT: Cannot create new session. Dropping packet\n");
return NF_DROP;
}
csum_replace4(&ip->check, ip->saddr, session->addr);
ip->saddr = session->addr;
if (icmp->type == 0 || icmp->type == 8) {
inet_proto_csum_replace2(&icmp->checksum, skb, nat_port, session->data->out_port, true);
icmp->un.echo.id = session->data->out_port;
}
rcu_read_unlock_bh();
//return NF_ACCEPT;
}
} else {
//skb_set_transport_header(skb, ip->ihl * 4);
//printk(KERN_DEBUG "xt_NAT SNAT: Generic IP packet\n");
rcu_read_lock_bh();
session = lookup_session(ht_inner, ip->protocol, ip->saddr, 0);
if (session) {
//printk(KERN_DEBUG "xt_NAT SNAT: found session for src ip = %pI4\n", &ip->saddr);
csum_replace4(&ip->check, ip->saddr, nat_addr);
ip->saddr = nat_addr;
if ((session->data->flags & FLAG_REPLIED) == 0) {
session->data->timeout=30;
} else {
session->data->timeout=300;
}
rcu_read_unlock_bh();
} else {
rcu_read_unlock_bh();
//printk(KERN_DEBUG "xt_NAT SNAT: NOT found session for src ip = %pI4\n",&ip->saddr);
session = create_nat_session(ip->protocol, ip->saddr, 0, nat_addr);
if (session == NULL) {
printk(KERN_NOTICE "xt_NAT SNAT: Cannot create new session. Dropping packet\n");
return NF_DROP;
}
csum_replace4(&ip->check, ip->saddr, session->addr);
ip->saddr = session->addr;
rcu_read_unlock_bh();
//return NF_ACCEPT;
}
}
} else if (info->variant == XTNAT_DNAT) {
//printk(KERN_DEBUG "xt_NAT DNAT: tg = DNAT, outer NAT IP = %pI4", &ip->daddr);
//printk(KERN_DEBUG "xt_NAT DNAT: check IPv4 packet with src ip = %pI4 and dst nat ip = %pI4\n", &ip->saddr, &ip->daddr);
if (ip->protocol == IPPROTO_TCP) {
if (unlikely(skb->len < ip_hdrlen(skb) + sizeof(struct tcphdr))) {
printk(KERN_DEBUG "xt_NAT DNAT: Drop truncated TCP packet\n");
return NF_DROP;
}
skb_set_transport_header(skb, ip->ihl * 4);