From 471f7ff08e897a6b4f2779e10d28aeb0a5b9e0e4 Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Wed, 29 Nov 2017 18:46:31 +0100 Subject: [PATCH] Added cross namespace TUN status detection --- src/conn.go | 4 ++-- src/receive.go | 4 ++-- src/tests/netns.sh | 1 + src/timers.go | 10 ++++++++-- src/tun_linux.go | 24 ++++++++++++++++++++++-- 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/conn.go b/src/conn.go index b17ac70..6d292d3 100644 --- a/src/conn.go +++ b/src/conn.go @@ -114,8 +114,8 @@ func updateBind(device *Device) error { // decrease waitgroup to 0 - go device.RoutineReceiveIncomming(ipv4.Version, netc.bind) - go device.RoutineReceiveIncomming(ipv6.Version, netc.bind) + go device.RoutineReceiveIncoming(ipv4.Version, netc.bind) + go device.RoutineReceiveIncoming(ipv6.Version, netc.bind) device.log.Debug.Println("UDP bind has been updated") } diff --git a/src/receive.go b/src/receive.go index 27fdb8a..0b0efbf 100644 --- a/src/receive.go +++ b/src/receive.go @@ -93,10 +93,10 @@ func (device *Device) addToHandshakeQueue( } } -func (device *Device) RoutineReceiveIncomming(IP int, bind Bind) { +func (device *Device) RoutineReceiveIncoming(IP int, bind Bind) { logDebug := device.log.Debug - logDebug.Println("Routine, receive incomming, IP version:", IP) + logDebug.Println("Routine, receive incoming, IP version:", IP) for { diff --git a/src/tests/netns.sh b/src/tests/netns.sh index 22abea8..02d428b 100755 --- a/src/tests/netns.sh +++ b/src/tests/netns.sh @@ -120,6 +120,7 @@ configure_peers() { ip1 link set up dev wg1 ip2 link set up dev wg2 + sleep 1 } configure_peers diff --git a/src/timers.go b/src/timers.go index 31165a3..d372b0d 100644 --- a/src/timers.go +++ b/src/timers.go @@ -321,8 +321,14 @@ func (peer *Peer) RoutineHandshakeInitiator() { goto BeginHandshakes case <-timeout.C: - // TODO: Clear source address for peer - continue + + // clear source address of peer + + peer.mutex.Lock() + if peer.endpoint != nil { + peer.endpoint.ClearSrc() + } + peer.mutex.Unlock() } } diff --git a/src/tun_linux.go b/src/tun_linux.go index a728a48..81f1bbb 100644 --- a/src/tun_linux.go +++ b/src/tun_linux.go @@ -11,6 +11,7 @@ import ( "net" "os" "strings" + "time" "unsafe" ) @@ -60,15 +61,32 @@ func (tun *NativeTun) File() *os.File { return tun.fd } +func (tun *NativeTun) RoutineHackListener() { + /* This is needed for the detection to work accross network namespaces + * If you are reading this and know a better method, please get in touch. + */ + fd := int(tun.fd.Fd()) + for { + _, err := unix.Write(fd, nil) + switch err { + case unix.EINVAL: + tun.events <- TUNEventUp + case unix.EIO: + tun.events <- TUNEventDown + default: + } + time.Sleep(time.Second / 10) + } +} + func (tun *NativeTun) RoutineNetlinkListener() { + sock := int(C.bind_rtmgrp()) if sock < 0 { tun.errors <- errors.New("Failed to create netlink event listener") return } - tun.events <- TUNEventUp // TODO: Fix network namespace problem - for msg := make([]byte, 1<<16); ; { msgn, _, _, _, err := unix.Recvmsg(sock, msg[:], nil, 0) @@ -269,6 +287,7 @@ func CreateTUNFromFile(name string, fd *os.File) (TUNDevice, error) { } go device.RoutineNetlinkListener() + go device.RoutineHackListener() // cross namespace // set default MTU @@ -324,6 +343,7 @@ func CreateTUN(name string) (TUNDevice, error) { } go device.RoutineNetlinkListener() + go device.RoutineHackListener() // cross namespace // set default MTU