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/>.
27 "golang.org/x/crypto/poly1305"
28 "golang.org/x/crypto/salsa20"
29 "golang.org/x/crypto/xtea"
34 // S20BS is Salsa20's internal blocksize in bytes
36 // Maximal amount of bytes transfered with single key (4 GiB)
37 MaxBytesPerKey int64 = 1 << 32
38 // Size of packet's size mark in bytes
40 // Heartbeat rate, relative to Timeout
56 CPRCycle time.Duration `json:"-"`
58 // Cryptography related
59 Key *[SSize]byte `json:"-"`
61 NonceOur uint64 `json:"-"`
62 NonceRecv uint64 `json:"-"`
63 NonceCipher *xtea.Cipher `json:"-"`
66 Timeout time.Duration `json:"-"`
70 willSentCycle time.Time
72 // This variables are initialized only once to relief GC
74 tag *[poly1305.TagSize]byte
96 func (p *Peer) String() string {
97 return p.Id.String() + ":" + p.Addr.String()
100 // Zero peer's memory state.
101 func (p *Peer) Zero() {
104 sliceZero(p.keyAuth[:])
111 Emptiness = make([]byte, 1<<14)
112 taps = make(map[string]*TAP)
115 // Create TAP listening goroutine.
116 // This function takes required TAP interface name, opens it and allocates
117 // a buffer where all frame data will be written, channel where information
118 // about number of read bytes is sent to, synchronization channel (external
119 // processes tell that read buffer can be used again) and possible channel
121 func TAPListen(ifaceName string, timeout time.Duration, cpr int) (*TAP, chan []byte, chan struct{}, chan struct{}, error) {
124 tap, exists := taps[ifaceName]
126 tap, err = NewTAP(ifaceName)
128 return nil, nil, nil, nil, err
130 taps[ifaceName] = tap
132 sink := make(chan []byte)
133 sinkReady := make(chan struct{})
134 sinkTerminate := make(chan struct{})
135 sinkSkip := make(chan struct{})
138 cprCycle := cprCycleCalculate(cpr)
139 if cprCycle != time.Duration(0) {
142 timeout = timeout / TimeoutHeartbeat
144 heartbeat := time.Tick(timeout)
149 case <-sinkTerminate:
152 go func() { sink <- make([]byte, 0) }()
156 tap.ready <- struct{}{}
162 go func() { sink <- make([]byte, 0) }()
163 goto HeartbeatCatched
164 case <-sinkTerminate:
166 case pkt = <-tap.sink:
175 if exists && tap.synced {
176 sinkSkip <- struct{}{}
178 sinkReady <- struct{}{}
180 return tap, sink, sinkReady, sinkTerminate, nil
183 // Create UDP listening goroutine.
184 // This function takes already listening UDP socket and a buffer where
185 // all UDP packet data will be saved, channel where information about
186 // remote address and number of written bytes are stored, and a channel
187 // used to tell that buffer is ready to be overwritten.
188 func ConnListen(conn *net.UDPConn) (chan UDPPkt, []byte, chan struct{}) {
189 buf := make([]byte, MTU)
190 sink := make(chan UDPPkt)
191 sinkReady := make(chan struct{})
192 go func(conn *net.UDPConn) {
194 var addr *net.UDPAddr
198 conn.SetReadDeadline(time.Now().Add(time.Second))
199 n, addr, err = conn.ReadFromUDP(buf)
201 // This is needed for ticking the timeouts counter outside
202 sink <- UDPPkt{nil, 0}
205 sink <- UDPPkt{addr, n}
208 sinkReady <- struct{}{}
209 return sink, buf, sinkReady
212 func newNonceCipher(key *[32]byte) *xtea.Cipher {
213 nonceKey := make([]byte, 16)
214 salsa20.XORKeyStream(
217 make([]byte, xtea.BlockSize),
220 ciph, err := xtea.NewCipher(nonceKey)
227 func cprCycleCalculate(rate int) time.Duration {
229 return time.Duration(0)
231 return time.Second / time.Duration(rate*(1<<10)/MTU)
234 func newPeer(addr *net.UDPAddr, conf *PeerConf, nonce int, key *[SSize]byte) *Peer {
236 timeout := conf.Timeout
237 cprCycle := cprCycleCalculate(conf.CPR)
238 noiseEnable := conf.NoiseEnable
243 timeout = timeout / TimeoutHeartbeat
251 NoiseEnable: noiseEnable,
254 Noncediff: conf.Noncediff,
255 NonceOur: uint64(conf.Noncediff + nonce),
256 NonceRecv: uint64(conf.Noncediff + 0),
258 NonceCipher: newNonceCipher(key),
259 buf: make([]byte, MTU+S20BS),
260 tag: new([poly1305.TagSize]byte),
261 keyAuth: new([SSize]byte),
262 nonce: make([]byte, NonceSize),
267 // Process incoming UDP packet.
268 // udpPkt is received data, related to the peer tap interface and
269 // ConnListen'es synchronization channel used to tell him that he is
270 // free to receive new packets. Authenticated and decrypted packets
271 // will be written to the interface immediately (except heartbeat ones).
272 func (p *Peer) UDPProcess(udpPkt []byte, tap io.Writer, ready chan struct{}) bool {
274 copy(p.buf, Emptiness)
275 copy(p.tag[:], udpPkt[p.size-poly1305.TagSize:])
276 copy(p.buf[S20BS:], udpPkt[NonceSize:p.size-poly1305.TagSize])
277 salsa20.XORKeyStream(
278 p.buf[:S20BS+p.size-poly1305.TagSize],
279 p.buf[:S20BS+p.size-poly1305.TagSize],
283 copy(p.keyAuth[:], p.buf[:SSize])
284 if !poly1305.Verify(p.tag, udpPkt[:p.size-poly1305.TagSize], p.keyAuth) {
289 p.NonceCipher.Decrypt(p.buf, udpPkt[:NonceSize])
290 p.nonceRecv, _ = binary.Uvarint(p.buf[:NonceSize])
291 if int(p.NonceRecv)-p.Noncediff >= 0 && int(p.nonceRecv) < int(p.NonceRecv)-p.Noncediff {
298 p.BytesIn += int64(p.size)
299 p.LastPing = time.Now()
300 p.NonceRecv = p.nonceRecv
301 p.pktSize, _ = binary.Uvarint(p.buf[S20BS : S20BS+PktSizeSize])
306 p.frame = p.buf[S20BS+PktSizeSize : S20BS+PktSizeSize+p.pktSize]
307 p.BytesPayloadIn += int64(p.pktSize)
312 type WriteToUDPer interface {
313 WriteToUDP([]byte, *net.UDPAddr) (int, error)
316 // Process incoming Ethernet packet.
317 // ethPkt is received data, conn is our outgoing connection.
318 // ready channel is TAPListen's synchronization channel used to tell him
319 // that he is free to receive new packets. Encrypted and authenticated
320 // packets will be sent to remote Peer side immediately.
321 func (p *Peer) EthProcess(ethPkt []byte, conn WriteToUDPer, ready chan struct{}) {
324 // If this heartbeat is necessary
325 if p.size == 0 && !p.LastSent.Add(p.Timeout).Before(p.now) {
328 copy(p.buf, Emptiness)
330 copy(p.buf[S20BS+PktSizeSize:], ethPkt)
332 binary.PutUvarint(p.buf[S20BS:S20BS+PktSizeSize], uint64(p.size))
333 p.BytesPayloadOut += int64(p.size)
339 copy(p.nonce, Emptiness)
340 binary.PutUvarint(p.nonce, p.NonceOur)
341 p.NonceCipher.Encrypt(p.nonce, p.nonce)
343 salsa20.XORKeyStream(p.buf, p.buf, p.nonce, p.Key)
344 copy(p.buf[S20BS-NonceSize:S20BS], p.nonce)
345 copy(p.keyAuth[:], p.buf[:SSize])
347 p.frame = p.buf[S20BS-NonceSize : S20BS+MTU-NonceSize-poly1305.TagSize]
349 p.frame = p.buf[S20BS-NonceSize : S20BS+PktSizeSize+p.size]
351 poly1305.Sum(p.tag, p.frame, p.keyAuth)
353 p.BytesOut += int64(len(p.frame) + poly1305.TagSize)
356 if p.CPRCycle != time.Duration(0) {
357 p.willSentCycle = p.LastSent.Add(p.CPRCycle)
358 if p.willSentCycle.After(p.now) {
359 time.Sleep(p.willSentCycle.Sub(p.now))
360 p.now = p.willSentCycle
364 conn.WriteToUDP(append(p.frame, p.tag[:]...), p.Addr)