summaryrefslogtreecommitdiffhomepage
path: root/src/conn_linux.go
blob: 034fb8bfe616f5174be754d8198e86c966732472 (plain)
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* Copyright 2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 *
 * This implements userspace semantics of "sticky sockets", modeled after
 * WireGuard's kernelspace implementation.
 */

package main

import (
	"errors"
	"golang.org/x/sys/unix"
	"net"
	"strconv"
	"unsafe"
)

import "fmt"

/* Supports source address caching
 *
 * Currently there is no way to achieve this within the net package:
 * See e.g. https://github.com/golang/go/issues/17930
 * So this code is platform dependent.
 *
 * It is important that the endpoint is only updated after the packet content has been authenticated!
 */

type Endpoint struct {
	// source (selected based on dst type)
	// (could use RawSockaddrAny and unsafe)
	// TODO: Merge
	src6   unix.RawSockaddrInet6
	src4   unix.RawSockaddrInet4
	src4if int32

	dst unix.RawSockaddrAny
}

type Socket int

/* Returns a byte representation of the source field(s)
 * for use in "under load" cookie computations.
 */
func (endpoint *Endpoint) Source() []byte {
	return nil
}

func zoneToUint32(zone string) (uint32, error) {
	if zone == "" {
		return 0, nil
	}
	if intr, err := net.InterfaceByName(zone); err == nil {
		return uint32(intr.Index), nil
	}
	n, err := strconv.ParseUint(zone, 10, 32)
	return uint32(n), err
}

func CreateIPv4Socket(port uint16) (Socket, uint16, error) {

	// create socket

	fd, err := unix.Socket(
		unix.AF_INET,
		unix.SOCK_DGRAM,
		0,
	)

	if err != nil {
		return -1, 0, err
	}

	addr := unix.SockaddrInet4{
		Port: int(port),
	}

	// set sockopts and bind

	if err := func() error {
		if err := unix.SetsockoptInt(
			fd,
			unix.SOL_SOCKET,
			unix.SO_REUSEADDR,
			1,
		); err != nil {
			return err
		}

		if err := unix.SetsockoptInt(
			fd,
			unix.IPPROTO_IP,
			unix.IP_PKTINFO,
			1,
		); err != nil {
			return err
		}

		return unix.Bind(fd, &addr)
	}(); err != nil {
		unix.Close(fd)
	}

	return Socket(fd), uint16(addr.Port), err
}

func CloseIPv4Socket(sock Socket) error {
	return unix.Close(int(sock))
}

func CloseIPv6Socket(sock Socket) error {
	return unix.Close(int(sock))
}

func CreateIPv6Socket(port uint16) (Socket, uint16, error) {

	// create socket

	fd, err := unix.Socket(
		unix.AF_INET,
		unix.SOCK_DGRAM,
		0,
	)

	if err != nil {
		return -1, 0, err
	}

	// set sockopts and bind

	addr := unix.SockaddrInet6{
		Port: int(port),
	}

	if err := func() error {

		if err := unix.SetsockoptInt(
			fd,
			unix.SOL_SOCKET,
			unix.SO_REUSEADDR,
			1,
		); err != nil {
			return err
		}

		if err := unix.SetsockoptInt(
			fd,
			unix.IPPROTO_IPV6,
			unix.IPV6_RECVPKTINFO,
			1,
		); err != nil {
			return err
		}

		if err := unix.SetsockoptInt(
			fd,
			unix.IPPROTO_IPV6,
			unix.IPV6_V6ONLY,
			1,
		); err != nil {
			return err
		}

		return unix.Bind(fd, &addr)

	}(); err != nil {
		unix.Close(fd)
	}

	return Socket(fd), uint16(addr.Port), err
}

func (end *Endpoint) ClearSrc() {
	end.src4if = 0
	end.src4 = unix.RawSockaddrInet4{}
	end.src6 = unix.RawSockaddrInet6{}
}

func (end *Endpoint) Set(s string) error {
	addr, err := parseEndpoint(s)
	if err != nil {
		return err
	}

	ipv6 := addr.IP.To16()
	if ipv6 != nil {
		zone, err := zoneToUint32(addr.Zone)
		if err != nil {
			return err
		}
		ptr := (*unix.RawSockaddrInet6)(unsafe.Pointer(&end.dst))
		ptr.Family = unix.AF_INET6
		ptr.Port = uint16(addr.Port)
		ptr.Flowinfo = 0
		ptr.Scope_id = zone
		copy(ptr.Addr[:], ipv6[:])
		end.ClearSrc()
		return nil
	}

	ipv4 := addr.IP.To4()
	if ipv4 != nil {
		ptr := (*unix.RawSockaddrInet4)(unsafe.Pointer(&end.dst))
		ptr.Family = unix.AF_INET
		ptr.Port = uint16(addr.Port)
		ptr.Zero = [8]byte{}
		copy(ptr.Addr[:], ipv4)
		end.ClearSrc()
		return nil
	}

	return errors.New("Failed to recognize IP address format")
}

func send6(sock uintptr, end *Endpoint, buff []byte) error {

	// construct message header

	var iovec unix.Iovec
	iovec.Base = (*byte)(unsafe.Pointer(&buff[0]))
	iovec.SetLen(len(buff))

	cmsg := struct {
		cmsghdr unix.Cmsghdr
		pktinfo unix.Inet6Pktinfo
	}{
		unix.Cmsghdr{
			Level: unix.IPPROTO_IPV6,
			Type:  unix.IPV6_PKTINFO,
			Len:   unix.SizeofInet6Pktinfo,
		},
		unix.Inet6Pktinfo{
			Addr:    end.src6.Addr,
			Ifindex: end.src6.Scope_id,
		},
	}

	msghdr := unix.Msghdr{
		Iov:     &iovec,
		Iovlen:  1,
		Name:    (*byte)(unsafe.Pointer(&end.dst)),
		Namelen: unix.SizeofSockaddrInet6,
		Control: (*byte)(unsafe.Pointer(&cmsg)),
	}

	msghdr.SetControllen(int(unsafe.Sizeof(cmsg)))

	// sendmsg(sock, &msghdr, 0)

	_, _, errno := unix.Syscall(
		unix.SYS_SENDMSG,
		sock,
		uintptr(unsafe.Pointer(&msghdr)),
		0,
	)
	if errno == unix.EINVAL {
		end.ClearSrc()
	}
	return errno
}

func send4(sock uintptr, end *Endpoint, buff []byte) error {

	// construct message header

	var iovec unix.Iovec
	iovec.Base = (*byte)(unsafe.Pointer(&buff[0]))
	iovec.SetLen(len(buff))

	cmsg := struct {
		cmsghdr unix.Cmsghdr
		pktinfo unix.Inet4Pktinfo
	}{
		unix.Cmsghdr{
			Level: unix.IPPROTO_IP,
			Type:  unix.IP_PKTINFO,
			Len:   unix.SizeofInet4Pktinfo,
		},
		unix.Inet4Pktinfo{
			Spec_dst: end.src4.Addr,
			Ifindex:  end.src4if,
		},
	}

	msghdr := unix.Msghdr{
		Iov:     &iovec,
		Iovlen:  1,
		Name:    (*byte)(unsafe.Pointer(&end.dst)),
		Namelen: unix.SizeofSockaddrInet4,
		Control: (*byte)(unsafe.Pointer(&cmsg)),
	}

	msghdr.SetControllen(int(unsafe.Sizeof(cmsg)))

	// sendmsg(sock, &msghdr, 0)

	_, _, errno := unix.Syscall(
		unix.SYS_SENDMSG,
		sock,
		uintptr(unsafe.Pointer(&msghdr)),
		0,
	)
	if errno == unix.EINVAL {
		end.ClearSrc()
	}
	return errno
}

func (end *Endpoint) Send(c *net.UDPConn, buff []byte) error {

	// extract underlying file descriptor

	file, err := c.File()
	if err != nil {
		return err
	}
	sock := file.Fd()

	// send depending on address family of dst

	family := *((*uint16)(unsafe.Pointer(&end.dst)))
	if family == unix.AF_INET {
		return send4(sock, end, buff)
	} else if family == unix.AF_INET6 {
		return send6(sock, end, buff)
	}
	return errors.New("Unknown address family of source")
}

func (end *Endpoint) ReceiveIPv4(sock Socket, buff []byte) (int, error) {

	// contruct message header

	var iovec unix.Iovec
	iovec.Base = (*byte)(unsafe.Pointer(&buff[0]))
	iovec.SetLen(len(buff))

	var cmsg struct {
		cmsghdr unix.Cmsghdr
		pktinfo unix.Inet4Pktinfo
	}

	var msghdr unix.Msghdr
	msghdr.Iov = &iovec
	msghdr.Iovlen = 1
	msghdr.Name = (*byte)(unsafe.Pointer(&end.dst))
	msghdr.Namelen = unix.SizeofSockaddrInet4
	msghdr.Control = (*byte)(unsafe.Pointer(&cmsg))
	msghdr.SetControllen(int(unsafe.Sizeof(cmsg)))

	// recvmsg(sock, &mskhdr, 0)

	size, _, errno := unix.Syscall(
		unix.SYS_RECVMSG,
		uintptr(sock),
		uintptr(unsafe.Pointer(&msghdr)),
		0,
	)

	if errno != 0 {
		return 0, errno
	}

	fmt.Println(msghdr)
	fmt.Println(cmsg)

	// update source cache

	if cmsg.cmsghdr.Level == unix.IPPROTO_IP &&
		cmsg.cmsghdr.Type == unix.IP_PKTINFO &&
		cmsg.cmsghdr.Len >= unix.SizeofInet4Pktinfo {
		end.src4.Addr = cmsg.pktinfo.Spec_dst
		end.src4if = cmsg.pktinfo.Ifindex
	}

	return int(size), nil
}

func (end *Endpoint) ReceiveIPv6(sock Socket, buff []byte) (int, error) {

	// contruct message header

	var iovec unix.Iovec
	iovec.Base = (*byte)(unsafe.Pointer(&buff[0]))
	iovec.SetLen(len(buff))

	var cmsg struct {
		cmsghdr unix.Cmsghdr
		pktinfo unix.Inet6Pktinfo
	}

	var msg unix.Msghdr
	msg.Iov = &iovec
	msg.Iovlen = 1
	msg.Name = (*byte)(unsafe.Pointer(&end.dst))
	msg.Namelen = uint32(unix.SizeofSockaddrInet6)
	msg.Control = (*byte)(unsafe.Pointer(&cmsg))
	msg.SetControllen(int(unsafe.Sizeof(cmsg)))

	// recvmsg(sock, &mskhdr, 0)

	size, _, errno := unix.Syscall(
		unix.SYS_RECVMSG,
		uintptr(sock),
		uintptr(unsafe.Pointer(&msg)),
		0,
	)

	if errno != 0 {
		return 0, errno
	}

	// update source cache

	if cmsg.cmsghdr.Level == unix.IPPROTO_IPV6 &&
		cmsg.cmsghdr.Type == unix.IPV6_PKTINFO &&
		cmsg.cmsghdr.Len >= unix.SizeofInet6Pktinfo {
		end.src6.Addr = cmsg.pktinfo.Addr
		end.src6.Scope_id = cmsg.pktinfo.Ifindex
	}

	return int(size), nil
}

func SetMark(sock Socket, value uint32) error {
	return unix.SetsockoptInt(
		int(sock),
		unix.SOL_SOCKET,
		unix.SO_MARK,
		int(value),
	)
}