forked from fanthos/openwrt-official-builds-fullcone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openwrt-22.03.3-fullcone.patch
3193 lines (3184 loc) · 103 KB
/
openwrt-22.03.3-fullcone.patch
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
diff --git a/package/kernel/nft-fullcone/Makefile b/package/kernel/nft-fullcone/Makefile
new file mode 100644
index 0000000000..f8108a044c
--- /dev/null
+++ b/package/kernel/nft-fullcone/Makefile
@@ -0,0 +1,44 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Copyright (c) 2018 Chion Tang <tech@chionlab.moe>
+# Original xt_FULLCONENAT and related iptables extension author
+# Copyright (c) 2019-2022 GitHub/llccd Twitter/@gNodeB
+# Added IPv6 support for xt_FULLCONENAT and ip6tables extension
+# Ported to recent kernel versions
+# Copyright (c) 2022 Syrone Wong <wong.syrone@gmail.com>
+# Massively rewrite the whole module, split the original code into library and nftables 'fullcone' expression module
+
+include $(TOPDIR)/rules.mk
+include $(INCLUDE_DIR)/kernel.mk
+
+PKG_NAME:=nft-fullcone
+PKG_RELEASE:=1
+
+include $(INCLUDE_DIR)/package.mk
+
+define KernelPackage/nft-fullcone
+ SUBMENU:=Netfilter Extensions
+ DEPENDS:=@IPV6 +kmod-nft-core +kmod-nf-conntrack +kmod-nf-conntrack6
+ TITLE:=nftables fullcone expression support
+ FILES:= $(PKG_BUILD_DIR)/nft_fullcone.ko
+# KCONFIG:= CONFIG_NFT_FULLCONE=y CONFIG_NF_NAT=y CONFIG_NF_NAT_IPV6=y CONFIG_NF_CONNTRACK_EVENTS=y CONFIG_NF_CONNTRACK_CHAIN_EVENTS=y
+ PROVIDES:=$(PKG_NAME)
+ AUTOLOAD:=$(call AutoProbe,nft_fullcone)
+endef
+
+define KernelPackage/nft-fullcone/Description
+Kernel module adds the fullcone expression that you can use
+to perform NAT in the RFC3489-compatible full cone SNAT flavour.
+Currently only UDP traffic is supported for full-cone NAT.
+For other protos FULLCONENAT is equivalent to MASQUERADE.
+endef
+
+# make use of all CPUs
+define Build/Compile
+ +$(MAKE) $(PKG_JOBS) $(KERNEL_MAKEOPTS) \
+ M="$(PKG_BUILD_DIR)" \
+ EXTRA_CFLAGS="$(BUILDFLAGS)" \
+ $(if $(CONFIG_IPv6),EXTRA_CFLAGS+="-DCONFIG_SFE_ECM",) \
+ modules
+endef
+
+$(eval $(call KernelPackage,nft-fullcone))
diff --git a/package/kernel/nft-fullcone/src/Makefile b/package/kernel/nft-fullcone/src/Makefile
new file mode 100644
index 0000000000..8427ebb0a1
--- /dev/null
+++ b/package/kernel/nft-fullcone/src/Makefile
@@ -0,0 +1,18 @@
+#
+# Makefile for Nftables/netfilter fullcone expression support.
+#
+
+# module name, should not have the same name as src file names
+TARGET = nft_fullcone
+
+obj-m += $(TARGET).o
+
+$(TARGET)-objs := \
+ nf_nat_fullcone.o \
+ nft_ext_fullcone.o
+
+# product
+ccflags-y += -Werror -Wall
+
+# develop
+#ccflags-y += -Wall -Wno-unused-function
diff --git a/package/kernel/nft-fullcone/src/nf_nat_fullcone.c b/package/kernel/nft-fullcone/src/nf_nat_fullcone.c
new file mode 100644
index 0000000000..0757ef9b15
--- /dev/null
+++ b/package/kernel/nft-fullcone/src/nf_nat_fullcone.c
@@ -0,0 +1,1590 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Nftables NAT extension: fullcone expression support library
+ *
+ * Copyright (c) 2018 Chion Tang <tech@chionlab.moe>
+ * Original xt_FULLCONENAT and related iptables extension author
+ * Copyright (c) 2019-2022 GitHub/llccd Twitter/@gNodeB
+ * Added IPv6 support for xt_FULLCONENAT and ip6tables extension
+ * Ported to recent kernel versions
+ * Copyright (c) 2022 Syrone Wong <wong.syrone@gmail.com>
+ * Massively rewrite the whole module, split the original code into library and nftables 'fullcone' expression module
+ */
+
+#define pr_fmt(fmt) "fullcone " KBUILD_MODNAME ": " fmt
+
+#include <linux/version.h>
+#include <linux/types.h>
+#include <linux/list.h>
+#include <linux/hashtable.h>
+#include <linux/atomic.h>
+#include <linux/kernel.h>
+#include <linux/jhash.h>
+
+#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
+#include <linux/notifier.h>
+#endif
+
+#include <linux/netfilter.h>
+#include <linux/netfilter_ipv4.h>
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+#include <linux/netfilter_ipv6.h>
+#include <linux/ipv6.h>
+#include <net/addrconf.h>
+#endif
+
+#include "nf_nat_fullcone.h"
+
+/*
+ * FULLCONE_HKEY generates u32 hash value
+ * Modified from net/netfilter/ipset/ip_set_hash_gen.h
+ * dataptr: a pointer
+ * datatypelen: sizeof(struct blah) or sizeof(u32)
+ * initval: initial value
+ * htable_bits: hashtable bits
+ */
+#define FULLCONE_HKEY(dataptr, datatypelen, initval, htable_bits) \
+({ \
+ const u32 *__k = (const u32 *)(dataptr); \
+ u32 __l = (datatypelen) / sizeof(u32); \
+ \
+ BUILD_BUG_ON((datatypelen) % sizeof(u32) != 0); \
+ \
+ jhash2(__k, __l, (initval)) & jhash_mask((htable_bits)); \
+})
+
+#define HASHTABLE_BUCKET_BITS 10
+
+/* static variables */
+
+static DEFINE_HASHTABLE(mapping_table_by_ext_port, HASHTABLE_BUCKET_BITS);
+static DEFINE_HASHTABLE(mapping_table_by_int_src, HASHTABLE_BUCKET_BITS);
+
+static DEFINE_SPINLOCK(fullconenat_lock);
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+static DEFINE_HASHTABLE(mapping6_table_by_ext_port, HASHTABLE_BUCKET_BITS);
+static DEFINE_HASHTABLE(mapping6_table_by_int_src, HASHTABLE_BUCKET_BITS);
+
+static DEFINE_SPINLOCK(fullconenat6_lock);
+#endif
+
+static LIST_HEAD(dying_tuple_list);
+static DEFINE_SPINLOCK(dying_tuple_list_lock);
+
+/* static variables end */
+
+/* forward declaration */
+
+#if IS_ENABLED(CONFIG_IPV6)
+static int nat_ipv6_dev_get_saddr(struct net *net, const struct net_device *dev,
+ const struct in6_addr *daddr, unsigned int srcprefs, struct in6_addr *saddr);
+#endif
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+/* non-atomic: can only be called serially within lock zones. */
+static char *fullcone_nf_ct_stringify_tuple6(const struct nf_conntrack_tuple
+ *t);
+#endif
+/* non-atomic: can only be called serially within lock zones. */
+static char *nf_ct_stringify_tuple(const struct nf_conntrack_tuple *t);
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+static struct nat_mapping6 *allocate_mapping6(const union nf_inet_addr
+ *int_addr,
+ const uint16_t int_port,
+ const uint16_t port, const union nf_inet_addr *addr);
+#endif
+static struct nat_mapping *allocate_mapping(const __be32 int_addr,
+ const uint16_t int_port, const uint16_t port, const __be32 addr);
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+static void add_original_tuple_to_mapping6(struct nat_mapping6 *mapping, const struct nf_conntrack_tuple
+ *original_tuple);
+#endif
+static void add_original_tuple_to_mapping(struct nat_mapping *mapping, const struct nf_conntrack_tuple
+ *original_tuple);
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+static struct nat_mapping6 *get_mapping6_by_int_src(const union nf_inet_addr
+ *src_ip, const uint16_t src_port, const union nf_inet_addr
+ *ext_ip);
+#endif
+
+static struct nat_mapping *get_mapping_by_int_src(const __be32 src_ip, const uint16_t src_port, const __be32 ext_ip);
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+static struct nat_mapping6 *get_mapping6_by_int_src_inrange(const union
+ nf_inet_addr
+ *src_ip, const uint16_t src_port, const union
+ nf_inet_addr
+ *min_ip, const union
+ nf_inet_addr
+ *max_ip);
+#endif
+static struct nat_mapping *get_mapping_by_int_src_inrange(const __be32 src_ip,
+ const uint16_t
+ src_port, const __be32 min_ip, const __be32 max_ip);
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+static void kill_mapping6(struct nat_mapping6 *mapping);
+#endif
+static void kill_mapping(struct nat_mapping *mapping);
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+
+/* check if a mapping is valid.
+ * possibly delete and free an invalid mapping.
+ * the mapping should not be used anymore after check_mapping6() returns 0. */
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
+static int check_mapping6(struct nat_mapping6 *mapping, struct net *net, const struct nf_conntrack_zone *zone);
+#else
+static int check_mapping6(struct nat_mapping6 *mapping, struct net *net, const u16 zone);
+#endif
+
+#endif
+
+/* check if a mapping is valid.
+ * possibly delete and free an invalid mapping.
+ * the mapping should not be used anymore after check_mapping() returns 0. */
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
+static int check_mapping(struct nat_mapping *mapping, struct net *net, const struct nf_conntrack_zone *zone);
+#else
+static int check_mapping(struct nat_mapping *mapping, struct net *net, const u16 zone);
+#endif
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
+static struct nat_mapping6 *get_mapping6_by_ext_port(const uint16_t port, const union nf_inet_addr
+ *ext_ip, struct net *net, const struct
+ nf_conntrack_zone *zone);
+#else
+static struct nat_mapping6 *get_mapping6_by_ext_port(const uint16_t port, const union nf_inet_addr
+ *ext_ip, struct net *net, const u16 zone);
+#endif
+#endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
+static struct nat_mapping *get_mapping_by_ext_port(const uint16_t port, const __be32 ext_ip, struct net *net, const struct
+ nf_conntrack_zone *zone);
+#else
+static struct nat_mapping *get_mapping_by_ext_port(const uint16_t port,
+ const __be32 ext_ip, struct net *net, const u16 zone);
+#endif
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
+static uint16_t find_appropriate_port6(struct net *net,
+ const struct nf_conntrack_zone *zone,
+ const uint16_t original_port, const union nf_inet_addr *ext_ip,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
+ struct nf_nat_range2 *range);
+
+#else
+ struct nf_nat_range *range);
+
+#endif
+
+#else
+static uint16_t find_appropriate_port6(struct net *net, const u16 zone,
+ const uint16_t original_port, const union nf_inet_addr *ext_ip,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
+ struct nf_nat_range2 *range);
+
+#else
+ struct nf_nat_range *range);
+
+#endif
+#endif
+
+#endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
+static uint16_t find_appropriate_port(struct net *net,
+ const struct nf_conntrack_zone *zone,
+ const uint16_t original_port, const __be32 ext_ip,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
+ struct nf_nat_range2 *range);
+#else
+ struct nf_nat_range *range);
+#endif
+
+#else
+static uint16_t find_appropriate_port(struct net *net, const u16 zone,
+ const uint16_t original_port, const __be32 ext_ip,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
+ struct nf_nat_range2 *range);
+#else
+ struct nf_nat_range *range);
+#endif
+#endif
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
+static void find_leastused_ip6(const struct nf_conntrack_zone *zone,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
+ struct nf_nat_range2 *range,
+#else
+ struct nf_nat_range *range,
+#endif
+ const union nf_inet_addr *src,
+ const union nf_inet_addr *dst, union nf_inet_addr *var_ipp);
+#else
+static void find_leastused_ip6(const u16 zone,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
+ struct nf_nat_range2 *range,
+#else
+ struct nf_nat_range *range,
+#endif
+ const union nf_inet_addr *src,
+ const union nf_inet_addr *dst, union nf_inet_addr *var_ipp);
+#endif
+#endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
+static __be32 find_leastused_ip(const struct nf_conntrack_zone *zone,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
+ struct nf_nat_range2 *range,
+#else
+ struct nf_nat_range *range,
+#endif
+ const __be32 src, const __be32 dst);
+#else
+static __be32 find_leastused_ip(const u16 zone,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
+ struct nf_nat_range2 *range,
+#else
+ struct nf_nat_range *range,
+#endif
+ const __be32 src, const __be32 dst);
+#endif
+
+/* forward declaration end */
+
+/* non-atomic part */
+
+static char tuple_tmp_string[512];
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+/* non-atomic: can only be called serially within lock zones. */
+static char *fullcone_nf_ct_stringify_tuple6(const struct nf_conntrack_tuple *t)
+{
+ snprintf(tuple_tmp_string, sizeof(tuple_tmp_string),
+ "[%pI6c]:%hu -> [%pI6c]:%hu", &t->src.u3.ip6,
+ be16_to_cpu(t->src.u.all), &t->dst.u3.ip6, be16_to_cpu(t->dst.u.all));
+ return tuple_tmp_string;
+}
+#endif
+/* non-atomic: can only be called serially within lock zones. */
+static char *nf_ct_stringify_tuple(const struct nf_conntrack_tuple *t)
+{
+ snprintf(tuple_tmp_string, sizeof(tuple_tmp_string),
+ "%pI4:%hu -> %pI4:%hu", &t->src.u3.ip,
+ be16_to_cpu(t->src.u.all), &t->dst.u3.ip, be16_to_cpu(t->dst.u.all));
+ return tuple_tmp_string;
+}
+
+/* non-atomic part end */
+
+void nf_nat_fullcone_dying_tuple_list_add(struct list_head *new_dying)
+{
+ spin_lock_bh(&dying_tuple_list_lock);
+ list_add(new_dying, &dying_tuple_list);
+ spin_unlock_bh(&dying_tuple_list_lock);
+}
+
+EXPORT_SYMBOL_GPL(nf_nat_fullcone_dying_tuple_list_add);
+
+void nf_nat_fullcone_handle_dying_tuples(void)
+{
+ struct list_head *iter, *tmp, *iter_2, *tmp_2;
+ struct tuple_list *item;
+ struct nf_conntrack_tuple *ct_tuple;
+ struct nat_mapping *mapping;
+ __be32 ip, ext_ip;
+ uint16_t port;
+ struct nat_mapping_original_tuple *original_tuple_item;
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+ struct nat_mapping6 *mapping6;
+ union nf_inet_addr *ip6, *ext_ip6;
+ spin_lock_bh(&fullconenat6_lock);
+#endif
+
+ spin_lock_bh(&fullconenat_lock);
+ spin_lock_bh(&dying_tuple_list_lock);
+
+ list_for_each_safe(iter, tmp, &dying_tuple_list) {
+ item = list_entry(iter, struct tuple_list, list);
+
+ /* we dont know the conntrack direction for now so we try in both ways. */
+ ct_tuple = &(item->tuple_original);
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+ if (ct_tuple->src.l3num == PF_INET6) {
+ ip6 = &(ct_tuple->src).u3;
+ port = be16_to_cpu((ct_tuple->src).u.udp.port);
+ ext_ip6 = &item->tuple_reply.dst.u3;
+ mapping6 = get_mapping6_by_int_src(ip6, port, ext_ip6);
+ if (mapping6 == NULL) {
+ ext_ip6 = &(ct_tuple->dst).u3;
+ ct_tuple = &(item->tuple_reply);
+ ip6 = &(ct_tuple->src).u3;
+ port = be16_to_cpu((ct_tuple->src).u.udp.port);
+ mapping6 = get_mapping6_by_int_src(ip6, port, ext_ip6);
+ if (mapping6 != NULL) {
+ pr_debug
+ ("nf_nat_fullcone_handle_dying_tuples(): INBOUND dying conntrack at ext port %d\n",
+ mapping6->port);
+ }
+ } else {
+ pr_debug
+ ("nf_nat_fullcone_handle_dying_tuples(): OUTBOUND dying conntrack at ext port %d\n",
+ mapping6->port);
+ }
+
+ if (mapping6 == NULL) {
+ goto next;
+ }
+
+ /* look for the corresponding out-dated tuple and free it */
+ list_for_each_safe(iter_2, tmp_2, &mapping6->original_tuple_list) {
+ original_tuple_item = list_entry(iter_2, struct
+ nat_mapping_original_tuple, node);
+
+ if (nf_ct_tuple_equal(&original_tuple_item->tuple, &(item->tuple_original))) {
+ pr_debug
+ ("nf_nat_fullcone_handle_dying_tuples(): tuple %s expired. free this tuple.\n",
+ fullcone_nf_ct_stringify_tuple6(&original_tuple_item->tuple));
+ list_del(&original_tuple_item->node);
+ kfree(original_tuple_item);
+ (mapping6->refer_count)--;
+ }
+ }
+
+ /* then kill the mapping if needed */
+ pr_debug
+ ("nf_nat_fullcone_handle_dying_tuples(): refer_count for mapping at ext_port %d is now %d\n",
+ mapping6->port, mapping6->refer_count);
+ if (mapping6->refer_count <= 0) {
+ pr_debug
+ ("nf_nat_fullcone_handle_dying_tuples(): kill expired mapping at ext port %d\n",
+ mapping6->port);
+ kill_mapping6(mapping6);
+ }
+ goto next;
+ }
+ if (unlikely(ct_tuple->src.l3num != PF_INET))
+#else
+ if (ct_tuple->src.l3num != PF_INET)
+#endif
+ goto next;
+
+ ip = (ct_tuple->src).u3.ip;
+ port = be16_to_cpu((ct_tuple->src).u.udp.port);
+ ext_ip = item->tuple_reply.dst.u3.ip;
+ mapping = get_mapping_by_int_src(ip, port, ext_ip);
+ if (mapping == NULL) {
+ ext_ip = (ct_tuple->dst).u3.ip;
+ ct_tuple = &(item->tuple_reply);
+ ip = (ct_tuple->src).u3.ip;
+ port = be16_to_cpu((ct_tuple->src).u.udp.port);
+ mapping = get_mapping_by_int_src(ip, port, ext_ip);
+ if (mapping != NULL) {
+ pr_debug
+ ("nf_nat_fullcone_handle_dying_tuples(): INBOUND dying conntrack at ext port %d\n",
+ mapping->port);
+ }
+ } else {
+ pr_debug
+ ("nf_nat_fullcone_handle_dying_tuples(): OUTBOUND dying conntrack at ext port %d\n",
+ mapping->port);
+ }
+
+ if (mapping == NULL) {
+ goto next;
+ }
+
+ /* look for the corresponding out-dated tuple and free it */
+ list_for_each_safe(iter_2, tmp_2, &mapping->original_tuple_list) {
+ original_tuple_item = list_entry(iter_2, struct nat_mapping_original_tuple, node);
+
+ if (nf_ct_tuple_equal(&original_tuple_item->tuple, &(item->tuple_original))) {
+ pr_debug
+ ("nf_nat_fullcone_handle_dying_tuples(): tuple %s expired. free this tuple.\n",
+ nf_ct_stringify_tuple(&original_tuple_item->tuple));
+ list_del(&original_tuple_item->node);
+ kfree(original_tuple_item);
+ (mapping->refer_count)--;
+ }
+ }
+
+ /* then kill the mapping if needed */
+ pr_debug
+ ("nf_nat_fullcone_handle_dying_tuples(): refer_count for mapping at ext_port %d is now %d\n",
+ mapping->port, mapping->refer_count);
+ if (mapping->refer_count <= 0) {
+ pr_debug
+ ("nf_nat_fullcone_handle_dying_tuples(): kill expired mapping at ext port %d\n",
+ mapping->port);
+ kill_mapping(mapping);
+ }
+
+next:
+ list_del(&item->list);
+ kfree(item);
+ }
+
+ spin_unlock_bh(&dying_tuple_list_lock);
+ spin_unlock_bh(&fullconenat_lock);
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+ spin_unlock_bh(&fullconenat6_lock);
+#endif
+}
+
+EXPORT_SYMBOL_GPL(nf_nat_fullcone_handle_dying_tuples);
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+static struct nat_mapping6 *allocate_mapping6(const union nf_inet_addr
+ *int_addr,
+ const uint16_t int_port,
+ const uint16_t port, const union nf_inet_addr *addr)
+{
+ struct nat_mapping6 *p_new;
+ u32 hash_src;
+
+ p_new = kmalloc(sizeof(struct nat_mapping6), GFP_ATOMIC);
+ if (p_new == NULL) {
+ pr_err("kmalloc() for allocate_mapping6 failed.\n");
+ return NULL;
+ }
+ p_new->addr = *addr;
+ p_new->port = port;
+ p_new->int_addr = *int_addr;
+ p_new->int_port = int_port;
+ p_new->refer_count = 0;
+ (p_new->original_tuple_list).next = &(p_new->original_tuple_list);
+ (p_new->original_tuple_list).prev = &(p_new->original_tuple_list);
+
+ hash_src = FULLCONE_HKEY(int_addr, sizeof(union nf_inet_addr), (u32) int_port, HASHTABLE_BUCKET_BITS);
+ //hash_src = jhash2((u32 *) int_addr->all, 4, (u32) int_port);
+
+ hash_add(mapping6_table_by_ext_port, &p_new->node_by_ext_port, port);
+ hash_add(mapping6_table_by_int_src, &p_new->node_by_int_src, hash_src);
+
+ pr_debug("new mapping allocated for [%pI6c]:%d ==> [%pI6c]:%d\n",
+ &p_new->int_addr, p_new->int_port, &p_new->addr, p_new->port);
+
+ return p_new;
+}
+#endif
+static struct nat_mapping *allocate_mapping(const __be32 int_addr,
+ const uint16_t int_port, const uint16_t port, const __be32 addr)
+{
+ struct nat_mapping *p_new;
+ u32 hash_src;
+
+ p_new = kmalloc(sizeof(struct nat_mapping), GFP_ATOMIC);
+ if (p_new == NULL) {
+ pr_err("kmalloc() for allocate_mapping failed.\n");
+ return NULL;
+ }
+ p_new->addr = addr;
+ p_new->port = port;
+ p_new->int_addr = int_addr;
+ p_new->int_port = int_port;
+ p_new->refer_count = 0;
+ (p_new->original_tuple_list).next = &(p_new->original_tuple_list);
+ (p_new->original_tuple_list).prev = &(p_new->original_tuple_list);
+
+ hash_src = FULLCONE_HKEY(&int_addr, sizeof(__be32), (u32) int_port, HASHTABLE_BUCKET_BITS);
+ //hash_src = HASH_2(int_addr, (u32) int_port);
+
+ hash_add(mapping_table_by_ext_port, &p_new->node_by_ext_port, port);
+ hash_add(mapping_table_by_int_src, &p_new->node_by_int_src, hash_src);
+
+ pr_debug("new mapping allocated for %pI4:%d ==> %pI4:%d\n",
+ &p_new->int_addr, p_new->int_port, &p_new->addr, p_new->port);
+
+ return p_new;
+}
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+static void add_original_tuple_to_mapping6(struct nat_mapping6 *mapping, const struct nf_conntrack_tuple
+ *original_tuple)
+{
+ struct nat_mapping_original_tuple *item = kmalloc(sizeof(struct nat_mapping_original_tuple), GFP_ATOMIC);
+ if (item == NULL) {
+ pr_err("kmalloc() for add_original_tuple_to_mapping6 failed.\n");
+ return;
+ }
+ memcpy(&item->tuple, original_tuple, sizeof(struct nf_conntrack_tuple));
+ list_add(&item->node, &mapping->original_tuple_list);
+ (mapping->refer_count)++;
+}
+#endif
+static void add_original_tuple_to_mapping(struct nat_mapping *mapping, const struct nf_conntrack_tuple
+ *original_tuple)
+{
+ struct nat_mapping_original_tuple *item = kmalloc(sizeof(struct nat_mapping_original_tuple), GFP_ATOMIC);
+ if (item == NULL) {
+ pr_err("kmalloc() for add_original_tuple_to_mapping failed.\n");
+ return;
+ }
+ memcpy(&item->tuple, original_tuple, sizeof(struct nf_conntrack_tuple));
+ list_add(&item->node, &mapping->original_tuple_list);
+ (mapping->refer_count)++;
+}
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+static struct nat_mapping6 *get_mapping6_by_int_src(const union nf_inet_addr
+ *src_ip, const uint16_t src_port, const union nf_inet_addr
+ *ext_ip)
+{
+ struct nat_mapping6 *p_current;
+ u32 hash_src = FULLCONE_HKEY(src_ip, sizeof(union nf_inet_addr), (u32) src_port, HASHTABLE_BUCKET_BITS);
+ //u32 hash_src = jhash2((u32 *) src_ip->all, 4, (u32) src_port);
+
+ hash_for_each_possible(mapping6_table_by_int_src, p_current, node_by_int_src, hash_src) {
+ if (nf_inet_addr_cmp(&p_current->int_addr, src_ip)
+ && p_current->int_port == src_port && nf_inet_addr_cmp(&p_current->addr, ext_ip)) {
+ return p_current;
+ }
+ }
+
+ return NULL;
+}
+#endif
+
+static struct nat_mapping *get_mapping_by_int_src(const __be32 src_ip, const uint16_t src_port, const __be32 ext_ip)
+{
+ struct nat_mapping *p_current;
+ u32 hash_src = FULLCONE_HKEY(&src_ip, sizeof(__be32), (u32) src_port, HASHTABLE_BUCKET_BITS);
+ //u32 hash_src = HASH_2(src_ip, (u32) src_port);
+
+ hash_for_each_possible(mapping_table_by_int_src, p_current, node_by_int_src, hash_src) {
+ if (p_current->int_addr == src_ip && p_current->int_port == src_port && p_current->addr == ext_ip) {
+ return p_current;
+ }
+ }
+
+ return NULL;
+}
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+static struct nat_mapping6 *get_mapping6_by_int_src_inrange(const union
+ nf_inet_addr
+ *src_ip, const uint16_t src_port, const union
+ nf_inet_addr
+ *min_ip, const union
+ nf_inet_addr
+ *max_ip)
+{
+ struct nat_mapping6 *p_current;
+
+ u32 hash_src = FULLCONE_HKEY(src_ip, sizeof(union nf_inet_addr), (u32) src_port, HASHTABLE_BUCKET_BITS);
+ //u32 hash_src = jhash2((u32 *) src_ip->all, 4, (u32) src_port);
+
+ hash_for_each_possible(mapping6_table_by_int_src, p_current, node_by_int_src, hash_src) {
+ if (nf_inet_addr_cmp(&p_current->int_addr, src_ip)
+ && p_current->int_port == src_port
+ && memcmp(&p_current->addr, min_ip,
+ sizeof(union nf_inet_addr)) >= 0
+ && memcmp(&p_current->addr, max_ip, sizeof(union nf_inet_addr)) <= 0) {
+ return p_current;
+ }
+ }
+
+ return NULL;
+}
+#endif
+static struct nat_mapping *get_mapping_by_int_src_inrange(const __be32 src_ip,
+ const uint16_t
+ src_port, const __be32 min_ip, const __be32 max_ip)
+{
+ struct nat_mapping *p_current;
+ u32 hash_src = FULLCONE_HKEY(&src_ip, sizeof(__be32), (u32) src_port, HASHTABLE_BUCKET_BITS);
+ //u32 hash_src = HASH_2(src_ip, (u32) src_port);
+
+ hash_for_each_possible(mapping_table_by_int_src, p_current, node_by_int_src, hash_src) {
+ if (p_current->int_addr == src_ip
+ && p_current->int_port == src_port
+ && memcmp(&p_current->addr, &min_ip, sizeof(__be32)) >= 0
+ && memcmp(&p_current->addr, &max_ip, sizeof(__be32)) <= 0) {
+ return p_current;
+ }
+ }
+
+ return NULL;
+}
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+static void kill_mapping6(struct nat_mapping6 *mapping)
+{
+ struct list_head *iter, *tmp;
+ struct nat_mapping_original_tuple *original_tuple_item;
+
+ if (mapping == NULL) {
+ return;
+ }
+
+ list_for_each_safe(iter, tmp, &mapping->original_tuple_list) {
+ original_tuple_item = list_entry(iter, struct nat_mapping_original_tuple, node);
+ list_del(&original_tuple_item->node);
+ kfree(original_tuple_item);
+ }
+
+ hash_del(&mapping->node_by_ext_port);
+ hash_del(&mapping->node_by_int_src);
+ kfree(mapping);
+}
+#endif
+static void kill_mapping(struct nat_mapping *mapping)
+{
+ struct list_head *iter, *tmp;
+ struct nat_mapping_original_tuple *original_tuple_item;
+
+ if (mapping == NULL) {
+ return;
+ }
+
+ list_for_each_safe(iter, tmp, &mapping->original_tuple_list) {
+ original_tuple_item = list_entry(iter, struct nat_mapping_original_tuple, node);
+ list_del(&original_tuple_item->node);
+ kfree(original_tuple_item);
+ }
+
+ hash_del(&mapping->node_by_ext_port);
+ hash_del(&mapping->node_by_int_src);
+ kfree(mapping);
+}
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+
+/* check if a mapping is valid.
+ * possibly delete and free an invalid mapping.
+ * the mapping should not be used anymore after check_mapping6() returns 0. */
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
+static int check_mapping6(struct nat_mapping6 *mapping, struct net *net, const struct nf_conntrack_zone *zone)
+{
+#else
+static int check_mapping6(struct nat_mapping6 *mapping, struct net *net, const u16 zone)
+{
+#endif
+ struct list_head *iter, *tmp;
+ struct nat_mapping_original_tuple *original_tuple_item;
+ struct nf_conntrack_tuple_hash *tuple_hash;
+ struct nf_conn *ct;
+
+ /* for dying/unconfirmed conntrack tuples, an IPCT_DESTROY event may NOT be fired.
+ * so we manually kill one of those tuples once we acquire one. */
+
+ list_for_each_safe(iter, tmp, &mapping->original_tuple_list) {
+ original_tuple_item = list_entry(iter, struct nat_mapping_original_tuple, node);
+
+ tuple_hash = nf_conntrack_find_get(net, zone, &original_tuple_item->tuple);
+
+ if (tuple_hash == NULL) {
+ pr_debug
+ ("check_mapping6(): tuple %s dying/unconfirmed. free this tuple.\n",
+ fullcone_nf_ct_stringify_tuple6(&original_tuple_item->tuple));
+
+ list_del(&original_tuple_item->node);
+ kfree(original_tuple_item);
+ (mapping->refer_count)--;
+ } else {
+ ct = nf_ct_tuplehash_to_ctrack(tuple_hash);
+ if (likely(ct != NULL))
+ nf_ct_put(ct);
+ }
+
+ }
+
+ /* kill the mapping if need */
+ pr_debug
+ ("check_mapping6() refer_count for mapping at ext_port %d is now %d\n",
+ mapping->port, mapping->refer_count);
+ if (mapping->refer_count <= 0) {
+ pr_debug("check_mapping6(): kill dying/unconfirmed mapping at ext port %d\n", mapping->port);
+ kill_mapping6(mapping);
+ return 0;
+ } else {
+ return 1;
+ }
+}
+
+#endif
+
+/* check if a mapping is valid.
+ * possibly delete and free an invalid mapping.
+ * the mapping should not be used anymore after check_mapping() returns 0. */
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
+static int check_mapping(struct nat_mapping *mapping, struct net *net, const struct nf_conntrack_zone *zone)
+{
+#else
+static int check_mapping(struct nat_mapping *mapping, struct net *net, const u16 zone)
+{
+#endif
+ struct list_head *iter, *tmp;
+ struct nat_mapping_original_tuple *original_tuple_item;
+ struct nf_conntrack_tuple_hash *tuple_hash;
+ struct nf_conn *ct;
+
+ /* for dying/unconfirmed conntrack tuples, an IPCT_DESTROY event may NOT be fired.
+ * so we manually kill one of those tuples once we acquire one. */
+
+ list_for_each_safe(iter, tmp, &mapping->original_tuple_list) {
+ original_tuple_item = list_entry(iter, struct nat_mapping_original_tuple, node);
+
+ tuple_hash = nf_conntrack_find_get(net, zone, &original_tuple_item->tuple);
+
+ if (tuple_hash == NULL) {
+ pr_debug
+ ("check_mapping(): tuple %s dying/unconfirmed. free this tuple.\n",
+ nf_ct_stringify_tuple(&original_tuple_item->tuple));
+
+ list_del(&original_tuple_item->node);
+ kfree(original_tuple_item);
+ (mapping->refer_count)--;
+ } else {
+ ct = nf_ct_tuplehash_to_ctrack(tuple_hash);
+ if (likely(ct != NULL))
+ nf_ct_put(ct);
+ }
+
+ }
+
+ /* kill the mapping if need */
+ pr_debug
+ ("check_mapping() refer_count for mapping at ext_port %d is now %d\n", mapping->port, mapping->refer_count);
+ if (mapping->refer_count <= 0) {
+ pr_debug("check_mapping(): kill dying/unconfirmed mapping at ext port %d\n", mapping->port);
+ kill_mapping(mapping);
+ return 0;
+ } else {
+ return 1;
+ }
+}
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
+static struct nat_mapping6 *get_mapping6_by_ext_port(const uint16_t port, const union nf_inet_addr
+ *ext_ip, struct net *net, const struct
+ nf_conntrack_zone *zone)
+{
+#else
+static struct nat_mapping6 *get_mapping6_by_ext_port(const uint16_t port, const union nf_inet_addr
+ *ext_ip, struct net *net, const u16 zone)
+{
+#endif
+ struct nat_mapping6 *p_current;
+ struct hlist_node *tmp;
+
+ hash_for_each_possible_safe(mapping6_table_by_ext_port, p_current, tmp, node_by_ext_port, port) {
+ if (p_current->port == port && check_mapping6(p_current, net, zone)
+ && nf_inet_addr_cmp(&p_current->addr, ext_ip)) {
+ return p_current;
+ }
+ }
+
+ return NULL;
+}
+#endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
+static struct nat_mapping *get_mapping_by_ext_port(const uint16_t port, const __be32 ext_ip, struct net *net, const struct
+ nf_conntrack_zone *zone)
+{
+#else
+static struct nat_mapping *get_mapping_by_ext_port(const uint16_t port,
+ const __be32 ext_ip, struct net *net, const u16 zone)
+{
+#endif
+ struct nat_mapping *p_current;
+ struct hlist_node *tmp;
+
+ hash_for_each_possible_safe(mapping_table_by_ext_port, p_current, tmp, node_by_ext_port, port) {
+ if (p_current->port == port && check_mapping(p_current, net, zone)
+ && p_current->addr == ext_ip) {
+ return p_current;
+ }
+ }
+
+ return NULL;
+}
+
+#if IS_ENABLED(CONFIG_NF_NAT_IPV6) || (IS_ENABLED(CONFIG_IPV6) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0))
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
+static uint16_t find_appropriate_port6(struct net *net,
+ const struct nf_conntrack_zone *zone,
+ const uint16_t original_port, const union nf_inet_addr *ext_ip,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
+ struct nf_nat_range2 *range)
+#else
+ struct nf_nat_range *range)
+#endif
+#else
+static uint16_t find_appropriate_port6(struct net *net, const u16 zone,
+ const uint16_t original_port, const union nf_inet_addr *ext_ip,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
+ struct nf_nat_range2 *range)
+#else
+ struct nf_nat_range *range)
+#endif
+#endif
+{
+ uint16_t min, start, selected, range_size, i;
+ struct nat_mapping6 *mapping = NULL;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
+ // nf_nat_range2 specific
+ memset(&range->base_proto, 0, sizeof(range->base_proto));
+#endif
+ if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
+ min = be16_to_cpu((range->min_proto).udp.port);
+ range_size = be16_to_cpu((range->max_proto).udp.port) - min + 1;
+ } else {
+ /* minimum port is 1024. same behavior as default linux NAT. */
+ min = 1024;
+ range_size = 65535 - min + 1;
+ }
+
+ if ((range->flags & NF_NAT_RANGE_PROTO_RANDOM)
+ || (range->flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY)) {
+ /* for now we do the same thing for both --random and --random-fully */
+
+ /* select a random starting point */
+ start = (uint16_t) (prandom_u32() % (u32) range_size);
+ } else {
+
+ if ((original_port >= min && original_port <= min + range_size - 1)
+ || !(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
+ /* 1. try to preserve the port if it's available */
+ mapping = get_mapping6_by_ext_port(original_port, ext_ip, net, zone);
+ if (mapping == NULL) {
+ return original_port;
+ }
+ }
+
+ /* otherwise, we start from zero */
+ start = 0;
+ }
+
+ for (i = 0; i < range_size; i++) {
+ /* 2. try to find an available port */
+ selected = min + ((start + i) % range_size);
+ mapping = get_mapping6_by_ext_port(selected, ext_ip, net, zone);
+ if (mapping == NULL) {
+ return selected;
+ }
+ }
+
+ /* 3. at least we tried. override a previous mapping. */
+ selected = min + start;
+ mapping = get_mapping6_by_ext_port(selected, ext_ip, net, zone);
+ kill_mapping6(mapping);
+
+ return selected;
+}
+#endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
+static uint16_t find_appropriate_port(struct net *net,
+ const struct nf_conntrack_zone *zone,
+ const uint16_t original_port, const __be32 ext_ip,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
+ struct nf_nat_range2 *range)
+#else
+ struct nf_nat_range *range)
+#endif
+#else
+static uint16_t find_appropriate_port(struct net *net, const u16 zone,
+ const uint16_t original_port, const __be32 ext_ip,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
+ struct nf_nat_range2 *range)
+#else
+ struct nf_nat_range *range)
+#endif
+#endif
+{
+ uint16_t min, start, selected, range_size, i;
+ struct nat_mapping *mapping = NULL;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
+ // nf_nat_range2 specific
+ memset(&range->base_proto, 0, sizeof(range->base_proto));
+#endif
+ if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
+ min = be16_to_cpu((range->min_proto).udp.port);
+ range_size = be16_to_cpu((range->max_proto).udp.port) - min + 1;
+ } else {
+ /* minimum port is 1024. same behavior as default linux NAT. */