Search code examples
pythonmtutun

Packets larger than MTU arriving on TUN interface


I'm using pytun to setup a TUN and forward packets that arrive on it to another machine using UDP. What's puzzling me is that even though I've configured the TUN to have MTU of 141 bytes, I'm reading packets of size 145 on it. See the code below:

from pytun import TunTapDevice
tun = TunTapDevice(name="vpn")
tun.addr = '10.8.0.1'
tun.dstaddr = '10.8.0.2'
tun.netmask = '255.255.255.0'
tun.mtu = 141
tun.up()
assert len(tun.read(1000)) <= tun.mtu # <-- fails for some packets

I've verified the actual MTU of the interface using ifconfig.

Am I missing something?


Solution

  • If you do not add the IFF_NO_PI flag you will get a 4 byte header on your frame.

    From the kernel Documentation/networking/tuntap.txt

    3.2 Frame format:

    If flag IFF_NO_PI is not set each frame format is:
    Flags [2 bytes]
    Proto [2 bytes]
    Raw protocol(IP, IPv6, etc) frame.

    Presumably, you should be able to get what you want with

    from pytun import TunTapDevice, IFF_TUN, IFF_NO_PI
    tun = TunTapDevice(name="vpn",flags=(IFF_TUN | IFF_NO_PI))