Added missing IF index check

This commit is contained in:
Mathias Hall-Andersen 2017-08-17 12:58:18 +02:00
parent 24f9394f64
commit 04640eb629
3 changed files with 37 additions and 22 deletions

View file

@ -5,9 +5,9 @@ import (
) )
func updateUDPConn(device *Device) error { func updateUDPConn(device *Device) error {
var err error
netc := &device.net netc := &device.net
netc.mutex.Lock() netc.mutex.Lock()
defer netc.mutex.Unlock()
// close existing connection // close existing connection
@ -18,15 +18,23 @@ func updateUDPConn(device *Device) error {
// open new connection // open new connection
if device.tun.isUp.Get() { if device.tun.isUp.Get() {
// listen on new address
conn, err := net.ListenUDP("udp", netc.addr) conn, err := net.ListenUDP("udp", netc.addr)
if err == nil { if err != nil {
netc.conn = conn return err
signalSend(device.signal.newUDPConn)
} }
// retrieve port (may have been chosen by kernel)
addr := conn.LocalAddr()
netc.conn = conn
netc.addr, _ = net.ResolveUDPAddr(addr.Network(), addr.String())
signalSend(device.signal.newUDPConn)
} }
netc.mutex.Unlock() return nil
return err
} }
func closeUDPConn(device *Device) { func closeUDPConn(device *Device) {

View file

@ -196,15 +196,19 @@ func (device *Device) RoutineTUNEventReader() {
} }
if event&TUNEventUp != 0 { if event&TUNEventUp != 0 {
device.tun.isUp.Set(true) if !device.tun.isUp.Get() {
updateUDPConn(device) device.tun.isUp.Set(true)
logInfo.Println("Interface set up") updateUDPConn(device)
logInfo.Println("Interface set up")
}
} }
if event&TUNEventDown != 0 { if event&TUNEventDown != 0 {
device.tun.isUp.Set(false) if device.tun.isUp.Get() {
closeUDPConn(device) device.tun.isUp.Set(false)
logInfo.Println("Interface set down") closeUDPConn(device)
logInfo.Println("Interface set down")
}
} }
} }
} }

View file

@ -50,10 +50,10 @@ const (
type NativeTun struct { type NativeTun struct {
fd *os.File fd *os.File
index int index int32 // if index
name string name string // name of interface
errors chan error // async error handling errors chan error // async error handling
events chan TUNEvent // events chan TUNEvent // device related events
} }
func (tun *NativeTun) RoutineNetlinkListener() { func (tun *NativeTun) RoutineNetlinkListener() {
@ -86,6 +86,11 @@ func (tun *NativeTun) RoutineNetlinkListener() {
case unix.RTM_NEWLINK: case unix.RTM_NEWLINK:
info := *(*unix.IfInfomsg)(unsafe.Pointer(&remain[unix.SizeofNlMsghdr])) info := *(*unix.IfInfomsg)(unsafe.Pointer(&remain[unix.SizeofNlMsghdr]))
if info.Index != tun.index {
// not our interface
continue
}
if info.Flags&unix.IFF_RUNNING != 0 { if info.Flags&unix.IFF_RUNNING != 0 {
tun.events <- TUNEventUp tun.events <- TUNEventUp
} }
@ -112,12 +117,12 @@ func (tun *NativeTun) Name() string {
return tun.name return tun.name
} }
func toInt32(val []byte) int { func toInt32(val []byte) int32 {
n := binary.LittleEndian.Uint32(val[:4]) n := binary.LittleEndian.Uint32(val[:4])
if n >= (1 << 31) { if n >= (1 << 31) {
return int(n-(1<<31)) - (1 << 31) return -int32(^n) - 1
} }
return int(n) return int32(n)
} }
func getDummySock() (int, error) { func getDummySock() (int, error) {
@ -128,7 +133,7 @@ func getDummySock() (int, error) {
) )
} }
func getIFIndex(name string) (int, error) { func getIFIndex(name string) (int32, error) {
fd, err := getDummySock() fd, err := getDummySock()
if err != nil { if err != nil {
return 0, err return 0, err
@ -288,7 +293,7 @@ func CreateTUN(name string) (TUNDevice, error) {
errors: make(chan error, 5), errors: make(chan error, 5),
} }
// fetch IF index // start event listener
device.index, err = getIFIndex(device.name) device.index, err = getIFIndex(device.name)
if err != nil { if err != nil {
@ -299,7 +304,5 @@ func CreateTUN(name string) (TUNDevice, error) {
// set default MTU // set default MTU
fmt.Println(device)
return device, device.setMTU(DefaultMTU) return device, device.setMTU(DefaultMTU)
} }