Skip to content

Commit 2a6451f

Browse files
committed
UDP socket should set different IP_TRANSPARENT and IPV6_TRANSPARENT
1 parent a4d684c commit 2a6451f

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/relay/tcprelay/redir/sys/unix/linux.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn create_redir_listener(addr: &SocketAddr) -> io::Result<TcpListener> {
101101
let socket = Socket::new(domain, Type::stream(), Some(Protocol::tcp()))?;
102102

103103
// For Linux 2.4+ TPROXY
104-
// Sockets have to set IP_TRANSPARENT for retrieving original destination by getsockname()
104+
// Sockets have to set IP_TRANSPARENT, IPV6_TRANSPARENT for retrieving original destination by getsockname()
105105
unsafe {
106106
let fd = socket.as_raw_fd();
107107

src/relay/udprelay/redir/sys/unix/linux.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,23 @@ fn set_socket_before_bind(addr: &SocketAddr, socket: &Socket) -> io::Result<()>
103103

104104
let enable: libc::c_int = 1;
105105
unsafe {
106-
// 1. Set IP_TRANSPARENT to allow binding to non-local addresses
107-
let ret = libc::setsockopt(
108-
fd,
109-
libc::SOL_IP,
110-
libc::IP_TRANSPARENT,
111-
&enable as *const _ as *const _,
112-
mem::size_of_val(&enable) as libc::socklen_t,
113-
);
106+
// 1. Set IP_TRANSPARENT, IPV6_TRANSPARENT to allow binding to non-local addresses
107+
let ret = match *addr {
108+
SocketAddr::V4(..) => libc::setsockopt(
109+
fd,
110+
libc::SOL_IP,
111+
libc::IP_TRANSPARENT,
112+
&enable as *const _ as *const _,
113+
mem::size_of_val(&enable) as libc::socklen_t,
114+
),
115+
SocketAddr::V6(..) => libc::setsockopt(
116+
fd,
117+
libc::SOL_IPV6,
118+
libc::IPV6_TRANSPARENT,
119+
&enable as *const _ as *const _,
120+
mem::size_of_val(&enable) as libc::socklen_t,
121+
),
122+
};
114123
if ret != 0 {
115124
return Err(Error::last_os_error());
116125
}

0 commit comments

Comments
 (0)