From 2a6451f0feef2abbcf961be38edf13abdada11b5 Mon Sep 17 00:00:00 2001 From: zonyitoo Date: Tue, 3 Nov 2020 01:17:47 +0800 Subject: [PATCH] UDP socket should set different IP_TRANSPARENT and IPV6_TRANSPARENT --- src/relay/tcprelay/redir/sys/unix/linux.rs | 2 +- src/relay/udprelay/redir/sys/unix/linux.rs | 25 +++++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/relay/tcprelay/redir/sys/unix/linux.rs b/src/relay/tcprelay/redir/sys/unix/linux.rs index 777aadb5..efbd1194 100644 --- a/src/relay/tcprelay/redir/sys/unix/linux.rs +++ b/src/relay/tcprelay/redir/sys/unix/linux.rs @@ -101,7 +101,7 @@ fn create_redir_listener(addr: &SocketAddr) -> io::Result { let socket = Socket::new(domain, Type::stream(), Some(Protocol::tcp()))?; // For Linux 2.4+ TPROXY - // Sockets have to set IP_TRANSPARENT for retrieving original destination by getsockname() + // Sockets have to set IP_TRANSPARENT, IPV6_TRANSPARENT for retrieving original destination by getsockname() unsafe { let fd = socket.as_raw_fd(); diff --git a/src/relay/udprelay/redir/sys/unix/linux.rs b/src/relay/udprelay/redir/sys/unix/linux.rs index ec12e473..1cf92210 100644 --- a/src/relay/udprelay/redir/sys/unix/linux.rs +++ b/src/relay/udprelay/redir/sys/unix/linux.rs @@ -103,14 +103,23 @@ fn set_socket_before_bind(addr: &SocketAddr, socket: &Socket) -> io::Result<()> let enable: libc::c_int = 1; unsafe { - // 1. Set IP_TRANSPARENT to allow binding to non-local addresses - let ret = libc::setsockopt( - fd, - libc::SOL_IP, - libc::IP_TRANSPARENT, - &enable as *const _ as *const _, - mem::size_of_val(&enable) as libc::socklen_t, - ); + // 1. Set IP_TRANSPARENT, IPV6_TRANSPARENT to allow binding to non-local addresses + let ret = match *addr { + SocketAddr::V4(..) => libc::setsockopt( + fd, + libc::SOL_IP, + libc::IP_TRANSPARENT, + &enable as *const _ as *const _, + mem::size_of_val(&enable) as libc::socklen_t, + ), + SocketAddr::V6(..) => libc::setsockopt( + fd, + libc::SOL_IPV6, + libc::IPV6_TRANSPARENT, + &enable as *const _ as *const _, + mem::size_of_val(&enable) as libc::socklen_t, + ), + }; if ret != 0 { return Err(Error::last_os_error()); }