]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/cmd/govpn-client/tcp.go
Close old sockets during rehandshake
[govpn.git] / src / govpn / cmd / govpn-client / tcp.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2015 Sergey Matveev <stargrave@stargrave.org>
4
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.
9
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.
14
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/>.
17 */
18
19 package main
20
21 import (
22         "bytes"
23         "encoding/binary"
24         "log"
25         "net"
26         "sync/atomic"
27         "time"
28
29         "govpn"
30 )
31
32 func startTCP(timeouted, rehandshaking, termination chan struct{}) {
33         remote, err := net.ResolveTCPAddr("tcp", *remoteAddr)
34         if err != nil {
35                 log.Fatalln("Can not resolve remote address:", err)
36         }
37         conn, err := net.DialTCP("tcp", nil, remote)
38         if err != nil {
39                 log.Fatalln("Can not connect to address:", err)
40         }
41         log.Println("Connected to TCP:" + *remoteAddr)
42         handleTCP(conn, timeouted, rehandshaking, termination)
43 }
44
45 func handleTCP(conn *net.TCPConn, timeouted, rehandshaking, termination chan struct{}) {
46         hs := govpn.HandshakeStart(*remoteAddr, conn, conf)
47         buf := make([]byte, govpn.MTU)
48         var n int
49         var err error
50         var prev int
51         var peer *govpn.Peer
52         var terminator chan struct{}
53 HandshakeCycle:
54         for {
55                 select {
56                 case <-termination:
57                         break HandshakeCycle
58                 default:
59                 }
60                 if prev == govpn.MTU {
61                         log.Println("Timeouted waiting for the packet")
62                         timeouted <- struct{}{}
63                         break HandshakeCycle
64                 }
65
66                 conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
67                 n, err = conn.Read(buf[prev:])
68                 if err != nil {
69                         log.Println("Connection timeouted")
70                         timeouted <- struct{}{}
71                         break HandshakeCycle
72                 }
73
74                 prev += n
75                 peerId := govpn.IDsCache.Find(buf[:prev])
76                 if peerId == nil {
77                         continue
78                 }
79                 peer = hs.Client(buf[:prev])
80                 prev = 0
81                 if peer == nil {
82                         continue
83                 }
84                 log.Println("Handshake completed")
85                 knownPeers = govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})
86                 if firstUpCall {
87                         go govpn.ScriptCall(*upPath, *ifaceName)
88                         firstUpCall = false
89                 }
90                 hs.Zero()
91                 terminator = make(chan struct{})
92                 go func() {
93                         heartbeat := time.NewTicker(peer.Timeout)
94                         var data []byte
95                 Processor:
96                         for {
97                                 select {
98                                 case <-heartbeat.C:
99                                         peer.EthProcess(nil)
100                                 case <-terminator:
101                                         break Processor
102                                 case data = <-tap.Sink:
103                                         peer.EthProcess(data)
104                                 }
105                         }
106                         heartbeat.Stop()
107                         peer.Zero()
108                 }()
109                 break HandshakeCycle
110         }
111         if hs != nil {
112                 hs.Zero()
113         }
114         if peer == nil {
115                 return
116         }
117
118         nonceExpectation := make([]byte, govpn.NonceSize)
119         binary.BigEndian.PutUint64(nonceExpectation, peer.NonceExpect)
120         peer.NonceCipher.Encrypt(nonceExpectation, nonceExpectation)
121         prev = 0
122         var i int
123 TransportCycle:
124         for {
125                 select {
126                 case <-termination:
127                         break TransportCycle
128                 default:
129                 }
130                 if prev == govpn.MTU {
131                         log.Println("Timeouted waiting for the packet")
132                         timeouted <- struct{}{}
133                         break TransportCycle
134                 }
135                 conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
136                 n, err = conn.Read(buf[prev:])
137                 if err != nil {
138                         log.Println("Connection timeouted")
139                         timeouted <- struct{}{}
140                         break TransportCycle
141                 }
142                 prev += n
143         CheckMore:
144                 if prev < govpn.MinPktLength {
145                         continue
146                 }
147                 i = bytes.Index(buf[:prev], nonceExpectation)
148                 if i == -1 {
149                         continue
150                 }
151                 if !peer.PktProcess(buf[:i+govpn.NonceSize], tap, false) {
152                         log.Println("Unauthenticated packet, dropping connection")
153                         timeouted <- struct{}{}
154                         break TransportCycle
155                 }
156                 if atomic.LoadInt64(&peer.BytesIn)+atomic.LoadInt64(&peer.BytesOut) > govpn.MaxBytesPerKey {
157                         log.Println("Need rehandshake")
158                         rehandshaking <- struct{}{}
159                         break TransportCycle
160                 }
161                 binary.BigEndian.PutUint64(nonceExpectation, peer.NonceExpect)
162                 peer.NonceCipher.Encrypt(nonceExpectation, nonceExpectation)
163                 copy(buf, buf[i+govpn.NonceSize:prev])
164                 prev = prev - i - govpn.NonceSize
165                 goto CheckMore
166         }
167         if terminator != nil {
168                 terminator <- struct{}{}
169         }
170         peer.Zero()
171         conn.Close()
172 }