@@ -61,7 +61,10 @@ int nat_v4_egress(struct __sk_buff *skb) {
6161#define BPF_LOG_TOPIC "nat_v4_egress_v3 <<<"
6262 struct packet_offset_info pkg_offset = {0 };
6363 struct inet4_pair ip_pair = {0 };
64- struct nat4_lookup_result_v3 lookup = {0 };
64+ struct nat_mapping_value_v4_v3 * nat_egress_value = NULL ;
65+ struct nat_mapping_value_v4_v3 * nat_ingress_value = NULL ;
66+ struct nat4_port_queue_value_v3 alloc_item = {0 };
67+ bool created = false;
6568 int ret = 0 ;
6669
6770 ret = scan_packet (skb , current_l3_offset , & pkg_offset );
@@ -78,29 +81,34 @@ int nat_v4_egress(struct __sk_buff *skb) {
7881 bool is_icmpx_error = is_icmp_error_pkt (& pkg_offset );
7982 bool allow_create_mapping = !is_icmpx_error && pkt_allow_initiating_ct (pkg_offset .pkt_type );
8083
81- ret = nat4_v3_egress_lookup_or_new_mapping (skb , pkg_offset .l4_protocol , allow_create_mapping ,
82- & ip_pair , & lookup );
83- if (ret != TC_ACT_OK || !lookup .egress ) {
84+ ret = nat4_v3_egress_lookup_or_new_mapping_v4 (
85+ skb , pkg_offset .l4_protocol , allow_create_mapping , & ip_pair , & nat_egress_value ,
86+ & nat_ingress_value , & alloc_item , & created );
87+ if (ret != TC_ACT_OK || !nat_egress_value || !nat_ingress_value ) {
8488 return TC_ACT_SHOT ;
8589 }
8690
87- if (!lookup .is_static && lookup .egress -> is_allow_reuse == 0 &&
88- pkg_offset .l4_protocol != IPPROTO_ICMP ) {
89- if (ip_pair .dst_addr .addr != lookup .egress -> trigger_addr ||
90- ip_pair .dst_port != lookup .egress -> trigger_port ) {
91+ bool is_dynamic = nat_egress_value -> is_static == 0 ;
92+ bool is_ancestor = ip_pair .dst_addr .addr == nat_egress_value -> trigger_addr &&
93+ ip_pair .dst_port == nat_egress_value -> trigger_port ;
94+
95+ if (is_dynamic && nat_egress_value -> is_allow_reuse == 0 && pkg_offset .l4_protocol != IPPROTO_ICMP ) {
96+ if (!is_ancestor ) {
9197 return TC_ACT_SHOT ;
9298 }
9399 }
94100
95- if (!lookup .is_static && ip_pair .dst_addr .addr == lookup .egress -> trigger_addr &&
96- ip_pair .dst_port == lookup .egress -> trigger_port ) {
101+ if (is_dynamic && is_ancestor ) {
97102 u8 allow = get_flow_allow_reuse_port (skb -> mark ) ? 1 : 0 ;
98- lookup . egress -> is_allow_reuse = allow ;
99- if ( lookup . ingress ) lookup . ingress -> is_allow_reuse = allow ;
103+ nat_egress_value -> is_allow_reuse = allow ;
104+ nat_ingress_value -> is_allow_reuse = allow ;
100105 }
101106
102- struct inet4_addr nat_addr = {0 };
103- if (lookup .is_static ) {
107+ struct inet4_addr nat_addr = {
108+ .addr = nat_egress_value -> addr ,
109+ };
110+ __be16 nat_port = nat_egress_value -> port ;
111+ if (!is_dynamic ) {
104112 struct wan_ip_info_key wan_search_key = {
105113 .ifindex = skb -> ifindex ,
106114 .l3_protocol = LANDSCAPE_IPV4_TYPE ,
@@ -109,37 +117,33 @@ int nat_v4_egress(struct __sk_buff *skb) {
109117 bpf_map_lookup_elem (& wan_ip_binding , & wan_search_key );
110118 if (!wan_ip_info ) return TC_ACT_SHOT ;
111119 nat_addr .addr = wan_ip_info -> addr .ip ;
112- } else {
113- nat_addr .addr = lookup .egress -> addr ;
114120 }
115121
116122 struct inet4_pair server_nat_pair = {
117123 .src_addr = ip_pair .dst_addr ,
118124 .src_port = ip_pair .dst_port ,
119125 .dst_addr = nat_addr ,
120- .dst_port = lookup . egress -> port ,
126+ .dst_port = nat_port ,
121127 };
122128 if (pkg_offset .l4_protocol == IPPROTO_ICMP ) {
123- server_nat_pair .src_port = lookup . egress -> port ;
129+ server_nat_pair .src_port = nat_port ;
124130 }
125131
126132 struct nat_timer_value_v4_v3 * ct_value = NULL ;
127- u16 generation = lookup .state ? lookup .state -> generation : 0 ;
128133 ret = nat4_v3_lookup_or_new_ct (skb , pkg_offset .l4_protocol , allow_create_mapping ,
129134 & server_nat_pair , & ip_pair .src_addr , ip_pair .src_port ,
130- NAT_MAPPING_EGRESS , generation ,
131- lookup .created || lookup .is_static , lookup .state , & ct_value );
135+ NAT_MAPPING_EGRESS , nat_ingress_value , & ct_value );
132136 if (ret == TIMER_NOT_FOUND || ret == TIMER_ERROR ) {
133- if (lookup . created && ! lookup . is_static ) {
134- nat4_v3_delete_mapping_and_state ( pkg_offset . l4_protocol , nat_addr . addr ,
135- lookup . egress -> port , ip_pair . src_addr . addr ,
136- ip_pair .src_port );
137- (void )nat4_v3_queue_push (pkg_offset .l4_protocol , & lookup . alloc_item );
137+ if (created && is_dynamic &&
138+ nat_ingress_value -> state_ref == nat4_v3_state_make ( NAT4_V3_STATE_ACTIVE , 0 )) {
139+ nat4_v3_delete_mapping_pair ( pkg_offset . l4_protocol , nat_addr . addr , nat_port ,
140+ ip_pair . src_addr . addr , ip_pair .src_port );
141+ (void )nat4_v3_queue_push (pkg_offset .l4_protocol , & alloc_item );
138142 }
139143 return TC_ACT_SHOT ;
140144 }
141145
142- if (!is_icmpx_error || ct_value != NULL ) {
146+ if (!is_icmpx_error ) {
143147 ct_state_transition (pkg_offset .pkt_type , NAT_MAPPING_EGRESS , nat4_v3_timer_base (ct_value ));
144148 nat_metric_accumulate (skb , false, nat4_v3_timer_base (ct_value ));
145149 }
@@ -148,7 +152,7 @@ int nat_v4_egress(struct __sk_buff *skb) {
148152 .from_addr = ip_pair .src_addr ,
149153 .from_port = ip_pair .src_port ,
150154 .to_addr = nat_addr ,
151- .to_port = lookup . egress -> port ,
155+ .to_port = nat_port ,
152156 };
153157
154158 ret = modify_headers_v4 (skb , is_icmpx_error , pkg_offset .l4_protocol , current_l3_offset ,
@@ -163,8 +167,7 @@ int nat_v4_ingress(struct __sk_buff *skb) {
163167#define BPF_LOG_TOPIC "nat_v4_ingress_v3 >>>"
164168 struct packet_offset_info pkg_offset = {0 };
165169 struct inet4_pair ip_pair = {0 };
166- struct nat_mapping_value_v4 * nat_ingress_value = NULL ;
167- struct nat4_mapping_state_v3 * state = NULL ;
170+ struct nat_mapping_value_v4_v3 * nat_ingress_value = NULL ;
168171 int ret = 0 ;
169172
170173 ret = scan_packet (skb , current_l3_offset , & pkg_offset );
@@ -180,32 +183,36 @@ int nat_v4_ingress(struct __sk_buff *skb) {
180183
181184 bool is_icmpx_error = is_icmp_error_pkt (& pkg_offset );
182185
183- ret = nat4_v3_ingress_lookup_mapping ( skb , pkg_offset .l4_protocol , & ip_pair , & nat_ingress_value ,
184- & state );
186+ ret = nat4_v3_ingress_lookup_or_new_mapping4 ( pkg_offset .l4_protocol , & ip_pair ,
187+ & nat_ingress_value );
185188 if (ret != TC_ACT_OK || !nat_ingress_value ) {
186189 return TC_ACT_SHOT ;
187190 }
188191
189- if (!nat_ingress_value -> is_static && nat_ingress_value -> is_allow_reuse == 0 &&
192+ bool is_static = nat_ingress_value -> is_static != 0 ;
193+
194+ if (!is_static && nat_ingress_value -> is_allow_reuse == 0 &&
190195 pkg_offset .l4_protocol != IPPROTO_ICMP ) {
191196 if (ip_pair .src_addr .addr != nat_ingress_value -> trigger_addr ||
192197 ip_pair .src_port != nat_ingress_value -> trigger_port ) {
193198 return TC_ACT_SHOT ;
194199 }
195200 }
196201
197- if (nat_ingress_value -> is_static ) {
202+ if (is_static ) {
198203 u32 mark = skb -> mark ;
199204 barrier_var (mark );
200205 skb -> mark = replace_cache_mask (mark , INGRESS_STATIC_MARK );
201206 }
202207
203208 struct inet4_addr lan_ip = {0 };
204- if (nat_ingress_value -> is_static && nat_ingress_value -> addr == 0 ) {
209+ __be16 lan_port = 0 ;
210+ if (is_static && nat_ingress_value -> addr == 0 ) {
205211 lan_ip .addr = ip_pair .dst_addr .addr ;
206212 } else {
207213 lan_ip .addr = nat_ingress_value -> addr ;
208214 }
215+ lan_port = nat_ingress_value -> port ;
209216
210217 struct inet4_pair server_nat_pair = {
211218 .src_addr = ip_pair .src_addr ,
@@ -214,21 +221,23 @@ int nat_v4_ingress(struct __sk_buff *skb) {
214221 .dst_port = ip_pair .dst_port ,
215222 };
216223
217- bool do_new_ct = nat_ingress_value -> is_static
224+ u64 ingress_state_ref = nat_ingress_value -> state_ref ;
225+ bool do_new_ct = is_static
218226 ? (!is_icmpx_error && pkt_allow_initiating_ct (pkg_offset .pkt_type ))
219- : (nat_ingress_value -> is_allow_reuse && !is_icmpx_error &&
227+ : (nat_ingress_value -> is_allow_reuse &&
228+ nat4_v3_state_get (ingress_state_ref ) == NAT4_V3_STATE_ACTIVE &&
229+ nat4_v3_ref_get (ingress_state_ref ) > 0 && !is_icmpx_error &&
220230 pkt_allow_initiating_ct (pkg_offset .pkt_type ));
221231
222232 struct nat_timer_value_v4_v3 * ct_value = NULL ;
223- u16 generation = state ? state -> generation : 0 ;
224233 ret = nat4_v3_lookup_or_new_ct (skb , pkg_offset .l4_protocol , do_new_ct , & server_nat_pair ,
225- & lan_ip , nat_ingress_value -> port , NAT_MAPPING_INGRESS ,
226- generation , nat_ingress_value -> is_static , state , & ct_value );
234+ & lan_ip , lan_port , NAT_MAPPING_INGRESS , nat_ingress_value ,
235+ & ct_value );
227236 if (ret == TIMER_NOT_FOUND || ret == TIMER_ERROR ) {
228237 return TC_ACT_SHOT ;
229238 }
230239
231- if (!is_icmpx_error || ct_value != NULL ) {
240+ if (!is_icmpx_error ) {
232241 ct_state_transition (pkg_offset .pkt_type , NAT_MAPPING_INGRESS , nat4_v3_timer_base (ct_value ));
233242 nat_metric_accumulate (skb , true, nat4_v3_timer_base (ct_value ));
234243 }
@@ -237,7 +246,7 @@ int nat_v4_ingress(struct __sk_buff *skb) {
237246 .from_addr = ip_pair .dst_addr ,
238247 .from_port = ip_pair .dst_port ,
239248 .to_addr = lan_ip ,
240- .to_port = nat_ingress_value -> port ,
249+ .to_port = lan_port ,
241250 };
242251
243252 ret = modify_headers_v4 (skb , is_icmpx_error , pkg_offset .l4_protocol , current_l3_offset ,
0 commit comments