2019-01-02 01:55:51 +01:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-05-03 15:04:00 +02:00
|
|
|
*
|
2021-01-28 17:52:15 +01:00
|
|
|
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
|
2017-09-24 21:35:25 +02:00
|
|
|
*/
|
|
|
|
|
2019-11-07 17:13:05 +01:00
|
|
|
package conn
|
2017-08-25 14:53:23 +02:00
|
|
|
|
|
|
|
import (
|
2017-09-24 21:35:25 +02:00
|
|
|
"errors"
|
2017-08-25 14:53:23 +02:00
|
|
|
"net"
|
2021-11-05 01:52:54 +01:00
|
|
|
"net/netip"
|
2017-09-24 21:35:25 +02:00
|
|
|
"strconv"
|
2018-05-20 03:37:33 +02:00
|
|
|
"sync"
|
2018-06-11 19:04:38 +02:00
|
|
|
"syscall"
|
2017-09-24 21:35:25 +02:00
|
|
|
"unsafe"
|
2019-05-14 09:09:52 +02:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2017-08-25 14:53:23 +02:00
|
|
|
)
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
type ipv4Source struct {
|
2019-11-07 17:13:05 +01:00
|
|
|
Src [4]byte
|
|
|
|
Ifindex int32
|
2018-04-20 04:05:11 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
type ipv6Source struct {
|
2018-04-20 04:05:11 +02:00
|
|
|
src [16]byte
|
2021-02-22 02:01:50 +01:00
|
|
|
// ifindex belongs in dst.ZoneId
|
2018-04-20 04:05:11 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
type LinuxSocketEndpoint struct {
|
2021-03-06 17:20:46 +01:00
|
|
|
mu sync.Mutex
|
2018-04-20 04:05:11 +02:00
|
|
|
dst [unsafe.Sizeof(unix.SockaddrInet6{})]byte
|
2021-02-22 02:01:50 +01:00
|
|
|
src [unsafe.Sizeof(ipv6Source{})]byte
|
2018-04-20 04:05:11 +02:00
|
|
|
isV6 bool
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func (endpoint *LinuxSocketEndpoint) Src4() *ipv4Source { return endpoint.src4() }
|
|
|
|
func (endpoint *LinuxSocketEndpoint) Dst4() *unix.SockaddrInet4 { return endpoint.dst4() }
|
|
|
|
func (endpoint *LinuxSocketEndpoint) IsV6() bool { return endpoint.isV6 }
|
2019-11-07 17:13:05 +01:00
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func (endpoint *LinuxSocketEndpoint) src4() *ipv4Source {
|
|
|
|
return (*ipv4Source)(unsafe.Pointer(&endpoint.src[0]))
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
2017-10-07 22:35:23 +02:00
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func (endpoint *LinuxSocketEndpoint) src6() *ipv6Source {
|
|
|
|
return (*ipv6Source)(unsafe.Pointer(&endpoint.src[0]))
|
2018-04-20 04:05:11 +02:00
|
|
|
}
|
2017-11-18 23:34:02 +01:00
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func (endpoint *LinuxSocketEndpoint) dst4() *unix.SockaddrInet4 {
|
2018-04-20 04:05:11 +02:00
|
|
|
return (*unix.SockaddrInet4)(unsafe.Pointer(&endpoint.dst[0]))
|
2017-11-18 23:34:02 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func (endpoint *LinuxSocketEndpoint) dst6() *unix.SockaddrInet6 {
|
2018-04-20 04:05:11 +02:00
|
|
|
return (*unix.SockaddrInet6)(unsafe.Pointer(&endpoint.dst[0]))
|
2017-11-11 15:43:55 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
// LinuxSocketBind uses sendmsg and recvmsg to implement a full bind with sticky sockets on Linux.
|
|
|
|
type LinuxSocketBind struct {
|
2021-03-31 22:55:18 +02:00
|
|
|
// mu guards sock4 and sock6 and the associated fds.
|
|
|
|
// As long as someone holds mu (read or write), the associated fds are valid.
|
|
|
|
mu sync.RWMutex
|
|
|
|
sock4 int
|
|
|
|
sock6 int
|
2017-11-11 15:43:55 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func NewLinuxSocketBind() Bind { return &LinuxSocketBind{sock4: -1, sock6: -1} }
|
|
|
|
func NewDefaultBind() Bind { return NewLinuxSocketBind() }
|
|
|
|
|
|
|
|
var _ Endpoint = (*LinuxSocketEndpoint)(nil)
|
|
|
|
var _ Bind = (*LinuxSocketBind)(nil)
|
2018-04-20 04:05:11 +02:00
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func (*LinuxSocketBind) ParseEndpoint(s string) (Endpoint, error) {
|
|
|
|
var end LinuxSocketEndpoint
|
2021-11-05 01:52:54 +01:00
|
|
|
e, err := netip.ParseAddrPort(s)
|
2017-11-19 00:21:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-05 01:52:54 +01:00
|
|
|
if e.Addr().Is4() {
|
2018-04-20 04:05:11 +02:00
|
|
|
dst := end.dst4()
|
|
|
|
end.isV6 = false
|
2021-11-05 01:52:54 +01:00
|
|
|
dst.Port = int(e.Port())
|
|
|
|
dst.Addr = e.Addr().As4()
|
2017-11-19 00:21:58 +01:00
|
|
|
end.ClearSrc()
|
|
|
|
return &end, nil
|
|
|
|
}
|
|
|
|
|
2021-11-05 01:52:54 +01:00
|
|
|
if e.Addr().Is6() {
|
|
|
|
zone, err := zoneToUint32(e.Addr().Zone())
|
2017-11-19 00:21:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-20 04:05:11 +02:00
|
|
|
dst := end.dst6()
|
|
|
|
end.isV6 = true
|
2021-11-05 01:52:54 +01:00
|
|
|
dst.Port = int(e.Port())
|
2018-04-20 04:05:11 +02:00
|
|
|
dst.ZoneId = zone
|
2021-11-05 01:52:54 +01:00
|
|
|
dst.Addr = e.Addr().As16()
|
2017-11-19 00:21:58 +01:00
|
|
|
end.ClearSrc()
|
|
|
|
return &end, nil
|
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
return nil, errors.New("invalid IP address")
|
2017-11-18 23:34:02 +01:00
|
|
|
}
|
|
|
|
|
2021-03-31 22:55:18 +02:00
|
|
|
func (bind *LinuxSocketBind) Open(port uint16) ([]ReceiveFunc, uint16, error) {
|
|
|
|
bind.mu.Lock()
|
|
|
|
defer bind.mu.Unlock()
|
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
var err error
|
2018-09-16 15:05:08 +02:00
|
|
|
var newPort uint16
|
2021-02-09 18:45:12 +01:00
|
|
|
var tries int
|
2021-02-22 02:01:50 +01:00
|
|
|
|
|
|
|
if bind.sock4 != -1 || bind.sock6 != -1 {
|
2021-03-31 22:55:18 +02:00
|
|
|
return nil, 0, ErrBindAlreadyOpen
|
2021-02-22 02:01:50 +01:00
|
|
|
}
|
|
|
|
|
2021-02-09 18:45:12 +01:00
|
|
|
originalPort := port
|
2017-10-08 22:03:32 +02:00
|
|
|
|
2021-02-09 18:45:12 +01:00
|
|
|
again:
|
|
|
|
port = originalPort
|
2021-03-31 22:55:18 +02:00
|
|
|
var sock4, sock6 int
|
2019-11-07 17:13:05 +01:00
|
|
|
// Attempt ipv6 bind, update port if successful.
|
2021-03-31 22:55:18 +02:00
|
|
|
sock6, newPort, err = create6(port)
|
2018-09-16 15:05:08 +02:00
|
|
|
if err != nil {
|
2021-03-31 22:55:18 +02:00
|
|
|
if !errors.Is(err, syscall.EAFNOSUPPORT) {
|
|
|
|
return nil, 0, err
|
2018-09-16 15:05:08 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
port = newPort
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2019-11-07 17:13:05 +01:00
|
|
|
// Attempt ipv4 bind, update port if successful.
|
2021-03-31 22:55:18 +02:00
|
|
|
sock4, newPort, err = create4(port)
|
2018-09-16 15:05:08 +02:00
|
|
|
if err != nil {
|
2021-03-31 22:55:18 +02:00
|
|
|
if originalPort == 0 && errors.Is(err, syscall.EADDRINUSE) && tries < 100 {
|
|
|
|
unix.Close(sock6)
|
2021-02-09 18:45:12 +01:00
|
|
|
tries++
|
|
|
|
goto again
|
|
|
|
}
|
2021-03-31 22:55:18 +02:00
|
|
|
if !errors.Is(err, syscall.EAFNOSUPPORT) {
|
|
|
|
unix.Close(sock6)
|
|
|
|
return nil, 0, err
|
2018-09-16 15:05:08 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
port = newPort
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
2018-09-16 15:05:08 +02:00
|
|
|
|
2021-03-31 22:55:18 +02:00
|
|
|
var fns []ReceiveFunc
|
|
|
|
if sock4 != -1 {
|
|
|
|
bind.sock4 = sock4
|
2021-05-20 18:09:55 +02:00
|
|
|
fns = append(fns, bind.receiveIPv4)
|
2021-03-31 22:55:18 +02:00
|
|
|
}
|
|
|
|
if sock6 != -1 {
|
|
|
|
bind.sock6 = sock6
|
2021-05-20 18:09:55 +02:00
|
|
|
fns = append(fns, bind.receiveIPv6)
|
2021-03-31 22:55:18 +02:00
|
|
|
}
|
|
|
|
if len(fns) == 0 {
|
|
|
|
return nil, 0, syscall.EAFNOSUPPORT
|
2018-09-16 15:05:08 +02:00
|
|
|
}
|
2021-03-31 22:55:18 +02:00
|
|
|
return fns, port, nil
|
2019-11-07 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func (bind *LinuxSocketBind) SetMark(value uint32) error {
|
2021-03-31 22:55:18 +02:00
|
|
|
bind.mu.RLock()
|
|
|
|
defer bind.mu.RUnlock()
|
2021-01-07 17:00:21 +01:00
|
|
|
|
2018-06-11 19:04:38 +02:00
|
|
|
if bind.sock6 != -1 {
|
|
|
|
err := unix.SetsockoptInt(
|
|
|
|
bind.sock6,
|
|
|
|
unix.SOL_SOCKET,
|
|
|
|
unix.SO_MARK,
|
|
|
|
int(value),
|
|
|
|
)
|
2017-10-08 22:03:32 +02:00
|
|
|
|
2018-06-11 19:04:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2018-06-11 19:04:38 +02:00
|
|
|
if bind.sock4 != -1 {
|
|
|
|
err := unix.SetsockoptInt(
|
|
|
|
bind.sock4,
|
|
|
|
unix.SOL_SOCKET,
|
|
|
|
unix.SO_MARK,
|
|
|
|
int(value),
|
|
|
|
)
|
2018-04-27 05:21:45 +02:00
|
|
|
|
2018-06-11 19:04:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-27 05:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func (bind *LinuxSocketBind) Close() error {
|
2021-03-31 22:55:18 +02:00
|
|
|
// Take a readlock to shut down the sockets...
|
|
|
|
bind.mu.RLock()
|
2021-01-07 17:00:21 +01:00
|
|
|
if bind.sock6 != -1 {
|
|
|
|
unix.Shutdown(bind.sock6, unix.SHUT_RDWR)
|
|
|
|
}
|
|
|
|
if bind.sock4 != -1 {
|
|
|
|
unix.Shutdown(bind.sock4, unix.SHUT_RDWR)
|
|
|
|
}
|
2021-03-31 22:55:18 +02:00
|
|
|
bind.mu.RUnlock()
|
|
|
|
// ...and a write lock to close the fd.
|
|
|
|
// This ensures that no one else is using the fd.
|
|
|
|
bind.mu.Lock()
|
|
|
|
defer bind.mu.Unlock()
|
|
|
|
var err1, err2 error
|
2018-06-11 19:04:38 +02:00
|
|
|
if bind.sock6 != -1 {
|
2021-01-07 17:00:21 +01:00
|
|
|
err1 = unix.Close(bind.sock6)
|
|
|
|
bind.sock6 = -1
|
2018-06-11 19:04:38 +02:00
|
|
|
}
|
|
|
|
if bind.sock4 != -1 {
|
2021-01-07 17:00:21 +01:00
|
|
|
err2 = unix.Close(bind.sock4)
|
|
|
|
bind.sock4 = -1
|
2018-06-11 19:04:38 +02:00
|
|
|
}
|
2018-05-14 14:08:03 +02:00
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
2019-11-07 17:13:05 +01:00
|
|
|
return err2
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 18:09:55 +02:00
|
|
|
func (bind *LinuxSocketBind) receiveIPv4(buf []byte) (int, Endpoint, error) {
|
|
|
|
bind.mu.RLock()
|
|
|
|
defer bind.mu.RUnlock()
|
|
|
|
if bind.sock4 == -1 {
|
|
|
|
return 0, nil, net.ErrClosed
|
2018-06-11 19:04:38 +02:00
|
|
|
}
|
2021-05-20 18:09:55 +02:00
|
|
|
var end LinuxSocketEndpoint
|
|
|
|
n, err := receive4(bind.sock4, buf, &end)
|
|
|
|
return n, &end, err
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 18:09:55 +02:00
|
|
|
func (bind *LinuxSocketBind) receiveIPv6(buf []byte) (int, Endpoint, error) {
|
|
|
|
bind.mu.RLock()
|
|
|
|
defer bind.mu.RUnlock()
|
|
|
|
if bind.sock6 == -1 {
|
|
|
|
return 0, nil, net.ErrClosed
|
2018-06-11 19:04:38 +02:00
|
|
|
}
|
2021-05-20 18:09:55 +02:00
|
|
|
var end LinuxSocketEndpoint
|
|
|
|
n, err := receive6(bind.sock6, buf, &end)
|
|
|
|
return n, &end, err
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func (bind *LinuxSocketBind) Send(buff []byte, end Endpoint) error {
|
|
|
|
nend, ok := end.(*LinuxSocketEndpoint)
|
|
|
|
if !ok {
|
|
|
|
return ErrWrongEndpointType
|
|
|
|
}
|
2021-03-31 22:55:18 +02:00
|
|
|
bind.mu.RLock()
|
|
|
|
defer bind.mu.RUnlock()
|
2018-04-20 04:05:11 +02:00
|
|
|
if !nend.isV6 {
|
2018-06-11 19:04:38 +02:00
|
|
|
if bind.sock4 == -1 {
|
2021-02-16 21:05:25 +01:00
|
|
|
return net.ErrClosed
|
2018-06-11 19:04:38 +02:00
|
|
|
}
|
2017-11-18 23:34:02 +01:00
|
|
|
return send4(bind.sock4, nend, buff)
|
2018-04-20 04:05:11 +02:00
|
|
|
} else {
|
2018-06-11 19:04:38 +02:00
|
|
|
if bind.sock6 == -1 {
|
2021-02-16 21:05:25 +01:00
|
|
|
return net.ErrClosed
|
2018-06-11 19:04:38 +02:00
|
|
|
}
|
2018-04-20 04:05:11 +02:00
|
|
|
return send6(bind.sock6, nend, buff)
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 01:52:54 +01:00
|
|
|
func (end *LinuxSocketEndpoint) SrcIP() netip.Addr {
|
2018-04-20 04:05:11 +02:00
|
|
|
if !end.isV6 {
|
2021-11-05 01:52:54 +01:00
|
|
|
return netip.AddrFrom4(end.src4().Src)
|
2018-04-20 04:05:11 +02:00
|
|
|
} else {
|
2021-11-05 01:52:54 +01:00
|
|
|
return netip.AddrFrom16(end.src6().src)
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 01:52:54 +01:00
|
|
|
func (end *LinuxSocketEndpoint) DstIP() netip.Addr {
|
2018-04-20 04:05:11 +02:00
|
|
|
if !end.isV6 {
|
2021-11-05 01:52:54 +01:00
|
|
|
return netip.AddrFrom4(end.src4().Src)
|
2018-04-20 04:05:11 +02:00
|
|
|
} else {
|
2021-11-05 01:52:54 +01:00
|
|
|
return netip.AddrFrom16(end.dst6().Addr)
|
2018-04-20 04:05:11 +02:00
|
|
|
}
|
2017-11-18 23:34:02 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func (end *LinuxSocketEndpoint) DstToBytes() []byte {
|
2018-04-20 04:05:11 +02:00
|
|
|
if !end.isV6 {
|
|
|
|
return (*[unsafe.Offsetof(end.dst4().Addr) + unsafe.Sizeof(end.dst4().Addr)]byte)(unsafe.Pointer(end.dst4()))[:]
|
|
|
|
} else {
|
|
|
|
return (*[unsafe.Offsetof(end.dst6().Addr) + unsafe.Sizeof(end.dst6().Addr)]byte)(unsafe.Pointer(end.dst6()))[:]
|
|
|
|
}
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func (end *LinuxSocketEndpoint) SrcToString() string {
|
2018-04-20 04:05:11 +02:00
|
|
|
return end.SrcIP().String()
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func (end *LinuxSocketEndpoint) DstToString() string {
|
2021-11-05 01:52:54 +01:00
|
|
|
var port int
|
2018-04-20 04:05:11 +02:00
|
|
|
if !end.isV6 {
|
2021-11-05 01:52:54 +01:00
|
|
|
port = end.dst4().Port
|
2018-04-20 04:05:11 +02:00
|
|
|
} else {
|
2021-11-05 01:52:54 +01:00
|
|
|
port = end.dst6().Port
|
2018-04-20 04:05:11 +02:00
|
|
|
}
|
2021-11-05 01:52:54 +01:00
|
|
|
return netip.AddrPortFrom(end.DstIP(), uint16(port)).String()
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func (end *LinuxSocketEndpoint) ClearDst() {
|
2018-04-20 04:05:11 +02:00
|
|
|
for i := range end.dst {
|
|
|
|
end.dst[i] = 0
|
|
|
|
}
|
2017-10-16 21:33:47 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func (end *LinuxSocketEndpoint) ClearSrc() {
|
2018-04-20 04:05:11 +02:00
|
|
|
for i := range end.src {
|
|
|
|
end.src[i] = 0
|
|
|
|
}
|
2017-10-07 22:35:23 +02:00
|
|
|
}
|
2017-10-06 22:56:01 +02:00
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
func create4(port uint16) (int, uint16, error) {
|
2017-10-06 22:56:01 +02:00
|
|
|
|
|
|
|
// create socket
|
|
|
|
|
|
|
|
fd, err := unix.Socket(
|
|
|
|
unix.AF_INET,
|
|
|
|
unix.SOCK_DGRAM,
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-01-07 17:00:21 +01:00
|
|
|
return -1, 0, err
|
2017-10-07 22:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
addr := unix.SockaddrInet4{
|
|
|
|
Port: int(port),
|
2017-10-06 22:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set sockopts and bind
|
|
|
|
|
|
|
|
if err := func() error {
|
|
|
|
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)
|
2021-01-07 17:00:21 +01:00
|
|
|
return -1, 0, err
|
2017-10-06 22:56:01 +02:00
|
|
|
}
|
|
|
|
|
2019-08-25 20:45:13 +02:00
|
|
|
sa, err := unix.Getsockname(fd)
|
|
|
|
if err == nil {
|
|
|
|
addr.Port = sa.(*unix.SockaddrInet4).Port
|
|
|
|
}
|
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
return fd, uint16(addr.Port), err
|
2017-10-06 22:56:01 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
func create6(port uint16) (int, uint16, error) {
|
2017-10-06 22:56:01 +02:00
|
|
|
|
|
|
|
// create socket
|
|
|
|
|
|
|
|
fd, err := unix.Socket(
|
2017-10-27 10:43:37 +02:00
|
|
|
unix.AF_INET6,
|
2017-10-06 22:56:01 +02:00
|
|
|
unix.SOCK_DGRAM,
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-01-07 17:00:21 +01:00
|
|
|
return -1, 0, err
|
2017-10-06 22:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set sockopts and bind
|
|
|
|
|
2017-10-07 22:35:23 +02:00
|
|
|
addr := unix.SockaddrInet6{
|
|
|
|
Port: int(port),
|
|
|
|
}
|
|
|
|
|
2017-10-06 22:56:01 +02:00
|
|
|
if err := func() error {
|
|
|
|
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)
|
2021-01-07 17:00:21 +01:00
|
|
|
return -1, 0, err
|
2017-10-06 22:56:01 +02:00
|
|
|
}
|
|
|
|
|
2019-08-25 20:45:13 +02:00
|
|
|
sa, err := unix.Getsockname(fd)
|
|
|
|
if err == nil {
|
|
|
|
addr.Port = sa.(*unix.SockaddrInet6).Port
|
|
|
|
}
|
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
return fd, uint16(addr.Port), err
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func send4(sock int, end *LinuxSocketEndpoint, buff []byte) error {
|
2017-09-24 21:35:25 +02:00
|
|
|
|
2017-10-06 22:56:01 +02:00
|
|
|
// construct message header
|
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
cmsg := struct {
|
|
|
|
cmsghdr unix.Cmsghdr
|
2018-04-20 04:05:11 +02:00
|
|
|
pktinfo unix.Inet4Pktinfo
|
2017-09-24 21:35:25 +02:00
|
|
|
}{
|
|
|
|
unix.Cmsghdr{
|
2018-04-20 04:05:11 +02:00
|
|
|
Level: unix.IPPROTO_IP,
|
|
|
|
Type: unix.IP_PKTINFO,
|
|
|
|
Len: unix.SizeofInet4Pktinfo + unix.SizeofCmsghdr,
|
2017-09-24 21:35:25 +02:00
|
|
|
},
|
2018-04-20 04:05:11 +02:00
|
|
|
unix.Inet4Pktinfo{
|
2019-11-07 17:13:05 +01:00
|
|
|
Spec_dst: end.src4().Src,
|
|
|
|
Ifindex: end.src4().Ifindex,
|
2017-09-24 21:35:25 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-06 17:20:46 +01:00
|
|
|
end.mu.Lock()
|
2018-04-20 04:05:11 +02:00
|
|
|
_, err := unix.SendmsgN(sock, buff, (*[unsafe.Sizeof(cmsg)]byte)(unsafe.Pointer(&cmsg))[:], end.dst4(), 0)
|
2021-03-06 17:20:46 +01:00
|
|
|
end.mu.Unlock()
|
2017-10-27 10:43:37 +02:00
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
if err == nil {
|
2017-10-27 10:43:37 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear src and retry
|
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
if err == unix.EINVAL {
|
2017-09-24 21:35:25 +02:00
|
|
|
end.ClearSrc()
|
2018-04-20 04:05:11 +02:00
|
|
|
cmsg.pktinfo = unix.Inet4Pktinfo{}
|
2021-03-06 17:20:46 +01:00
|
|
|
end.mu.Lock()
|
2018-04-20 04:05:11 +02:00
|
|
|
_, err = unix.SendmsgN(sock, buff, (*[unsafe.Sizeof(cmsg)]byte)(unsafe.Pointer(&cmsg))[:], end.dst4(), 0)
|
2021-03-06 17:20:46 +01:00
|
|
|
end.mu.Unlock()
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
2017-10-27 10:43:37 +02:00
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
return err
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func send6(sock int, end *LinuxSocketEndpoint, buff []byte) error {
|
2017-09-24 21:35:25 +02:00
|
|
|
|
2017-10-06 22:56:01 +02:00
|
|
|
// construct message header
|
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
cmsg := struct {
|
|
|
|
cmsghdr unix.Cmsghdr
|
2018-04-20 04:05:11 +02:00
|
|
|
pktinfo unix.Inet6Pktinfo
|
2017-09-24 21:35:25 +02:00
|
|
|
}{
|
|
|
|
unix.Cmsghdr{
|
2018-04-20 04:05:11 +02:00
|
|
|
Level: unix.IPPROTO_IPV6,
|
|
|
|
Type: unix.IPV6_PKTINFO,
|
|
|
|
Len: unix.SizeofInet6Pktinfo + unix.SizeofCmsghdr,
|
2017-09-24 21:35:25 +02:00
|
|
|
},
|
2018-04-20 04:05:11 +02:00
|
|
|
unix.Inet6Pktinfo{
|
|
|
|
Addr: end.src6().src,
|
|
|
|
Ifindex: end.dst6().ZoneId,
|
2017-09-24 21:35:25 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
if cmsg.pktinfo.Addr == [16]byte{} {
|
|
|
|
cmsg.pktinfo.Ifindex = 0
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
2017-10-17 16:50:23 +02:00
|
|
|
|
2021-03-06 17:20:46 +01:00
|
|
|
end.mu.Lock()
|
2018-04-20 04:05:11 +02:00
|
|
|
_, err := unix.SendmsgN(sock, buff, (*[unsafe.Sizeof(cmsg)]byte)(unsafe.Pointer(&cmsg))[:], end.dst6(), 0)
|
2021-03-06 17:20:46 +01:00
|
|
|
end.mu.Unlock()
|
2017-10-17 16:50:23 +02:00
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
2017-10-17 16:50:23 +02:00
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
// clear src and retry
|
2017-11-11 15:43:55 +01:00
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
if err == unix.EINVAL {
|
|
|
|
end.ClearSrc()
|
|
|
|
cmsg.pktinfo = unix.Inet6Pktinfo{}
|
2021-03-06 17:20:46 +01:00
|
|
|
end.mu.Lock()
|
2018-04-20 04:05:11 +02:00
|
|
|
_, err = unix.SendmsgN(sock, buff, (*[unsafe.Sizeof(cmsg)]byte)(unsafe.Pointer(&cmsg))[:], end.dst6(), 0)
|
2021-03-06 17:20:46 +01:00
|
|
|
end.mu.Unlock()
|
2017-11-11 15:43:55 +01:00
|
|
|
}
|
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
return err
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func receive4(sock int, buff []byte, end *LinuxSocketEndpoint) (int, error) {
|
2017-09-24 21:35:25 +02:00
|
|
|
|
2019-10-21 22:52:26 +02:00
|
|
|
// construct message header
|
2017-10-06 22:56:01 +02:00
|
|
|
|
|
|
|
var cmsg struct {
|
|
|
|
cmsghdr unix.Cmsghdr
|
|
|
|
pktinfo unix.Inet4Pktinfo
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
size, _, _, newDst, err := unix.Recvmsg(sock, buff, (*[unsafe.Sizeof(cmsg)]byte)(unsafe.Pointer(&cmsg))[:], 0)
|
2017-10-06 22:56:01 +02:00
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
end.isV6 = false
|
2017-10-06 22:56:01 +02:00
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
if newDst4, ok := newDst.(*unix.SockaddrInet4); ok {
|
|
|
|
*end.dst4() = *newDst4
|
2017-10-06 22:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// update source cache
|
|
|
|
|
|
|
|
if cmsg.cmsghdr.Level == unix.IPPROTO_IP &&
|
|
|
|
cmsg.cmsghdr.Type == unix.IP_PKTINFO &&
|
|
|
|
cmsg.cmsghdr.Len >= unix.SizeofInet4Pktinfo {
|
2019-11-07 17:13:05 +01:00
|
|
|
end.src4().Src = cmsg.pktinfo.Spec_dst
|
|
|
|
end.src4().Ifindex = cmsg.pktinfo.Ifindex
|
2017-10-06 22:56:01 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
return size, nil
|
2017-10-06 22:56:01 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
func receive6(sock int, buff []byte, end *LinuxSocketEndpoint) (int, error) {
|
2017-10-06 22:56:01 +02:00
|
|
|
|
2019-10-21 22:52:26 +02:00
|
|
|
// construct message header
|
2017-10-06 22:56:01 +02:00
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
var cmsg struct {
|
|
|
|
cmsghdr unix.Cmsghdr
|
2017-10-06 22:56:01 +02:00
|
|
|
pktinfo unix.Inet6Pktinfo
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
size, _, _, newDst, err := unix.Recvmsg(sock, buff, (*[unsafe.Sizeof(cmsg)]byte)(unsafe.Pointer(&cmsg))[:], 0)
|
2017-09-24 21:35:25 +02:00
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
end.isV6 = true
|
2017-09-24 21:35:25 +02:00
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
if newDst6, ok := newDst.(*unix.SockaddrInet6); ok {
|
|
|
|
*end.dst6() = *newDst6
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
|
|
|
|
2017-10-06 22:56:01 +02:00
|
|
|
// update source cache
|
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
if cmsg.cmsghdr.Level == unix.IPPROTO_IPV6 &&
|
|
|
|
cmsg.cmsghdr.Type == unix.IPV6_PKTINFO &&
|
|
|
|
cmsg.cmsghdr.Len >= unix.SizeofInet6Pktinfo {
|
2018-04-20 04:05:11 +02:00
|
|
|
end.src6().src = cmsg.pktinfo.Addr
|
|
|
|
end.dst6().ZoneId = cmsg.pktinfo.Ifindex
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
return size, nil
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|