Skip to content

Commit

Permalink
UDP IPv6 outbound socket should disable IPV6_V6ONLY
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Apr 18, 2022
1 parent 66dc357 commit 9276cf8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
4 changes: 2 additions & 2 deletions crates/shadowsocks/src/net/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn socket_bind_dual_stack_inner(socket: &Socket, addr: &SocketAddr, ipv6_only: b
socket.bind(&saddr)?;
} else {
if let Err(err) = socket.set_only_v6(false) {
warn!("failed to set IPV6_V6ONLY: false for listener, error: {}", err);
warn!("failed to set IPV6_V6ONLY: false for socket, error: {}", err);

// This is not a fatal error, just warn and skip
}
Expand All @@ -115,7 +115,7 @@ fn socket_bind_dual_stack_inner(socket: &Socket, addr: &SocketAddr, ipv6_only: b
);

if let Err(err) = socket.set_only_v6(true) {
warn!("failed to set IPV6_V6ONLY: true for listener, error: {}", err);
warn!("failed to set IPV6_V6ONLY: true for socket, error: {}", err);

// This is not a fatal error, just warn and skip
}
Expand Down
15 changes: 13 additions & 2 deletions crates/shadowsocks/src/net/sys/unix/bsd/freebsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ use std::{

use log::{error, warn};
use pin_project::pin_project;
use socket2::{Domain, Protocol, Socket, Type};
use tokio::{
io::{AsyncRead, AsyncWrite, ReadBuf},
net::{TcpSocket, TcpStream as TokioTcpStream, UdpSocket},
};
use tokio_tfo::TfoStream;

use crate::net::{
sys::{set_common_sockopt_after_connect, set_common_sockopt_for_connect},
sys::{set_common_sockopt_after_connect, set_common_sockopt_for_connect, socket_bind_dual_stack},
AddrFamily,
ConnectOpts,
};
Expand Down Expand Up @@ -223,7 +224,17 @@ pub async fn create_outbound_udp_socket(af: AddrFamily, config: &ConnectOpts) ->
(AddrFamily::Ipv6, ..) => SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0),
};

let socket = UdpSocket::bind(bind_addr).await?;
let socket = if af != AddrFamily::Ipv6 {
UdpSocket::bind(bind_addr).await?
} else {
let socket = Socket::new(Domain::for_address(bind_addr), Type::DGRAM, Some(Protocol::UDP))?;
socket_bind_dual_stack(&socket, &bind_addr, false)?;

// UdpSocket::from_std requires socket to be non-blocked
socket.set_nonblocking(true)?;
UdpSocket::from_std(socket.into())?
};

if let Err(err) = set_disable_ip_fragmentation(af, &socket) {
warn!("failed to disable IP fragmentation, error: {}", err);
}
Expand Down
15 changes: 13 additions & 2 deletions crates/shadowsocks/src/net/sys/unix/bsd/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ use std::{

use log::{error, warn};
use pin_project::pin_project;
use socket2::{Domain, Protocol, Socket, Type};
use tokio::{
io::{AsyncRead, AsyncWrite, ReadBuf},
net::{TcpSocket, TcpStream as TokioTcpStream, UdpSocket},
};
use tokio_tfo::TfoStream;

use crate::net::{
sys::{set_common_sockopt_after_connect, set_common_sockopt_for_connect},
sys::{set_common_sockopt_after_connect, set_common_sockopt_for_connect, socket_bind_dual_stack},
AddrFamily,
ConnectOpts,
};
Expand Down Expand Up @@ -250,7 +251,17 @@ pub async fn create_outbound_udp_socket(af: AddrFamily, config: &ConnectOpts) ->
(AddrFamily::Ipv6, ..) => SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0),
};

let socket = UdpSocket::bind(bind_addr).await?;
let socket = if af != AddrFamily::Ipv6 {
UdpSocket::bind(bind_addr).await?
} else {
let socket = Socket::new(Domain::for_address(bind_addr), Type::DGRAM, Some(Protocol::UDP))?;
socket_bind_dual_stack(&socket, &bind_addr, false)?;

// UdpSocket::from_std requires socket to be non-blocked
socket.set_nonblocking(true)?;
UdpSocket::from_std(socket.into())?
};

if let Err(err) = set_disable_ip_fragmentation(af, &socket) {
warn!("failed to disable IP fragmentation, error: {}", err);
}
Expand Down
15 changes: 13 additions & 2 deletions crates/shadowsocks/src/net/sys/unix/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ use std::{
use cfg_if::cfg_if;
use log::{error, warn};
use pin_project::pin_project;
use socket2::{Domain, Protocol, Socket, Type};
use tokio::{
io::{AsyncRead, AsyncWrite, ReadBuf},
net::{TcpSocket, TcpStream as TokioTcpStream, UdpSocket},
};
use tokio_tfo::TfoStream;

use crate::net::{
sys::{set_common_sockopt_after_connect, set_common_sockopt_for_connect},
sys::{set_common_sockopt_after_connect, set_common_sockopt_for_connect, socket_bind_dual_stack},
AddrFamily,
ConnectOpts,
};
Expand Down Expand Up @@ -245,7 +246,17 @@ pub async fn create_outbound_udp_socket(af: AddrFamily, config: &ConnectOpts) ->
(AddrFamily::Ipv6, ..) => SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0),
};

let socket = UdpSocket::bind(bind_addr).await?;
let socket = if af != AddrFamily::Ipv6 {
UdpSocket::bind(bind_addr).await?
} else {
let socket = Socket::new(Domain::for_address(bind_addr), Type::DGRAM, Some(Protocol::UDP))?;
socket_bind_dual_stack(&socket, &bind_addr, false)?;

// UdpSocket::from_std requires socket to be non-blocked
socket.set_nonblocking(true)?;
UdpSocket::from_std(socket.into())?
};

if let Err(err) = set_disable_ip_fragmentation(af, &socket) {
warn!("failed to disable IP fragmentation, error: {}", err);
}
Expand Down
12 changes: 11 additions & 1 deletion crates/shadowsocks/src/net/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,17 @@ pub async fn create_outbound_udp_socket(af: AddrFamily, opts: &ConnectOpts) -> i
(AddrFamily::Ipv6, ..) => SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0),
};

let socket = UdpSocket::bind(bind_addr).await?;
let socket = if af != AddrFamily::Ipv6 {
UdpSocket::bind(addr).await?
} else {
let socket = Socket::new(Domain::for_address(bind_addr), Type::DGRAM, Some(Protocol::UDP))?;
socket_bind_dual_stack(&socket, &bind_addr, false)?;

// UdpSocket::from_std requires socket to be non-blocked
socket.set_nonblocking(true)?;
UdpSocket::from_std(socket.into())?
};

if let Err(err) = set_disable_ip_fragmentation(af, &socket) {
warn!("failed to disable IP fragmentation, error: {}", err);
}
Expand Down

0 comments on commit 9276cf8

Please sign in to comment.