Fix MTU of RawSocket and TapInterface.

Linux's MTU is the IP MTU, while smoltcp's is the Ethernet MTU.
Therefore we have to add the ethernet header size to it.
This commit is contained in:
Dario Nieuwenhuis 2020-12-25 23:57:54 +01:00
parent a40a4ac5af
commit 273f4d60b6
2 changed files with 10 additions and 3 deletions

View File

@ -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 {
@ -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<()> {

View File

@ -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 {
@ -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> {