2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2015 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 "golang.org/x/crypto/poly1305"
29 "golang.org/x/crypto/salsa20"
30 "golang.org/x/crypto/xtea"
36 // S20BS is Salsa20's internal blocksize in bytes
38 // Maximal amount of bytes transfered with single key (4 GiB)
39 MaxBytesPerKey int64 = 1 << 32
40 // Size of packet's size mark in bytes
42 // Heartbeat rate, relative to Timeout
54 Key *[KeySize]byte `json:"-"`
55 NonceOur uint64 `json:"-"`
56 NonceRecv uint64 `json:"-"`
57 NonceCipher *xtea.Cipher `json:"-"`
61 tag *[poly1305.TagSize]byte
62 keyAuth *[KeySize]byte
79 func (p *Peer) String() string {
80 return p.Id.String() + ":" + p.Addr.String()
83 // Zero peer's memory state
84 func (p *Peer) Zero() {
87 sliceZero(p.keyAuth[:])
94 Emptiness = make([]byte, 1<<14)
95 taps = make(map[string]*TAP)
96 heartbeatPeriod time.Duration
99 func heartbeatPeriodGet() time.Duration {
100 if heartbeatPeriod == time.Duration(0) {
101 heartbeatPeriod = Timeout / TimeoutHeartbeat
103 return heartbeatPeriod
106 // Create TAP listening goroutine.
107 // This function takes required TAP interface name, opens it and allocates
108 // a buffer where all frame data will be written, channel where information
109 // about number of read bytes is sent to, synchronization channel (external
110 // processes tell that read buffer can be used again) and possible channel
112 func TAPListen(ifaceName string) (*TAP, chan []byte, chan struct{}, chan struct{}, error) {
115 tap, exists := taps[ifaceName]
117 tap, err = NewTAP(ifaceName)
119 return nil, nil, nil, nil, err
121 taps[ifaceName] = tap
123 sink := make(chan []byte)
124 sinkReady := make(chan struct{})
125 sinkTerminate := make(chan struct{})
126 sinkSkip := make(chan struct{})
129 heartbeat := time.Tick(heartbeatPeriodGet())
134 case <-sinkTerminate:
137 go func() { sink <- make([]byte, 0) }()
141 tap.ready <- struct{}{}
147 go func() { sink <- make([]byte, 0) }()
148 goto HeartbeatCatched
149 case <-sinkTerminate:
151 case pkt = <-tap.sink:
160 if exists && tap.synced {
161 sinkSkip <- struct{}{}
163 sinkReady <- struct{}{}
165 return tap, sink, sinkReady, sinkTerminate, nil
168 // Create UDP listening goroutine.
169 // This function takes already listening UDP socket and a buffer where
170 // all UDP packet data will be saved, channel where information about
171 // remote address and number of written bytes are stored, and a channel
172 // used to tell that buffer is ready to be overwritten.
173 func ConnListen(conn *net.UDPConn) (chan UDPPkt, []byte, chan struct{}) {
174 buf := make([]byte, MTU)
175 sink := make(chan UDPPkt)
176 sinkReady := make(chan struct{})
177 go func(conn *net.UDPConn) {
179 var addr *net.UDPAddr
183 conn.SetReadDeadline(time.Now().Add(time.Second))
184 n, addr, err = conn.ReadFromUDP(buf)
186 // This is needed for ticking the timeouts counter outside
187 sink <- UDPPkt{nil, 0}
190 sink <- UDPPkt{addr, n}
193 sinkReady <- struct{}{}
194 return sink, buf, sinkReady
197 func newNonceCipher(key *[KeySize]byte) *xtea.Cipher {
198 nonceKey := make([]byte, 16)
199 salsa20.XORKeyStream(
201 make([]byte, KeySize),
202 make([]byte, xtea.BlockSize),
205 ciph, err := xtea.NewCipher(nonceKey)
212 func newPeer(addr *net.UDPAddr, id PeerId, nonce int, key *[KeySize]byte) *Peer {
215 LastPing: time.Now(),
217 NonceOur: uint64(Noncediff + nonce),
218 NonceRecv: uint64(Noncediff + 0),
220 NonceCipher: newNonceCipher(key),
221 buf: make([]byte, MTU+S20BS),
222 tag: new([poly1305.TagSize]byte),
223 keyAuth: new([KeySize]byte),
224 nonce: make([]byte, NonceSize),
229 // Process incoming UDP packet.
230 // udpPkt is received data, related to the peer tap interface and
231 // ConnListen'es synchronization channel used to tell him that he is
232 // free to receive new packets. Authenticated and decrypted packets
233 // will be written to the interface immediately (except heartbeat ones).
234 func (p *Peer) UDPProcess(udpPkt []byte, tap io.Writer, ready chan struct{}) bool {
236 copy(p.buf, Emptiness)
237 copy(p.tag[:], udpPkt[size-poly1305.TagSize:])
238 copy(p.buf[S20BS:], udpPkt[NonceSize:size-poly1305.TagSize])
239 salsa20.XORKeyStream(
240 p.buf[:S20BS+size-poly1305.TagSize],
241 p.buf[:S20BS+size-poly1305.TagSize],
245 copy(p.keyAuth[:], p.buf[:KeySize])
246 if !poly1305.Verify(p.tag, udpPkt[:size-poly1305.TagSize], p.keyAuth) {
251 p.NonceCipher.Decrypt(p.buf, udpPkt[:NonceSize])
252 p.nonceRecv, _ = binary.Uvarint(p.buf[:NonceSize])
253 if int(p.NonceRecv)-Noncediff >= 0 && int(p.nonceRecv) < int(p.NonceRecv)-Noncediff {
260 p.BytesIn += int64(size)
261 p.LastPing = time.Now()
262 p.NonceRecv = p.nonceRecv
263 p.pktSize, _ = binary.Uvarint(p.buf[S20BS : S20BS+PktSizeSize])
268 p.frame = p.buf[S20BS+PktSizeSize : S20BS+PktSizeSize+p.pktSize]
269 p.BytesPayloadIn += int64(p.pktSize)
274 type WriteToer interface {
275 WriteTo([]byte, net.Addr) (int, error)
278 // Process incoming Ethernet packet.
279 // ethPkt is received data, conn is our outgoing connection.
280 // ready channel is TAPListen's synchronization channel used to tell him
281 // that he is free to receive new packets. Encrypted and authenticated
282 // packets will be sent to remote Peer side immediately.
283 func (p *Peer) EthProcess(ethPkt []byte, conn WriteToer, ready chan struct{}) {
286 // If this heartbeat is necessary
287 if size == 0 && !p.LastSent.Add(heartbeatPeriodGet()).Before(now) {
290 copy(p.buf, Emptiness)
292 copy(p.buf[S20BS+PktSizeSize:], ethPkt)
294 binary.PutUvarint(p.buf[S20BS:S20BS+PktSizeSize], uint64(size))
295 p.BytesPayloadOut += int64(size)
301 copy(p.nonce, Emptiness)
302 binary.PutUvarint(p.nonce, p.NonceOur)
303 p.NonceCipher.Encrypt(p.nonce, p.nonce)
305 salsa20.XORKeyStream(p.buf, p.buf, p.nonce, p.Key)
306 copy(p.buf[S20BS-NonceSize:S20BS], p.nonce)
307 copy(p.keyAuth[:], p.buf[:KeySize])
309 p.frame = p.buf[S20BS-NonceSize : S20BS+MTU-NonceSize-poly1305.TagSize]
311 p.frame = p.buf[S20BS-NonceSize : S20BS+PktSizeSize+size]
313 poly1305.Sum(p.tag, p.frame, p.keyAuth)
315 p.BytesOut += int64(len(p.frame) + poly1305.TagSize)
318 if _, err := conn.WriteTo(append(p.frame, p.tag[:]...), p.Addr); err != nil {
319 log.Println("Error sending UDP", err)