Skip to content

Commit 84eab9e

Browse files
committed
fix(local-tun): tun2 already handles IP packet information
- Removed all excessive IP packet information handling code - Removed all route setting code
1 parent d8191ec commit 84eab9e

File tree

16 files changed

+55
-527
lines changed

16 files changed

+55
-527
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "shadowsocks-rust"
3-
version = "1.19.1"
3+
version = "1.19.2"
44
authors = ["Shadowsocks Contributors"]
55
description = "shadowsocks is a fast tunnel proxy that helps you bypass firewalls."
66
repository = "https://github.com/shadowsocks/shadowsocks-rust"
@@ -248,7 +248,7 @@ jemallocator = { version = "0.5", optional = true }
248248
snmalloc-rs = { version = "0.3", optional = true }
249249
rpmalloc = { version = "0.2", optional = true }
250250

251-
shadowsocks-service = { version = "1.19.1", path = "./crates/shadowsocks-service" }
251+
shadowsocks-service = { version = "1.19.2", path = "./crates/shadowsocks-service" }
252252

253253
windows-service = { version = "0.7", optional = true }
254254

crates/shadowsocks-service/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "shadowsocks-service"
3-
version = "1.19.1"
3+
version = "1.19.2"
44
authors = ["Shadowsocks Contributors"]
55
description = "shadowsocks is a fast tunnel proxy that helps you bypass firewalls."
66
repository = "https://github.com/shadowsocks/shadowsocks-rust"

crates/shadowsocks-service/src/local/tun/mod.rs

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ use ipnet::IpNet;
1515
use log::{debug, error, info, trace, warn};
1616
use shadowsocks::config::Mode;
1717
use smoltcp::wire::{IpProtocol, TcpPacket, UdpPacket};
18-
use tokio::{io::AsyncReadExt, sync::mpsc, time};
18+
use tokio::{
19+
io::{AsyncReadExt, AsyncWriteExt},
20+
sync::mpsc,
21+
time,
22+
};
1923

2024
cfg_if! {
2125
if #[cfg(any(target_os = "ios",
2226
target_os = "macos",
2327
target_os = "linux",
2428
target_os = "android",
25-
target_os = "windows"))] {
29+
target_os = "windows",
30+
target_os = "freebsd"))] {
2631
use tun2::{
2732
create_as_async, AsyncDevice, Configuration as TunConfiguration, AbstractDevice, Error as TunError, Layer,
2833
};
@@ -36,15 +41,9 @@ cfg_if! {
3641

3742
use crate::local::{context::ServiceContext, loadbalancing::PingBalancer};
3843

39-
use self::{
40-
ip_packet::IpPacket,
41-
sys::{write_packet_with_pi, IFF_PI_PREFIX_LEN},
42-
tcp::TcpTun,
43-
udp::UdpTun,
44-
};
44+
use self::{ip_packet::IpPacket, tcp::TcpTun, udp::UdpTun};
4545

4646
mod ip_packet;
47-
mod sys;
4847
mod tcp;
4948
mod udp;
5049
mod virt_device;
@@ -159,10 +158,6 @@ pub struct Tun {
159158
impl Tun {
160159
/// Start serving
161160
pub async fn run(mut self) -> io::Result<()> {
162-
if let Ok(mtu) = self.device.as_ref().mtu() {
163-
assert!(mtu > 0 && mtu as usize > IFF_PI_PREFIX_LEN);
164-
}
165-
166161
info!(
167162
"shadowsocks tun device {}, mode {}",
168163
self.device
@@ -204,15 +199,9 @@ impl Tun {
204199
netmask
205200
);
206201

207-
// Set default route
208-
// XXX: tun2 already set it by default.
209-
// if let Err(err) = sys::set_route_configuration(self.device.as_mut()).await {
210-
// warn!("[TUN] tun device set route failed, error: {}", err);
211-
// }
212-
213202
let address_broadcast = address_net.broadcast();
214203

215-
let mut packet_buffer = vec![0u8; 65536 + IFF_PI_PREFIX_LEN].into_boxed_slice();
204+
let mut packet_buffer = vec![0u8; 65536].into_boxed_slice();
216205
let mut udp_cleanup_timer = time::interval(self.udp_cleanup_interval);
217206

218207
loop {
@@ -221,15 +210,7 @@ impl Tun {
221210
n = self.device.read(&mut packet_buffer) => {
222211
let n = n?;
223212

224-
if n <= IFF_PI_PREFIX_LEN {
225-
error!(
226-
"[TUN] packet too short, packet: {:?}",
227-
ByteStr::new(&packet_buffer[..n])
228-
);
229-
continue;
230-
}
231-
232-
let packet = &mut packet_buffer[IFF_PI_PREFIX_LEN..n];
213+
let packet = &mut packet_buffer[..n];
233214
trace!("[TUN] received IP packet {:?}", ByteStr::new(packet));
234215

235216
if let Err(err) = self.handle_tun_frame(&address_broadcast, packet).await {
@@ -239,10 +220,17 @@ impl Tun {
239220

240221
// UDP channel sent back
241222
packet = self.udp.recv_packet() => {
242-
if let Err(err) = write_packet_with_pi(&mut self.device, &packet).await {
243-
error!("[TUN] failed to set packet information, error: {}, {:?}", err, ByteStr::new(&packet));
244-
} else {
245-
trace!("[TUN] sent IP packet (UDP) {:?}", ByteStr::new(&packet));
223+
match self.device.write(&packet).await {
224+
Ok(n) => {
225+
if n < packet.len() {
226+
warn!("[TUN] sent IP packet (UDP), but truncated. sent {} < {}, {:?}", n, packet.len(), ByteStr::new(&packet));
227+
} else {
228+
trace!("[TUN] sent IP packet (UDP) {:?}", ByteStr::new(&packet));
229+
}
230+
}
231+
Err(err) => {
232+
error!("[TUN] failed to set packet information, error: {}, {:?}", err, ByteStr::new(&packet));
233+
}
246234
}
247235
}
248236

@@ -259,10 +247,17 @@ impl Tun {
259247

260248
// TCP channel sent back
261249
packet = self.tcp.recv_packet() => {
262-
if let Err(err) = write_packet_with_pi(&mut self.device, &packet).await {
263-
error!("[TUN] failed to set packet information, error: {}, {:?}", err, ByteStr::new(&packet));
264-
} else {
265-
trace!("[TUN] sent IP packet (TCP) {:?}", ByteStr::new(&packet));
250+
match self.device.write(&packet).await {
251+
Ok(n) => {
252+
if n < packet.len() {
253+
warn!("[TUN] sent IP packet (TCP), but truncated. sent {} < {}, {:?}", n, packet.len(), ByteStr::new(&packet));
254+
} else {
255+
trace!("[TUN] sent IP packet (TCP) {:?}", ByteStr::new(&packet));
256+
}
257+
}
258+
Err(err) => {
259+
error!("[TUN] failed to set packet information, error: {}, {:?}", err, ByteStr::new(&packet));
260+
}
266261
}
267262
}
268263
}
@@ -278,20 +273,20 @@ impl Tun {
278273
}
279274
};
280275

276+
trace!("[TUN] {:?}", packet);
277+
281278
let src_ip_addr = packet.src_addr();
282279
let dst_ip_addr = packet.dst_addr();
283-
let src_non_unicast = match src_ip_addr {
284-
IpAddr::V4(v4) => {
285-
v4.is_broadcast() || v4.is_multicast() || v4.is_unspecified() || v4 == *device_broadcast_addr
286-
}
287-
IpAddr::V6(v6) => v6.is_multicast() || v6.is_unspecified(),
288-
};
289-
let dst_non_unicast = match dst_ip_addr {
290-
IpAddr::V4(v4) => {
291-
v4.is_broadcast() || v4.is_multicast() || v4.is_unspecified() || v4 == *device_broadcast_addr
292-
}
293-
IpAddr::V6(v6) => v6.is_multicast() || v6.is_unspecified(),
294-
};
280+
let src_non_unicast = src_ip_addr == *device_broadcast_addr
281+
|| match src_ip_addr {
282+
IpAddr::V4(v4) => v4.is_broadcast() || v4.is_multicast() || v4.is_unspecified(),
283+
IpAddr::V6(v6) => v6.is_multicast() || v6.is_unspecified(),
284+
};
285+
let dst_non_unicast = dst_ip_addr == *device_broadcast_addr
286+
|| match dst_ip_addr {
287+
IpAddr::V4(v4) => v4.is_broadcast() || v4.is_multicast() || v4.is_unspecified(),
288+
IpAddr::V6(v6) => v6.is_multicast() || v6.is_unspecified(),
289+
};
295290

296291
if src_non_unicast || dst_non_unicast {
297292
trace!(

crates/shadowsocks-service/src/local/tun/sys/mod.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

crates/shadowsocks-service/src/local/tun/sys/others.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.

crates/shadowsocks-service/src/local/tun/sys/unix/android.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)