]> Cypherpunks.ru repositories - govpn.git/blobdiff - govpn.go
Well, performance is not so high actually
[govpn.git] / govpn.go
index b4eda05acd98f559e79f5b31f57d86b21decf4ab..7b15dd429910ab039016347a8dff3966fd0fa1e9 100644 (file)
--- a/govpn.go
+++ b/govpn.go
@@ -1,5 +1,5 @@
 /*
-govpn -- high-performance secure virtual private network daemon
+govpn -- simple secure virtual private network daemon
 Copyright (C) 2014 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
@@ -15,6 +15,8 @@ GNU General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
+
+// Simple secure virtual private network daemon
 package main
 
 import (
@@ -32,8 +34,8 @@ import (
        "os/signal"
        "time"
 
-       "code.google.com/p/go.crypto/poly1305"
-       "code.google.com/p/go.crypto/salsa20"
+       "golang.org/x/crypto/poly1305"
+       "golang.org/x/crypto/salsa20"
 )
 
 var (
@@ -44,6 +46,7 @@ var (
        upPath     = flag.String("up", "", "Path to up-script")
        downPath   = flag.String("down", "", "Path to down-script")
        mtu        = flag.Int("mtu", 1500, "MTU")
+       nonceDiff  = flag.Int("noncediff", 1, "Allow nonce difference")
        timeoutP   = flag.Int("timeout", 60, "Timeout seconds")
        verboseP   = flag.Bool("v", false, "Increase verbosity")
 )
@@ -92,6 +95,7 @@ func main() {
        flag.Parse()
        timeout := *timeoutP
        verbose := *verboseP
+       noncediff := uint64(*nonceDiff)
        log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
 
        // Key decoding
@@ -198,8 +202,6 @@ func main() {
        tag := new([poly1305.TagSize]byte)
        buf := make([]byte, *mtu+S20BS)
        emptyKey := make([]byte, KeySize)
-       ethPkt := make([]byte, maxIfacePktSize)
-       udpPktDataBuf := make([]byte, *mtu)
 
        if !serverMode {
                states[remote.String()] = HandshakeStart(conn, remote, key)
@@ -235,9 +237,7 @@ func main() {
                                udpSinkReady <- true
                                continue
                        }
-                       copy(udpPktDataBuf, udpBuf[:udpPkt.size])
-                       udpSinkReady <- true
-                       udpPktData = udpPktDataBuf[:udpPkt.size]
+                       udpPktData = udpBuf[:udpPkt.size]
                        if isValidHandshakePkt(udpPktData) {
                                addr = udpPkt.addr.String()
                                state, exists := states[addr]
@@ -246,13 +246,14 @@ func main() {
                                                state = &Handshake{addr: udpPkt.addr}
                                                states[addr] = state
                                        }
-                                       p = state.Server(conn, key, udpPktData)
+                                       p = state.Server(noncediff, conn, key, udpPktData)
                                } else {
                                        if !exists {
                                                fmt.Print("[HS?]")
+                                               udpSinkReady <- true
                                                continue
                                        }
-                                       p = state.Client(conn, key, udpPktData)
+                                       p = state.Client(noncediff, conn, key, udpPktData)
                                }
                                if p != nil {
                                        fmt.Print("[HS-OK]")
@@ -262,14 +263,17 @@ func main() {
                                        peer = p
                                        delete(states, addr)
                                }
+                               udpSinkReady <- true
                                continue
                        }
                        if peer == nil {
+                               udpSinkReady <- true
                                continue
                        }
                        nonceRecv, _ := binary.Uvarint(udpPktData[:8])
-                       if peer.nonceRecv >= nonceRecv {
+                       if nonceRecv < peer.nonceRecv-noncediff {
                                fmt.Print("R")
+                               udpSinkReady <- true
                                continue
                        }
                        copy(buf[:KeySize], emptyKey)
@@ -283,9 +287,11 @@ func main() {
                        )
                        copy(keyAuth[:], buf[:KeySize])
                        if !poly1305.Verify(tag, udpPktData[:udpPkt.size-poly1305.TagSize], keyAuth) {
+                               udpSinkReady <- true
                                fmt.Print("T")
                                continue
                        }
+                       udpSinkReady <- true
                        peer.nonceRecv = nonceRecv
                        timeouts = 0
                        frame = buf[S20BS : S20BS+udpPkt.size-NonceSize-poly1305.TagSize]
@@ -307,17 +313,16 @@ func main() {
                                ethSinkReady <- true
                                continue
                        }
+                       peer.nonceOur = peer.nonceOur + 2
+                       binary.PutUvarint(nonce, peer.nonceOur)
+                       copy(buf[:KeySize], emptyKey)
                        if ethPktSize > -1 {
-                               copy(ethPkt, ethBuf[:ethPktSize])
+                               copy(buf[S20BS:], ethBuf[:ethPktSize])
                                ethSinkReady <- true
                        } else {
-                               copy(ethPkt, heartbeatMark)
+                               copy(buf[S20BS:], heartbeatMark)
                                ethPktSize = HeartBeatSize
                        }
-                       peer.nonceOur = peer.nonceOur + 2
-                       binary.PutUvarint(nonce, peer.nonceOur)
-                       copy(buf[:KeySize], emptyKey)
-                       copy(buf[S20BS:], ethPkt[:ethPktSize])
                        salsa20.XORKeyStream(buf, buf, nonce, peer.key)
                        copy(buf[S20BS-NonceSize:S20BS], nonce)
                        copy(keyAuth[:], buf[:KeySize])