Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MTU of RawSocket and TapInterface. #393

Merged
merged 1 commit into from
Dec 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/phy/sys/raw_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{mem, io};
use std::os::unix::io::{RawFd, AsRawFd};
use libc;
use super::*;
use crate::wire::EthernetFrame;

#[derive(Debug)]
pub struct RawSocketDesc {
Expand Down Expand Up @@ -31,7 +32,10 @@ impl RawSocketDesc {
}

pub fn interface_mtu(&mut self) -> io::Result<usize> {
ifreq_ioctl(self.lower, &mut self.ifreq, imp::SIOCGIFMTU).map(|mtu| mtu as usize)
// SIOCGIFMTU returns the IP MTU (typically 1500 bytes.)
// smoltcp counts the entire Ethernet packet in the MTU, so add the Ethernet header size to it.
let ip_mtu = ifreq_ioctl(self.lower, &mut self.ifreq, imp::SIOCGIFMTU).map(|mtu| mtu as usize)?;
Ok(ip_mtu + EthernetFrame::<&[u8]>::header_len())
}

pub fn bind_interface(&mut self) -> io::Result<()> {
Expand Down
7 changes: 5 additions & 2 deletions src/phy/sys/tap_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::io;
use std::os::unix::io::{RawFd, AsRawFd};
use libc;
use super::*;
use crate::wire::EthernetFrame;

#[derive(Debug)]
pub struct TapInterfaceDesc {
Expand Down Expand Up @@ -42,11 +43,13 @@ impl TapInterfaceDesc {
lower
};

let mtu = ifreq_ioctl(lower, &mut self.ifreq, imp::SIOCGIFMTU).map(|mtu| mtu as usize);
let ip_mtu = ifreq_ioctl(lower, &mut self.ifreq, imp::SIOCGIFMTU).map(|mtu| mtu as usize);

unsafe { libc::close(lower); }

mtu
// SIOCGIFMTU returns the IP MTU (typically 1500 bytes.)
// smoltcp counts the entire Ethernet packet in the MTU, so add the Ethernet header size to it.
Ok(ip_mtu? + EthernetFrame::<&[u8]>::header_len())
}

pub fn recv(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
Expand Down