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 willSentCycle time.Time
63 tag *[poly1305.TagSize]byte
64 keyAuth *[KeySize]byte
81 func (p *Peer) String() string {
82 return p.Id.String() + ":" + p.Addr.String()
85 // Zero peer's memory state
86 func (p *Peer) Zero() {
89 sliceZero(p.keyAuth[:])
96 Emptiness = make([]byte, 1<<14)
97 taps = make(map[string]*TAP)
98 heartbeatPeriod time.Duration
101 func heartbeatPeriodGet() time.Duration {
102 if heartbeatPeriod == time.Duration(0) {
103 heartbeatPeriod = Timeout / TimeoutHeartbeat
105 return heartbeatPeriod
108 // Create TAP listening goroutine.
109 // This function takes required TAP interface name, opens it and allocates
110 // a buffer where all frame data will be written, channel where information
111 // about number of read bytes is sent to, synchronization channel (external
112 // processes tell that read buffer can be used again) and possible channel
114 func TAPListen(ifaceName string) (*TAP, chan []byte, chan struct{}, chan struct{}, error) {
117 tap, exists := taps[ifaceName]
119 tap, err = NewTAP(ifaceName)
121 return nil, nil, nil, nil, err
123 taps[ifaceName] = tap
125 sink := make(chan []byte)
126 sinkReady := make(chan struct{})
127 sinkTerminate := make(chan struct{})
128 sinkSkip := make(chan struct{})
131 heartbeat := time.Tick(heartbeatPeriodGet())
136 case <-sinkTerminate:
139 go func() { sink <- make([]byte, 0) }()
143 tap.ready <- struct{}{}
149 go func() { sink <- make([]byte, 0) }()
150 goto HeartbeatCatched
151 case <-sinkTerminate:
153 case pkt = <-tap.sink:
162 if exists && tap.synced {
163 sinkSkip <- struct{}{}
165 sinkReady <- struct{}{}
167 return tap, sink, sinkReady, sinkTerminate, nil
170 // Create UDP listening goroutine.
171 // This function takes already listening UDP socket and a buffer where
172 // all UDP packet data will be saved, channel where information about
173 // remote address and number of written bytes are stored, and a channel
174 // used to tell that buffer is ready to be overwritten.
175 func ConnListen(conn *net.UDPConn) (chan UDPPkt, []byte, chan struct{}) {
176 buf := make([]byte, MTU)
177 sink := make(chan UDPPkt)
178 sinkReady := make(chan struct{})
179 go func(conn *net.UDPConn) {
181 var addr *net.UDPAddr
185 conn.SetReadDeadline(time.Now().Add(time.Second))
186 n, addr, err = conn.ReadFromUDP(buf)
188 // This is needed for ticking the timeouts counter outside
189 sink <- UDPPkt{nil, 0}
192 sink <- UDPPkt{addr, n}
195 sinkReady <- struct{}{}
196 return sink, buf, sinkReady
199 func newNonceCipher(key *[KeySize]byte) *xtea.Cipher {
200 nonceKey := make([]byte, 16)
201 salsa20.XORKeyStream(
203 make([]byte, KeySize),
204 make([]byte, xtea.BlockSize),
207 ciph, err := xtea.NewCipher(nonceKey)
214 func newPeer(addr *net.UDPAddr, id PeerId, nonce int, key *[KeySize]byte) *Peer {
221 NonceOur: uint64(Noncediff + nonce),
222 NonceRecv: uint64(Noncediff + 0),
224 NonceCipher: newNonceCipher(key),
225 buf: make([]byte, MTU+S20BS),
226 tag: new([poly1305.TagSize]byte),
227 keyAuth: new([KeySize]byte),
228 nonce: make([]byte, NonceSize),
233 // Process incoming UDP packet.
234 // udpPkt is received data, related to the peer tap interface and
235 // ConnListen'es synchronization channel used to tell him that he is
236 // free to receive new packets. Authenticated and decrypted packets
237 // will be written to the interface immediately (except heartbeat ones).
238 func (p *Peer) UDPProcess(udpPkt []byte, tap io.Writer, ready chan struct{}) bool {
240 copy(p.buf, Emptiness)
241 copy(p.tag[:], udpPkt[size-poly1305.TagSize:])
242 copy(p.buf[S20BS:], udpPkt[NonceSize:size-poly1305.TagSize])
243 salsa20.XORKeyStream(
244 p.buf[:S20BS+size-poly1305.TagSize],
245 p.buf[:S20BS+size-poly1305.TagSize],
249 copy(p.keyAuth[:], p.buf[:KeySize])
250 if !poly1305.Verify(p.tag, udpPkt[:size-poly1305.TagSize], p.keyAuth) {
255 p.NonceCipher.Decrypt(p.buf, udpPkt[:NonceSize])
256 p.nonceRecv, _ = binary.Uvarint(p.buf[:NonceSize])
257 if int(p.NonceRecv)-Noncediff >= 0 && int(p.nonceRecv) < int(p.NonceRecv)-Noncediff {
264 p.BytesIn += int64(size)
265 p.LastPing = time.Now()
266 p.NonceRecv = p.nonceRecv
267 p.pktSize, _ = binary.Uvarint(p.buf[S20BS : S20BS+PktSizeSize])
272 p.frame = p.buf[S20BS+PktSizeSize : S20BS+PktSizeSize+p.pktSize]
273 p.BytesPayloadIn += int64(p.pktSize)
278 type WriteToer interface {
279 WriteTo([]byte, net.Addr) (int, error)
282 // Process incoming Ethernet packet.
283 // ethPkt is received data, conn is our outgoing connection.
284 // ready channel is TAPListen's synchronization channel used to tell him
285 // that he is free to receive new packets. Encrypted and authenticated
286 // packets will be sent to remote Peer side immediately.
287 func (p *Peer) EthProcess(ethPkt []byte, conn WriteToer, ready chan struct{}) {
290 // If this heartbeat is necessary
291 if size == 0 && !p.LastSent.Add(heartbeatPeriodGet()).Before(now) {
294 copy(p.buf, Emptiness)
296 copy(p.buf[S20BS+PktSizeSize:], ethPkt)
298 binary.PutUvarint(p.buf[S20BS:S20BS+PktSizeSize], uint64(size))
299 p.BytesPayloadOut += int64(size)
305 copy(p.nonce, Emptiness)
306 binary.PutUvarint(p.nonce, p.NonceOur)
307 p.NonceCipher.Encrypt(p.nonce, p.nonce)
309 salsa20.XORKeyStream(p.buf, p.buf, p.nonce, p.Key)
310 copy(p.buf[S20BS-NonceSize:S20BS], p.nonce)
311 copy(p.keyAuth[:], p.buf[:KeySize])
313 p.frame = p.buf[S20BS-NonceSize : S20BS+MTU-NonceSize-poly1305.TagSize]
315 p.frame = p.buf[S20BS-NonceSize : S20BS+PktSizeSize+size]
317 poly1305.Sum(p.tag, p.frame, p.keyAuth)
319 p.BytesOut += int64(len(p.frame) + poly1305.TagSize)
323 p.willSentCycle = p.LastSent.Add(cprCycle)
324 if p.willSentCycle.After(now) {
325 time.Sleep(p.willSentCycle.Sub(now))
326 now = p.willSentCycle
330 if _, err := conn.WriteTo(append(p.frame, p.tag[:]...), p.Addr); err != nil {
331 log.Println("Error sending UDP", err)