diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2017-07-15 16:27:59 +0200 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2017-07-15 16:27:59 +0200 |
commit | dd4da93749fd9a8a231942a6b75ad137cc308e02 (patch) | |
tree | 187c4d0257e4216ea332cd09d19f6b50041b0791 /src/tun_linux.go | |
parent | b21c82e32d955e15d846aed87535b6d728ab1d0b (diff) |
Added padding
Added plaintext padding and fixed default interface MTU
Diffstat (limited to 'src/tun_linux.go')
-rw-r--r-- | src/tun_linux.go | 48 |
1 files changed, 41 insertions, 7 deletions
diff --git a/src/tun_linux.go b/src/tun_linux.go index 63c3886..d0e9761 100644 --- a/src/tun_linux.go +++ b/src/tun_linux.go @@ -23,6 +23,39 @@ func (tun *NativeTun) Name() string { return tun.name } +func (tun *NativeTun) setMTU(n int) error { + + // open datagram socket + + fd, err := syscall.Socket( + syscall.AF_INET, + syscall.SOCK_DGRAM, + 0, + ) + + if err != nil { + return err + } + + // do ioctl call + + var ifr [64]byte + copy(ifr[:], tun.name) + binary.LittleEndian.PutUint32(ifr[16:20], uint32(n)) + _, _, errno := syscall.Syscall( + syscall.SYS_IOCTL, + uintptr(fd), + uintptr(syscall.SIOCSIFMTU), + uintptr(unsafe.Pointer(&ifr[0])), + ) + + if errno != 0 { + return errors.New("Failed to set MTU of TUN device") + } + + return nil +} + func (tun *NativeTun) MTU() (int, error) { // open datagram socket @@ -40,9 +73,7 @@ func (tun *NativeTun) MTU() (int, error) { // do ioctl call var ifr [64]byte - var flags uint16 copy(ifr[:], tun.name) - binary.LittleEndian.PutUint16(ifr[16:], flags) _, _, errno := syscall.Syscall( syscall.SYS_IOCTL, uintptr(fd), @@ -79,7 +110,7 @@ func CreateTUN(name string) (TUNDevice, error) { return nil, err } - // prepare ifreq struct + // create new device var ifr [64]byte var flags uint16 = syscall.IFF_TUN | syscall.IFF_NO_PI @@ -90,8 +121,6 @@ func CreateTUN(name string) (TUNDevice, error) { copy(ifr[:], nameBytes) binary.LittleEndian.PutUint16(ifr[16:], flags) - // create new device - _, _, errno := syscall.Syscall( syscall.SYS_IOCTL, uintptr(fd.Fd()), @@ -106,8 +135,13 @@ func CreateTUN(name string) (TUNDevice, error) { newName := string(ifr[:]) newName = newName[:strings.Index(newName, "\000")] - return &NativeTun{ + device := &NativeTun{ fd: fd, name: newName, - }, nil + } + + // set default MTU + + err = device.setMTU(DefaultMTU) + return device, err } |