Skip to content

Commit 02d7060

Browse files
committed
add IPV6_TRANSPARENT for tproxy
1 parent 15ade51 commit 02d7060

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/socket.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,46 @@ impl Socket {
17361736
}
17371737
}
17381738

1739+
/// Get the value of the `IPV6_TRANSPARENT` option on this socket.
1740+
///
1741+
/// For more information about this option, see [`set_ip_transparent_v6`].
1742+
///
1743+
/// [`set_ip_transparent_v6`]: Socket::set_ip_transparent_v6
1744+
#[cfg(all(feature = "all", target_os = "linux"))]
1745+
pub fn ip_transparent_v6(&self) -> io::Result<bool> {
1746+
unsafe {
1747+
getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IPV6, libc::IPV6_TRANSPARENT)
1748+
.map(|transparent| transparent != 0)
1749+
}
1750+
}
1751+
1752+
/// Set the value of the `IPV6_TRANSPARENT` option on this socket.
1753+
///
1754+
/// Setting this boolean option enables transparent proxying
1755+
/// on this socket. This socket option allows the calling
1756+
/// application to bind to a nonlocal IP address and operate
1757+
/// both as a client and a server with the foreign address as
1758+
/// the local endpoint. NOTE: this requires that routing be
1759+
/// set up in a way that packets going to the foreign address
1760+
/// are routed through the TProxy box (i.e., the system
1761+
/// hosting the application that employs the IPV6_TRANSPARENT
1762+
/// socket option). Enabling this socket option requires
1763+
/// superuser privileges (the `CAP_NET_ADMIN` capability).
1764+
///
1765+
/// TProxy redirection with the iptables TPROXY target also
1766+
/// requires that this option be set on the redirected socket.
1767+
#[cfg(all(feature = "all", target_os = "linux"))]
1768+
pub fn set_ip_transparent_v6(&self, transparent: bool) -> io::Result<()> {
1769+
unsafe {
1770+
setsockopt(
1771+
self.as_raw(),
1772+
sys::IPPROTO_IPV6,
1773+
libc::IPV6_TRANSPARENT,
1774+
transparent as c_int,
1775+
)
1776+
}
1777+
}
1778+
17391779
/// Join a multicast group using `IPV6_ADD_MEMBERSHIP` option on this socket.
17401780
///
17411781
/// Some OSs use `IPV6_JOIN_GROUP` for this option.

tests/socket.rs

100644100755
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,22 @@ macro_rules! test {
13291329
test!(__ Domain::IPV6, $get_fn, $set_fn($arg), $arg);
13301330
}
13311331
};
1332+
// Only test using a IPv6 socket and with attributes.
1333+
($( #[ $attr: meta ] )* IPv6 $get_fn: ident, $set_fn: ident ( $arg: expr ) ) => {
1334+
#[test]
1335+
$( #[$attr] )*
1336+
fn $get_fn() {
1337+
test!(__ Domain::IPV6, $get_fn, $set_fn($arg), $arg);
1338+
}
1339+
};
1340+
// Only test using a IPv4 socket with attributes.
1341+
($( #[ $attr: meta ] )* IPv4 $get_fn: ident, $set_fn: ident ( $arg: expr ) ) => {
1342+
#[test]
1343+
$( #[$attr] )*
1344+
fn $get_fn() {
1345+
test!(__ Domain::IPV4, $get_fn, $set_fn($arg), $arg);
1346+
}
1347+
};
13321348

13331349
// Internal to this macro.
13341350
(__ $ty: expr, $get_fn: ident, $set_fn: ident ( $arg: expr ), $expected: expr ) => {
@@ -1392,7 +1408,7 @@ test!(
13921408
#[cfg(all(feature = "all", target_os = "linux"))]
13931409
test!(
13941410
#[ignore = "setting `IP_TRANSPARENT` requires the `CAP_NET_ADMIN` capability (works when running as root)"]
1395-
ip_transparent_v4,
1411+
IPv4 ip_transparent_v4,
13961412
set_ip_transparent_v4(true)
13971413
);
13981414
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
@@ -1505,6 +1521,13 @@ test!(IPv6 tclass_v6, set_tclass_v6(96));
15051521
)))]
15061522
test!(IPv6 recv_tclass_v6, set_recv_tclass_v6(true));
15071523

1524+
#[cfg(all(feature = "all", target_os = "linux"))]
1525+
test!(
1526+
#[ignore = "setting `IPV6_TRANSPARENT` requires the `CAP_NET_ADMIN` capability (works when running as root)"]
1527+
IPv6 ip_transparent_v6,
1528+
set_ip_transparent_v6(true)
1529+
);
1530+
15081531
#[cfg(all(
15091532
feature = "all",
15101533
not(any(

0 commit comments

Comments
 (0)