This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
forked from titanous/go-wireguard
-
Notifications
You must be signed in to change notification settings - Fork 2
/
interface.go
298 lines (233 loc) · 6.41 KB
/
interface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package wireguard
import (
"errors"
"fmt"
"io"
"sync"
"sync/atomic"
"github.com/flynn/noise"
"golang.org/x/crypto/curve25519"
)
var (
errPeerDoesntExist = errors.New("wireguard: peer doesnt exist")
)
const maxQueuePackets int = 1024
// An InterfaceConfig is the configuration used to create an interface.
type InterfaceConfig struct {
// Outside is the connection that will be used to send and receive encrypted
// packets with peers. It will be closed if Close is called on the Interface.
Outside UDPConn
// Inside is the interface that will be used to read plaintext packets
// destined for peers and write decrypted packets received from peers. Each
// Read must return a single IP packet to send to a peer, and each Write
// will provide a single received IP packet.
Inside io.ReadWriter
// PrivateKey holds the static Curve25519 private key for the interface. If
// set, it must be exactly 32 random bytes.
PrivateKey []byte
// PresharedKey holds an optional pre-shared key to use during handshakes.
// If set, it must be exactly 32 random bytes.
PresharedKey []byte
// Peers is the initial set of peers that the interface will communicate
// with.
Peers []*Peer
}
func NewInterface(c InterfaceConfig) (*Interface, error) {
if c.Outside == nil {
return nil, errors.New("wireguard: Outside connection is nil")
}
if c.Inside == nil {
return nil, errors.New("wireguard: Inside pipe is nil")
}
f := &Interface{
outside: c.Outside,
inside: c.Inside,
}
if err := f.SetPrivateKey(c.PrivateKey); err != nil {
return nil, err
}
if err := f.SetPresharedKey(c.PresharedKey); err != nil {
return nil, err
}
f.handshakes = make(map[uint32]*noiseHandshake)
f.routetable = NewRouteTable()
if err := f.SetPeers(c.Peers); err != nil {
return nil, err
}
return f, nil
}
type publicKey [32]byte
// An Interface communicates encrypted packets with peers.
type Interface struct {
started bool
outside UDPConn
inside io.ReadWriter
identityMtx sync.RWMutex // protects staticKey and presharedKey
staticKey noise.DHKey
presharedKey []byte
peersMtx sync.RWMutex
peers map[publicKey]*peer
peerList []*peer
handshakesMtx sync.RWMutex
handshakes map[uint32]*noiseHandshake
keypairsMtx sync.RWMutex
keypairs map[uint32]*noiseKeypair
routetable RouteTable
}
// Run starts the interface and blocks until it is closed.
func (f *Interface) Run() error {
if f.started {
return errors.New("wireguard: the interface is already started")
}
// What do we do here?
// 1. Watch for incoming packets from listening UDP connection
// 2. Watch for incoming packets from inside interface (io.ReadWriter)
var wg sync.WaitGroup
wg.Add(2)
go func() {
// udp socket read loop
f.acceptOutsidePackets()
wg.Done()
}()
go func() {
// handle inside interface i/o
f.readInsidePackets()
wg.Done()
}()
wg.Wait()
return nil
}
// Close shuts down the interface.
func (f *Interface) Close() error {
return f.outside.Close()
}
// SetPrivateKey changes the private key for the interface. It is safe to call
// while the interface is running.
func (f *Interface) SetPrivateKey(k []byte) error {
if len(k) != 0 && len(k) != 32 {
return errors.New("wireguard: PrivateKey must be exactly 32 bytes")
}
f.identityMtx.Lock()
defer f.identityMtx.Unlock()
if len(k) == 0 {
f.staticKey.Private = nil
f.staticKey.Public = nil
return nil
}
// calculate public key and set the keys on the interface
var pubkey, privkey [32]byte
copy(privkey[:], k)
curve25519.ScalarBaseMult(&pubkey, &privkey)
f.staticKey.Private = privkey[:]
f.staticKey.Public = pubkey[:]
return nil
}
// SetPresharedKey changes the pre-shared key for the interface.
func (f *Interface) SetPresharedKey(k []byte) error {
if len(k) != 0 && len(k) != 32 {
return errors.New("wireguard: PresharedKey must be exactly 32 bytes")
}
f.identityMtx.Lock()
defer f.identityMtx.Unlock()
if len(k) == 0 {
f.presharedKey = nil
} else {
key := make([]byte, 32)
copy(key, k)
f.presharedKey = key
}
return nil
}
// SetPeers replaces all of the peers that the interface is configured for with
// a new list.
func (f *Interface) SetPeers(peers []*Peer) error {
f.routetable.Clear()
f.peersMtx.Lock()
f.peerList = []*peer{}
f.peers = make(map[publicKey]*peer)
f.peersMtx.Unlock()
for _, p := range peers {
if err := f.AddPeer(p); err != nil {
return err
}
}
return nil
}
// GetPeers retrieves a list of all peers known to the interface.
func (f *Interface) GetPeers() []*Peer {
peers := make([]*Peer, len(f.peerList))
for i, v := range f.peerList {
peers[i] = v.public()
}
return peers
}
// RemovePeer removes the peer identified with the public key pubkey from the
// interface configuration.
func (f *Interface) RemovePeer(pubkey []byte) error {
pk := publicKey{}
copy(pk[:], pubkey)
p := f.peers[pk]
if p == nil {
return errPeerDoesntExist
}
f.peersMtx.Lock()
for i := range f.peerList {
if p.internalID == f.peerList[i].internalID {
copy(f.peerList[i:], f.peerList[i+1:])
break
}
}
f.peersMtx.Unlock()
p.handshake.clear()
p.keypairs.clear()
err := f.routetable.RemoveByPeer(p)
f.peersMtx.Lock()
delete(f.peers, pk)
f.peersMtx.Unlock()
p.retransmitHandshake.Stop()
p.sendKeepalive.Stop()
p.newHandshake.Stop()
p.killEphemerals.Stop()
p.persistentKeepalive.Stop()
p.txQueue = nil
return err
}
// AddPeer adds a peer to the interface configuration. If the peer, identified
// by its public key, already exists, then all configuration will be replaced
// with the new fields.
func (f *Interface) AddPeer(p *Peer) error {
pk := publicKey{}
copy(pk[:], p.PublicKey)
np := f.peers[pk]
// peer exists
if np != nil {
_ = f.routetable.RemoveByPeer(np)
goto ReplaceExistingPeer
}
// new peer
np = &peer{internalID: atomic.AddUint64(&peerCounter, 1)}
f.peersMtx.Lock()
f.peers[pk] = np
f.peerList = append(f.peerList, np)
f.peersMtx.Unlock()
np.txQueue = &PacketQueue{}
np.handshake = noiseHandshake{remoteStatic: [32]byte(pk), peer: np}
np.iface = f
np.initTimers()
ReplaceExistingPeer:
np.persistentKeepaliveInterval = p.PersistentKeepaliveInterval
np.endpointAddrMtx.Lock()
np.endpointAddr = p.Endpoint
fmt.Println("adding peer: ", np.endpointAddr)
if np.endpointAddr != nil {
np.conn = f.outside
}
np.endpointAddrMtx.Unlock()
for _, r := range p.AllowedIPs {
if err := f.routetable.Insert(r, np); err != nil {
defer f.routetable.RemoveByPeer(np)
return err
}
}
return nil
}