@@ -240,50 +240,10 @@ int wg_forward_proxy(struct xdp_md *xdp_ctx) {
240240 __u16 src_port = ctx .src_port ;
241241 __u16 dst_port = ctx .dst_port ;
242242 __u16 wg_port = CONFIG (wg_port );
243- if (dst_port != wg_port && src_port != wg_port )
244- return XDP_PASS ;
245243
246244 __u8 is_to_wg = (dst_port == wg_port ) ? 1 : 0 ;
247- __u8 is_from_wg = (src_port == wg_port ) ? 1 : 0 ;
248-
249245 __u32 pkt_len = (void * )(long )xdp_ctx -> data_end - (void * )(long )xdp_ctx -> data ;
250246
251- if (likely (is_from_wg )) {
252- struct connection_key original_conn = { 0 };
253- if (restore_nat_connection (& ctx , & original_conn ) < 0 ) {
254- DEBUG_PRINTK ("Failed to restore NAT connection for FROM WG packet, passing "
255- "through" );
256- return XDP_PASS ;
257- }
258-
259- struct connection_value * conn_value = bpf_map_lookup_elem (& connection_map , & original_conn );
260- if (!conn_value ) {
261- DEBUG_PRINTK ("No connection value found for FROM WG packet" );
262- return XDP_PASS ;
263- }
264-
265- __u8 backend_index = conn_value -> backend_index ;
266-
267- // FROM_WG path: backend->proxy (upstream rx), proxy->client (downstream tx)
268- update_metrics (backend_index , METRIC_UPSTREAM , pkt_len , 1 , METRIC_REASON_FORWARDED );
269-
270- if (instr_deobfuscate_xdp (& ctx ) < 0 ) {
271- DEBUG_PRINTK ("Deobfuscation failed, dropping packet" );
272- update_metrics (backend_index , METRIC_UPSTREAM , pkt_len , 1 , METRIC_REASON_DROPPED );
273- return XDP_DROP ;
274- }
275-
276- __u32 tx_pkt_len = (void * )(long )xdp_ctx -> data_end - (void * )(long )xdp_ctx -> data ;
277-
278- __u32 dst_addr = bpf_ntohl (ctx .ip -> daddr );
279- __u32 client_ip = bpf_ntohl (original_conn .client_ip );
280- __u16 server_port = original_conn .server_port ;
281- __u16 client_port = original_conn .client_port ;
282-
283- update_metrics (backend_index , METRIC_DOWNSTREAM , tx_pkt_len , 0 , METRIC_REASON_FORWARDED );
284- return forward_packet (& ctx , dst_addr , server_port , client_ip , client_port );
285- }
286-
287247 if (unlikely (is_to_wg )) {
288248 struct backend_entry backend = { 0 };
289249 if (create_nat_connection (& ctx , & backend ) < 0 ) {
@@ -326,6 +286,44 @@ int wg_forward_proxy(struct xdp_md *xdp_ctx) {
326286 return forward_packet (& ctx , proxy_ip , conn_value -> nat_port , server_ip , target_port );
327287 }
328288
289+ __u8 is_from_wg = bpf_map_lookup_elem (& backend_port_set , & src_port ) != NULL ? 1 : 0 ;
290+
291+ if (likely (is_from_wg )) {
292+ struct connection_key original_conn = { 0 };
293+ if (restore_nat_connection (& ctx , & original_conn ) < 0 ) {
294+ DEBUG_PRINTK ("Failed to restore NAT connection for FROM WG packet, passing "
295+ "through" );
296+ return XDP_PASS ;
297+ }
298+
299+ struct connection_value * conn_value = bpf_map_lookup_elem (& connection_map , & original_conn );
300+ if (!conn_value ) {
301+ DEBUG_PRINTK ("No connection value found for FROM WG packet" );
302+ return XDP_PASS ;
303+ }
304+
305+ __u8 backend_index = conn_value -> backend_index ;
306+
307+ // FROM_WG path: backend->proxy (upstream rx), proxy->client (downstream tx)
308+ update_metrics (backend_index , METRIC_UPSTREAM , pkt_len , 1 , METRIC_REASON_FORWARDED );
309+
310+ if (instr_deobfuscate_xdp (& ctx ) < 0 ) {
311+ DEBUG_PRINTK ("Deobfuscation failed, dropping packet" );
312+ update_metrics (backend_index , METRIC_UPSTREAM , pkt_len , 1 , METRIC_REASON_DROPPED );
313+ return XDP_DROP ;
314+ }
315+
316+ __u32 tx_pkt_len = (void * )(long )xdp_ctx -> data_end - (void * )(long )xdp_ctx -> data ;
317+
318+ __u32 dst_addr = bpf_ntohl (ctx .ip -> daddr );
319+ __u32 client_ip = bpf_ntohl (original_conn .client_ip );
320+ __u16 server_port = original_conn .server_port ;
321+ __u16 client_port = original_conn .client_port ;
322+
323+ update_metrics (backend_index , METRIC_DOWNSTREAM , tx_pkt_len , 0 , METRIC_REASON_FORWARDED );
324+ return forward_packet (& ctx , dst_addr , server_port , client_ip , client_port );
325+ }
326+
329327 DEBUG_PRINTK ("No matching handler for WG packet, passing through" );
330328 return XDP_PASS ;
331329}
0 commit comments