]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
Fixed transport header problem
authorMathias Hall-Andersen <mathias@hall-andersen.dk>
Sun, 2 Jul 2017 13:28:38 +0000 (15:28 +0200)
committerMathias Hall-Andersen <mathias@hall-andersen.dk>
Sun, 2 Jul 2017 13:28:38 +0000 (15:28 +0200)
src/helper_test.go
src/keypair.go
src/noise_protocol.go
src/receive.go
src/send.go

index 464292fcf31c0e47ed729ee697ee8a5f451b868f..6d85771c3975dffac1b4e48f070a1e82d9c5f0eb 100644 (file)
@@ -10,7 +10,7 @@ import (
 
 type DummyTUN struct {
        name    string
-       mtu     uint
+       mtu     int
        packets chan []byte
 }
 
@@ -18,7 +18,7 @@ func (tun *DummyTUN) Name() string {
        return tun.name
 }
 
-func (tun *DummyTUN) MTU() uint {
+func (tun *DummyTUN) MTU() int {
        return tun.mtu
 }
 
index 0e845f772e8feefed5f546b2e357307750725c75..0fac5cb290481e44e4e6175b175e99769720cbbc 100644 (file)
@@ -13,7 +13,8 @@ type KeyPair struct {
        sendNonce   uint64
        isInitiator bool
        created     time.Time
-       id          uint32
+       localIndex  uint32
+       remoteIndex uint32
 }
 
 type KeyPairs struct {
index adb00ec719108d7d894852856929184b1cd8bb29..5a6290131082bd3e8ec73aa0d020e70d92e4835c 100644 (file)
@@ -32,10 +32,11 @@ const (
 )
 
 const (
-       MessageInitiationSize  = 148
-       MessageResponseSize    = 92
-       MessageCookieReplySize = 64
-       MessageTransportSize   = 16 + poly1305.TagSize // size of empty transport
+       MessageInitiationSize      = 148
+       MessageResponseSize        = 92
+       MessageCookieReplySize     = 64
+       MessageTransportHeaderSize = 16
+       MessageTransportSize       = MessageTransportHeaderSize + poly1305.TagSize // size of empty transport
 )
 
 const (
@@ -449,6 +450,8 @@ func (peer *Peer) NewKeyPair() *KeyPair {
        keyPair.sendNonce = 0
        keyPair.recvNonce = 0
        keyPair.created = time.Now()
+       keyPair.localIndex = peer.handshake.localIndex
+       keyPair.remoteIndex = peer.handshake.remoteIndex
 
        // remap index
 
@@ -471,7 +474,7 @@ func (peer *Peer) NewKeyPair() *KeyPair {
                        if kp.previous != nil {
                                kp.previous.send = nil
                                kp.previous.recv = nil
-                               peer.device.indices.Delete(kp.previous.id)
+                               peer.device.indices.Delete(kp.previous.localIndex)
                        }
                        kp.previous = kp.current
                        kp.current = keyPair
index ab289446571f81b5e4d7f5ab5286bf0fe24f7dee..5afbf7f1a406717ba015b7a95b95cb448dd142eb 100644 (file)
@@ -75,6 +75,7 @@ func (device *Device) RoutineReceiveIncomming() {
                // handle packet
 
                packet = packet[:size]
+               debugLog.Println("GOT:", packet)
                msgType := binary.LittleEndian.Uint32(packet[:4])
 
                func() {
@@ -371,6 +372,8 @@ func (peer *Peer) RoutineSequentialReceiver() {
 
                // check for replay
 
+               // strip padding
+
                // check for keep-alive
 
                if len(elem.packet) == 0 {
@@ -393,8 +396,6 @@ func (device *Device) RoutineWriteToTUN(tun TUNDevice) {
                case packet = <-device.queue.inbound:
                }
 
-               device.log.Debug.Println("GOT:", packet)
-
                size, err := tun.Write(packet)
                device.log.Debug.Println("DEBUG:", size, err)
                if err != nil {
index 7a105605c483631c7322cfc9bf1c51098417a9e6..3fe4733a02d61943d7ef7d2671d8ecdd91479f51 100644 (file)
@@ -171,8 +171,6 @@ func (peer *Peer) RoutineNonce() {
                                }
                        }
 
-                       logger.Println("PACKET:", packet)
-
                        // wait for key pair
 
                        for {
@@ -221,8 +219,6 @@ func (peer *Peer) RoutineNonce() {
                                work.peer = peer
                                work.mutex.Lock()
 
-                               logger.Println("WORK:", work)
-
                                packet = nil
 
                                // drop packets until there is space
@@ -263,7 +259,7 @@ func (device *Device) RoutineEncryption() {
 
                // pad packet
 
-               padding := device.mtu - len(work.packet)
+               padding := device.mtu - len(work.packet) - MessageTransportSize
                if padding < 0 {
                        work.Drop()
                        continue
@@ -272,19 +268,30 @@ func (device *Device) RoutineEncryption() {
                for n := 0; n < padding; n += 1 {
                        work.packet = append(work.packet, 0)
                }
-               device.log.Debug.Println(work.packet)
+               content := work.packet[MessageTransportHeaderSize:]
+               copy(content, work.packet)
+
+               // prepare header
 
-               // encrypt
+               binary.LittleEndian.PutUint32(work.packet[:4], MessageTransportType)
+               binary.LittleEndian.PutUint32(work.packet[4:8], work.keyPair.remoteIndex)
+               binary.LittleEndian.PutUint64(work.packet[8:16], work.nonce)
+
+               device.log.Debug.Println(work.packet, work.nonce)
+
+               // encrypt content
 
                binary.LittleEndian.PutUint64(nonce[4:], work.nonce)
-               work.packet = work.keyPair.send.Seal(
-                       work.packet[:0],
+               work.keyPair.send.Seal(
+                       content[:0],
                        nonce[:],
-                       work.packet,
+                       content,
                        nil,
                )
                work.mutex.Unlock()
 
+               device.log.Debug.Println(work.packet, work.nonce)
+
                // initiate new handshake
 
                work.peer.KeepKeyFreshSending()