2018-05-03 15:04:00 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
2017-06-04 21:48:15 +02:00
|
|
|
package main
|
|
|
|
|
2017-08-22 14:57:32 +02:00
|
|
|
import (
|
2017-11-14 18:26:28 +01:00
|
|
|
"os"
|
2017-08-22 14:57:32 +02:00
|
|
|
"sync/atomic"
|
|
|
|
)
|
2017-07-15 16:27:59 +02:00
|
|
|
|
|
|
|
const DefaultMTU = 1420
|
|
|
|
|
2018-02-11 18:55:30 +01:00
|
|
|
type TUNEvent int
|
|
|
|
|
2017-08-07 15:25:04 +02:00
|
|
|
const (
|
|
|
|
TUNEventUp = 1 << iota
|
|
|
|
TUNEventDown
|
|
|
|
TUNEventMTUUpdate
|
|
|
|
)
|
|
|
|
|
2017-06-28 23:45:45 +02:00
|
|
|
type TUNDevice interface {
|
2017-12-04 21:39:06 +01:00
|
|
|
File() *os.File // returns the file descriptor of the device
|
|
|
|
Read([]byte, int) (int, error) // read a packet from the device (without any additional headers)
|
|
|
|
Write([]byte, int) (int, error) // writes a packet to the device (without any additional headers)
|
|
|
|
MTU() (int, error) // returns the MTU of the device
|
2018-04-18 16:39:14 +02:00
|
|
|
Name() (string, error) // fetches and returns the current name
|
2018-02-11 18:55:30 +01:00
|
|
|
Events() chan TUNEvent // returns a constant channel of events related to the device
|
2017-12-04 21:39:06 +01:00
|
|
|
Close() error // stops the device and closes the event channel
|
2017-06-04 21:48:15 +02:00
|
|
|
}
|
2017-08-22 14:57:32 +02:00
|
|
|
|
|
|
|
func (device *Device) RoutineTUNEventReader() {
|
2018-02-11 23:26:54 +01:00
|
|
|
setUp := false
|
2017-08-22 14:57:32 +02:00
|
|
|
logInfo := device.log.Info
|
|
|
|
logError := device.log.Error
|
|
|
|
|
2018-05-16 22:20:15 +02:00
|
|
|
device.state.starting.Done()
|
|
|
|
|
2017-08-22 14:57:32 +02:00
|
|
|
for event := range device.tun.device.Events() {
|
2018-02-11 18:55:30 +01:00
|
|
|
if event&TUNEventMTUUpdate != 0 {
|
2017-08-22 14:57:32 +02:00
|
|
|
mtu, err := device.tun.device.MTU()
|
|
|
|
old := atomic.LoadInt32(&device.tun.mtu)
|
|
|
|
if err != nil {
|
|
|
|
logError.Println("Failed to load updated MTU of device:", err)
|
|
|
|
} else if int(old) != mtu {
|
|
|
|
if mtu+MessageTransportSize > MaxMessageSize {
|
|
|
|
logInfo.Println("MTU updated:", mtu, "(too large)")
|
|
|
|
} else {
|
|
|
|
logInfo.Println("MTU updated:", mtu)
|
|
|
|
}
|
2017-08-22 17:22:45 +02:00
|
|
|
atomic.StoreInt32(&device.tun.mtu, int32(mtu))
|
2017-08-22 14:57:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 23:26:54 +01:00
|
|
|
if event&TUNEventUp != 0 && !setUp {
|
2017-12-29 17:42:09 +01:00
|
|
|
logInfo.Println("Interface set up")
|
2018-02-11 23:26:54 +01:00
|
|
|
setUp = true
|
2017-12-29 17:42:09 +01:00
|
|
|
device.Up()
|
2017-08-22 14:57:32 +02:00
|
|
|
}
|
|
|
|
|
2018-02-11 23:26:54 +01:00
|
|
|
if event&TUNEventDown != 0 && setUp {
|
2017-12-29 17:42:09 +01:00
|
|
|
logInfo.Println("Interface set down")
|
2018-02-11 23:26:54 +01:00
|
|
|
setUp = false
|
2018-01-13 09:00:37 +01:00
|
|
|
device.Down()
|
2017-08-22 14:57:32 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-16 22:20:15 +02:00
|
|
|
|
|
|
|
device.state.stopping.Done()
|
2017-08-22 14:57:32 +02:00
|
|
|
}
|