-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conn.go
416 lines (386 loc) · 11.2 KB
/
conn.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package connectip
import (
"context"
"encoding/binary"
"errors"
"fmt"
"log"
"net"
"net/netip"
"slices"
"sync"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
"github.com/quic-go/quic-go/quicvarint"
)
type CloseError struct {
Remote bool
}
func (e *CloseError) Error() string { return net.ErrClosed.Error() }
func (e *CloseError) Is(target error) bool { return target == net.ErrClosed }
type appendable interface{ append([]byte) []byte }
type writeCapsule struct {
capsule appendable
result chan error
}
const (
ipProtoICMP = 1
ipProtoICMPv6 = 58
)
// If a packet is too large to fit into a QUIC datagram,
// we send an ICMP Packet Too Big packet.
// On IPv6, the minimum MTU of a link is 1280 bytes.
const minMTU = 1280
// Conn is a connection that proxies IP packets over HTTP/3.
type Conn struct {
str http3.Stream
writes chan writeCapsule
assignedAddressNotify chan struct{}
availableRoutesNotify chan struct{}
mu sync.Mutex
peerAddresses []netip.Prefix // IP prefixes that we assigned to the peer
localRoutes []IPRoute // IP routes that we advertised to the peer
assignedAddresses []netip.Prefix
availableRoutes []IPRoute
closeChan chan struct{}
closeErr error
}
func newProxiedConn(str http3.Stream) *Conn {
c := &Conn{
str: str,
writes: make(chan writeCapsule),
assignedAddressNotify: make(chan struct{}, 1),
availableRoutesNotify: make(chan struct{}, 1),
closeChan: make(chan struct{}),
}
go func() {
if err := c.readFromStream(); err != nil {
log.Printf("handling stream failed: %v", err)
c.mu.Lock()
if c.closeErr == nil {
c.closeErr = &CloseError{Remote: true}
close(c.closeChan)
}
c.mu.Unlock()
}
}()
go func() {
if err := c.writeToStream(); err != nil {
log.Printf("writing to stream failed: %v", err)
c.mu.Lock()
if c.closeErr == nil {
c.closeErr = &CloseError{Remote: true}
close(c.closeChan)
}
c.mu.Unlock()
}
}()
return c
}
// AdvertiseRoute informs the peer about available routes.
// This function can be called multiple times, but only the routes from the most recent call will be active.
// Previous route advertisements are overwritten by each new call to this function.
func (c *Conn) AdvertiseRoute(ctx context.Context, routes []IPRoute) error {
for _, route := range routes {
if route.StartIP.Compare(route.EndIP) == 1 {
return fmt.Errorf("invalid route advertising start_ip: %s larger than %s", route.StartIP, route.EndIP)
}
}
c.mu.Lock()
c.localRoutes = slices.Clone(routes)
c.mu.Unlock()
return c.sendCapsule(ctx, &routeAdvertisementCapsule{IPAddressRanges: routes})
}
// AssignAddresses assigned address prefixes to the peer.
// This function can be called multiple times, but only the addresses from the most recent call will be active.
// Previous address assignments are overwritten by each new call to this function.
func (c *Conn) AssignAddresses(ctx context.Context, prefixes []netip.Prefix) error {
c.mu.Lock()
c.peerAddresses = slices.Clone(prefixes)
c.mu.Unlock()
capsule := &addressAssignCapsule{AssignedAddresses: make([]AssignedAddress, 0, len(prefixes))}
for _, p := range prefixes {
capsule.AssignedAddresses = append(capsule.AssignedAddresses, AssignedAddress{IPPrefix: p})
}
return c.sendCapsule(ctx, capsule)
}
func (c *Conn) sendCapsule(ctx context.Context, capsule appendable) error {
res := make(chan error, 1)
select {
case c.writes <- writeCapsule{
capsule: capsule,
result: res,
}:
select {
case <-ctx.Done():
return ctx.Err()
case err := <-res:
return err
}
case <-c.closeChan:
return c.closeErr
case <-ctx.Done():
return ctx.Err()
}
}
// LocalPrefixes returns the prefixes that the peer currently assigned.
// Note that at any point during the connection, the peer can change the assignment.
// It is therefore recommended to call this function in a loop.
func (c *Conn) LocalPrefixes(ctx context.Context) ([]netip.Prefix, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-c.closeChan:
return nil, c.closeErr
case <-c.assignedAddressNotify:
c.mu.Lock()
defer c.mu.Unlock()
return c.assignedAddresses, nil
}
}
// Routes returns the routes that the peer currently advertised.
// Note that at any point during the connection, the peer can change the advertised routes.
// It is therefore recommended to call this function in a loop.
func (c *Conn) Routes(ctx context.Context) ([]IPRoute, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-c.closeChan:
return nil, c.closeErr
case <-c.availableRoutesNotify:
c.mu.Lock()
defer c.mu.Unlock()
return c.availableRoutes, nil
}
}
func (c *Conn) readFromStream() error {
defer c.str.Close()
r := quicvarint.NewReader(c.str)
for {
t, cr, err := http3.ParseCapsule(r)
if err != nil {
return err
}
switch t {
case capsuleTypeAddressAssign:
capsule, err := parseAddressAssignCapsule(cr)
if err != nil {
return err
}
prefixes := make([]netip.Prefix, 0, len(capsule.AssignedAddresses))
for _, assigned := range capsule.AssignedAddresses {
prefixes = append(prefixes, assigned.IPPrefix)
}
c.mu.Lock()
c.assignedAddresses = prefixes
c.mu.Unlock()
select {
case c.assignedAddressNotify <- struct{}{}:
default:
}
case capsuleTypeAddressRequest:
if _, err := parseAddressRequestCapsule(cr); err != nil {
return err
}
return errors.New("connect-ip: address request not yet supported")
case capsuleTypeRouteAdvertisement:
capsule, err := parseRouteAdvertisementCapsule(cr)
if err != nil {
return err
}
c.mu.Lock()
c.availableRoutes = capsule.IPAddressRanges
c.mu.Unlock()
select {
case c.availableRoutesNotify <- struct{}{}:
default:
}
default:
return fmt.Errorf("unknown capsule type: %d", t)
}
}
}
func (c *Conn) writeToStream() error {
buf := make([]byte, 0, 1024)
for {
select {
case <-c.closeChan:
return c.closeErr
case req, ok := <-c.writes:
if !ok {
return nil
}
buf = req.capsule.append(buf[:0])
_, err := c.str.Write(buf)
req.result <- err
if err != nil {
return err
}
}
}
}
func (c *Conn) ReadPacket(b []byte) (n int, err error) {
start:
data, err := c.str.ReceiveDatagram(context.Background())
if err != nil {
select {
case <-c.closeChan:
return 0, c.closeErr
default:
return 0, err
}
}
contextID, n, err := quicvarint.Parse(data)
if err != nil {
// TODO: close connection
return 0, fmt.Errorf("connect-ip: malformed datagram: %w", err)
}
if contextID != 0 {
// Drop this datagram. We currently only support proxying of IP payloads.
goto start
}
if err := c.handleIncomingProxiedPacket(data[n:]); err != nil {
log.Printf("dropping proxied packet: %s", err)
goto start
}
return copy(b, data[n:]), nil
}
func (c *Conn) handleIncomingProxiedPacket(data []byte) error {
if len(data) == 0 {
return errors.New("connect-ip: empty packet")
}
var src, dst netip.Addr
var ipProto uint8
switch v := ipVersion(data); v {
default:
return fmt.Errorf("connect-ip: unknown IP versions: %d", v)
case 4:
if len(data) < ipv4.HeaderLen {
return fmt.Errorf("connect-ip: malformed datagram: too short")
}
src = netip.AddrFrom4([4]byte(data[12:16]))
dst = netip.AddrFrom4([4]byte(data[16:20]))
ipProto = data[9]
case 6:
if len(data) < ipv6.HeaderLen {
return fmt.Errorf("connect-ip: malformed datagram: too short")
}
src = netip.AddrFrom16([16]byte(data[8:24]))
dst = netip.AddrFrom16([16]byte(data[24:40]))
ipProto = data[6]
}
c.mu.Lock()
assignedAddresses := c.assignedAddresses
localRoutes := c.localRoutes
peerAddresses := c.peerAddresses
c.mu.Unlock()
// We don't necessarily assign any addresses to the peer.
// For example, in the Remote Access VPN use case (RFC 9484, section 8.1),
// the client accepts incoming traffic from all IPs.
if peerAddresses != nil {
if !slices.ContainsFunc(peerAddresses, func(p netip.Prefix) bool { return p.Contains(src) }) {
// TODO: send ICMP
return fmt.Errorf("connect-ip: datagram source address not allowed: %s", src)
}
}
// The destination IP address is valid if it
// 1. is within one of the ranges assigned to us, or
// 2. is within one of the ranges that we advertised to the peer.
var isAllowedDst bool
if len(assignedAddresses) > 0 {
isAllowedDst = slices.ContainsFunc(assignedAddresses, func(p netip.Prefix) bool { return p.Contains(dst) })
}
if !isAllowedDst {
isAllowedDst = slices.ContainsFunc(localRoutes, func(r IPRoute) bool {
if r.StartIP.Compare(dst) > 0 || dst.Compare(r.EndIP) > 0 {
return false
}
// ICMP is always allowed
if (ipVersion(data) == 4 && ipProto == ipProtoICMP) || (ipVersion(data) == 6 && ipProto == ipProtoICMPv6) {
return true
}
// TODO: walk the chain of IPv6 extensions
// See section 4.8 of RFC 9484 for details.
return r.IPProtocol == 0 || r.IPProtocol == ipProto
})
}
if !isAllowedDst {
// TODO: send ICMP
return fmt.Errorf("connect-ip: datagram destination address / protocol not allowed: %s (protocol: %d)", dst, ipProto)
}
return nil
}
// WritePacket writes an IP packet to the stream.
// If sending the packet fails, it might return an ICMP packet.
// It is the caller's responsibility to send the ICMP packet to the sender.
func (c *Conn) WritePacket(b []byte) (icmp []byte, err error) {
data, err := c.composeDatagram(b)
if err != nil {
log.Printf("dropping proxied packet (%d bytes) that can't be proxied: %s", len(b), err)
return nil, nil
}
if err := c.str.SendDatagram(data); err != nil {
if errors.Is(err, &quic.DatagramTooLargeError{}) {
icmpPacket, err := composeICMPTooLargePacket(b, minMTU)
if err != nil {
log.Printf("failed to compose ICMP too large packet: %s", err)
}
return icmpPacket, nil
}
select {
case <-c.closeChan:
return nil, c.closeErr
default:
return nil, err
}
}
return nil, nil
}
func (c *Conn) composeDatagram(b []byte) ([]byte, error) {
// TODO: implement src, dst and ipproto checks
if len(b) == 0 {
return nil, nil
}
switch v := ipVersion(b); v {
default:
return nil, fmt.Errorf("connect-ip: unknown IP versions: %d", v)
case 4:
if len(b) < ipv4.HeaderLen {
return nil, fmt.Errorf("connect-ip: IPv4 packet too short")
}
ttl := b[8]
if ttl <= 1 {
return nil, fmt.Errorf("connect-ip: datagram TTL too small: %d", ttl)
}
b[8]-- // decrement TTL
// recalculate the checksum
binary.BigEndian.PutUint16(b[10:12], calculateIPv4Checksum(([ipv4.HeaderLen]byte)(b[:ipv4.HeaderLen])))
case 6:
if len(b) < ipv6.HeaderLen {
return nil, fmt.Errorf("connect-ip: IPv6 packet too short")
}
hopLimit := b[7]
if hopLimit <= 1 {
return nil, fmt.Errorf("connect-ip: datagram Hop Limit too small: %d", hopLimit)
}
b[7]-- // Decrement Hop Limit
}
data := make([]byte, 0, len(contextIDZero)+len(b))
data = append(data, contextIDZero...)
data = append(data, b...)
return data, nil
}
func (c *Conn) Close() error {
c.mu.Lock()
if c.closeErr == nil {
c.closeErr = &CloseError{Remote: false}
close(c.closeChan)
}
c.mu.Unlock()
c.str.CancelRead(quic.StreamErrorCode(http3.ErrCodeNoError))
err := c.str.Close()
return err
}
func ipVersion(b []byte) uint8 { return b[0] >> 4 }