Skip to content

Commit 651101d

Browse files
committed
fix: calculate mss only during handshake
1 parent 146bee5 commit 651101d

1 file changed

Lines changed: 20 additions & 17 deletions

File tree

src/stream/tcp.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,16 @@ async fn tcp_main_logic_loop(
452452
&up_packet_sender,
453453
network_tuple,
454454
&tcb,
455-
config.options.as_ref(),
455+
config.options.as_ref().as_deref().map(|o| {
456+
(
457+
o,
458+
if network_tuple.src.is_ipv4() && network_tuple.dst.is_ipv4() {
459+
tcb.get_mtu() - 20 - TCP_HEADER_LEN
460+
} else {
461+
tcb.get_mtu() - 40 - TCP_HEADER_LEN
462+
},
463+
)
464+
}),
456465
ACK | SYN,
457466
None,
458467
None,
@@ -862,7 +871,7 @@ pub(crate) fn write_packet_to_device(
862871
up_packet_sender: &PacketSender,
863872
tuple: NetworkTuple,
864873
tcb: &Tcb,
865-
options: Option<&Vec<TcpOptions>>,
874+
options: Option<(&Vec<TcpOptions>, u16)>,
866875
flags: u8,
867876
seq: Option<SeqNum>,
868877
payload: Option<Vec<u8>>,
@@ -881,7 +890,6 @@ pub(crate) fn write_packet_to_device(
881890
seq,
882891
ack,
883892
window_size,
884-
tcb.get_mtu(),
885893
payload.unwrap_or_default(),
886894
options,
887895
)?;
@@ -900,9 +908,8 @@ pub(crate) fn create_raw_packet(
900908
seq: u32,
901909
ack: u32,
902910
win: u16,
903-
mtu: u16,
904911
mut payload: Vec<u8>,
905-
options: Option<&Vec<TcpOptions>>,
912+
options: Option<(&Vec<TcpOptions>, u16)>,
906913
) -> std::io::Result<NetworkPacket> {
907914
let mut tcp_header = etherparse::TcpHeader::new(src_addr.port(), dst_addr.port(), seq, win);
908915
tcp_header.acknowledgment_number = ack;
@@ -911,23 +918,19 @@ pub(crate) fn create_raw_packet(
911918
tcp_header.rst = flags & RST != 0;
912919
tcp_header.fin = flags & FIN != 0;
913920
tcp_header.psh = flags & PSH != 0;
914-
let mut actual_mss = if src_addr.is_ipv6() || dst_addr.is_ipv6() {
915-
mtu - 40 - TCP_HEADER_LEN
916-
} else {
917-
mtu - 20 - TCP_HEADER_LEN
918-
};
919-
let mut tcp_options = Vec::new();
920-
if let Some(opts) = options {
921+
922+
if let Some((opts, mut mss)) = options {
923+
let mut tcp_options = Vec::new();
921924
for opt in opts {
922925
match opt {
923-
TcpOptions::MaximumSegmentSize(mss) => actual_mss = *mss,
926+
TcpOptions::MaximumSegmentSize(fixed_mss) => mss = *fixed_mss,
924927
}
925928
}
929+
tcp_options.push(etherparse::TcpOptionElement::MaximumSegmentSize(mss));
930+
tcp_header
931+
.set_options(&tcp_options)
932+
.map_err(|e| std::io::Error::new(InvalidInput, e))?;
926933
}
927-
tcp_options.push(etherparse::TcpOptionElement::MaximumSegmentSize(actual_mss));
928-
tcp_header
929-
.set_options(&tcp_options)
930-
.map_err(|e| std::io::Error::new(InvalidInput, e))?;
931934

932935
let ip_header = match (src_addr.ip(), dst_addr.ip()) {
933936
(std::net::IpAddr::V4(src), std::net::IpAddr::V4(dst)) => {

0 commit comments

Comments
 (0)