2018-05-26 02:59:26 +02:00
|
|
|
// +build !android
|
|
|
|
|
2019-01-02 01:55:51 +01:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-05-03 15:04:00 +02:00
|
|
|
*
|
2019-01-02 01:55:51 +01:00
|
|
|
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
|
2017-09-24 21:35:25 +02:00
|
|
|
*
|
|
|
|
* This implements userspace semantics of "sticky sockets", modeled after
|
2018-04-20 04:05:11 +02:00
|
|
|
* WireGuard's kernelspace implementation. This is more or less a straight port
|
|
|
|
* of the sticky-sockets.c example code:
|
|
|
|
* https://git.zx2c4.com/WireGuard/tree/contrib/examples/sticky-sockets/sticky-sockets.c
|
|
|
|
*
|
|
|
|
* 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 remains platform dependent.
|
2017-09-24 21:35:25 +02:00
|
|
|
*/
|
|
|
|
|
2017-08-25 14:53:23 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-09-24 21:35:25 +02:00
|
|
|
"errors"
|
2019-02-18 04:44:41 +01:00
|
|
|
"golang.zx2c4.com/wireguard/rwcancel"
|
2017-08-25 14:53:23 +02:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"net"
|
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"
|
2017-08-25 14:53:23 +02:00
|
|
|
)
|
|
|
|
|
2018-09-16 15:05:08 +02:00
|
|
|
const (
|
|
|
|
FD_ERR = -1
|
|
|
|
)
|
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
type IPv4Source struct {
|
|
|
|
src [4]byte
|
|
|
|
ifindex int32
|
|
|
|
}
|
|
|
|
|
|
|
|
type IPv6Source struct {
|
|
|
|
src [16]byte
|
|
|
|
//ifindex belongs in dst.ZoneId
|
|
|
|
}
|
|
|
|
|
2017-11-18 23:34:02 +01:00
|
|
|
type NativeEndpoint struct {
|
2018-04-20 04:05:11 +02:00
|
|
|
dst [unsafe.Sizeof(unix.SockaddrInet6{})]byte
|
|
|
|
src [unsafe.Sizeof(IPv6Source{})]byte
|
|
|
|
isV6 bool
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
func (endpoint *NativeEndpoint) 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
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
func (endpoint *NativeEndpoint) src6() *IPv6Source {
|
|
|
|
return (*IPv6Source)(unsafe.Pointer(&endpoint.src[0]))
|
|
|
|
}
|
2017-11-18 23:34:02 +01:00
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
func (endpoint *NativeEndpoint) dst4() *unix.SockaddrInet4 {
|
|
|
|
return (*unix.SockaddrInet4)(unsafe.Pointer(&endpoint.dst[0]))
|
2017-11-18 23:34:02 +01:00
|
|
|
}
|
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
func (endpoint *NativeEndpoint) dst6() *unix.SockaddrInet6 {
|
|
|
|
return (*unix.SockaddrInet6)(unsafe.Pointer(&endpoint.dst[0]))
|
2017-11-11 15:43:55 +01:00
|
|
|
}
|
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
type NativeBind struct {
|
2018-05-26 02:59:26 +02:00
|
|
|
sock4 int
|
|
|
|
sock6 int
|
|
|
|
netlinkSock int
|
|
|
|
netlinkCancel *rwcancel.RWCancel
|
|
|
|
lastMark uint32
|
2017-11-11 15:43:55 +01:00
|
|
|
}
|
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
var _ Endpoint = (*NativeEndpoint)(nil)
|
2018-04-27 05:21:45 +02:00
|
|
|
var _ Bind = (*NativeBind)(nil)
|
2018-04-20 04:05:11 +02:00
|
|
|
|
2017-11-19 00:21:58 +01:00
|
|
|
func CreateEndpoint(s string) (Endpoint, error) {
|
|
|
|
var end NativeEndpoint
|
|
|
|
addr, err := parseEndpoint(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ipv4 := addr.IP.To4()
|
|
|
|
if ipv4 != nil {
|
2018-04-20 04:05:11 +02:00
|
|
|
dst := end.dst4()
|
|
|
|
end.isV6 = false
|
|
|
|
dst.Port = addr.Port
|
2017-11-19 00:21:58 +01:00
|
|
|
copy(dst.Addr[:], ipv4)
|
|
|
|
end.ClearSrc()
|
|
|
|
return &end, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ipv6 := addr.IP.To16()
|
|
|
|
if ipv6 != nil {
|
|
|
|
zone, err := zoneToUint32(addr.Zone)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-20 04:05:11 +02:00
|
|
|
dst := end.dst6()
|
|
|
|
end.isV6 = true
|
|
|
|
dst.Port = addr.Port
|
|
|
|
dst.ZoneId = zone
|
2017-11-19 00:21:58 +01:00
|
|
|
copy(dst.Addr[:], ipv6[:])
|
|
|
|
end.ClearSrc()
|
|
|
|
return &end, nil
|
|
|
|
}
|
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
return nil, errors.New("Invalid IP address")
|
2017-11-18 23:34:02 +01:00
|
|
|
}
|
|
|
|
|
2018-04-27 05:21:45 +02:00
|
|
|
func createNetlinkRouteSocket() (int, error) {
|
|
|
|
sock, err := unix.Socket(unix.AF_NETLINK, unix.SOCK_RAW, unix.NETLINK_ROUTE)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
saddr := &unix.SockaddrNetlink{
|
|
|
|
Family: unix.AF_NETLINK,
|
|
|
|
Groups: uint32(1 << (unix.RTNLGRP_IPV4_ROUTE - 1)),
|
|
|
|
}
|
|
|
|
err = unix.Bind(sock, saddr)
|
|
|
|
if err != nil {
|
|
|
|
unix.Close(sock)
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return sock, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-14 03:00:40 +02:00
|
|
|
func CreateBind(port uint16, device *Device) (*NativeBind, uint16, error) {
|
2017-10-08 22:03:32 +02:00
|
|
|
var err error
|
2017-10-17 16:50:23 +02:00
|
|
|
var bind NativeBind
|
2018-09-16 15:05:08 +02:00
|
|
|
var newPort uint16
|
2017-10-08 22:03:32 +02:00
|
|
|
|
2018-04-27 05:21:45 +02:00
|
|
|
bind.netlinkSock, err = createNetlinkRouteSocket()
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2018-05-14 14:08:03 +02:00
|
|
|
bind.netlinkCancel, err = rwcancel.NewRWCancel(bind.netlinkSock)
|
|
|
|
if err != nil {
|
|
|
|
unix.Close(bind.netlinkSock)
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2018-04-27 05:21:45 +02:00
|
|
|
|
2018-05-14 03:00:40 +02:00
|
|
|
go bind.routineRouteListener(device)
|
2018-04-27 05:21:45 +02:00
|
|
|
|
2018-09-16 15:05:08 +02:00
|
|
|
// attempt ipv6 bind, update port if succesful
|
|
|
|
|
|
|
|
bind.sock6, newPort, err = create6(port)
|
|
|
|
if err != nil {
|
|
|
|
if err != syscall.EAFNOSUPPORT {
|
|
|
|
bind.netlinkCancel.Cancel()
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
port = newPort
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2018-09-16 15:05:08 +02:00
|
|
|
// attempt ipv4 bind, update port if succesful
|
|
|
|
|
|
|
|
bind.sock4, newPort, err = create4(port)
|
|
|
|
if err != nil {
|
|
|
|
if err != syscall.EAFNOSUPPORT {
|
|
|
|
bind.netlinkCancel.Cancel()
|
|
|
|
unix.Close(bind.sock6)
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
port = newPort
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
2018-09-16 15:05:08 +02:00
|
|
|
|
|
|
|
if bind.sock4 == FD_ERR && bind.sock6 == FD_ERR {
|
|
|
|
return nil, 0, errors.New("ipv4 and ipv6 not supported")
|
|
|
|
}
|
|
|
|
|
2018-06-11 19:04:38 +02:00
|
|
|
return &bind, port, nil
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2018-04-27 05:21:45 +02:00
|
|
|
func (bind *NativeBind) SetMark(value uint32) error {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bind.lastMark = value
|
|
|
|
return nil
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2017-11-11 23:26:44 +01:00
|
|
|
func closeUnblock(fd int) error {
|
2018-05-14 03:00:40 +02:00
|
|
|
// shutdown to unblock readers and writers
|
|
|
|
unix.Shutdown(fd, unix.SHUT_RDWR)
|
2017-11-11 23:26:44 +01:00
|
|
|
return unix.Close(fd)
|
|
|
|
}
|
|
|
|
|
2018-04-27 05:21:45 +02:00
|
|
|
func (bind *NativeBind) Close() error {
|
2018-06-11 19:04:38 +02:00
|
|
|
var err1, err2, err3 error
|
|
|
|
if bind.sock6 != -1 {
|
|
|
|
err1 = closeUnblock(bind.sock6)
|
|
|
|
}
|
|
|
|
if bind.sock4 != -1 {
|
|
|
|
err2 = closeUnblock(bind.sock4)
|
|
|
|
}
|
|
|
|
err3 = bind.netlinkCancel.Cancel()
|
2018-05-14 14:08:03 +02:00
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
2018-04-27 05:21:45 +02:00
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
return err3
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2018-04-27 05:21:45 +02:00
|
|
|
func (bind *NativeBind) ReceiveIPv6(buff []byte) (int, Endpoint, error) {
|
2017-11-18 23:34:02 +01:00
|
|
|
var end NativeEndpoint
|
2018-06-11 19:04:38 +02:00
|
|
|
if bind.sock6 == -1 {
|
|
|
|
return 0, nil, syscall.EAFNOSUPPORT
|
|
|
|
}
|
2017-11-18 23:34:02 +01:00
|
|
|
n, err := receive6(
|
2017-10-08 22:03:32 +02:00
|
|
|
bind.sock6,
|
|
|
|
buff,
|
2017-11-18 23:34:02 +01:00
|
|
|
&end,
|
2017-10-08 22:03:32 +02:00
|
|
|
)
|
2017-11-18 23:34:02 +01:00
|
|
|
return n, &end, err
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2018-04-27 05:21:45 +02:00
|
|
|
func (bind *NativeBind) ReceiveIPv4(buff []byte) (int, Endpoint, error) {
|
2017-11-18 23:34:02 +01:00
|
|
|
var end NativeEndpoint
|
2018-06-11 19:04:38 +02:00
|
|
|
if bind.sock4 == -1 {
|
|
|
|
return 0, nil, syscall.EAFNOSUPPORT
|
|
|
|
}
|
2017-11-18 23:34:02 +01:00
|
|
|
n, err := receive4(
|
2017-10-08 22:03:32 +02:00
|
|
|
bind.sock4,
|
|
|
|
buff,
|
2017-11-18 23:34:02 +01:00
|
|
|
&end,
|
2017-10-08 22:03:32 +02:00
|
|
|
)
|
2017-11-18 23:34:02 +01:00
|
|
|
return n, &end, err
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 14:14:09 +01:00
|
|
|
func (bind *NativeBind) Send(buff []byte, end Endpoint, tos byte) error {
|
2017-11-18 23:34:02 +01:00
|
|
|
nend := end.(*NativeEndpoint)
|
2018-04-20 04:05:11 +02:00
|
|
|
if !nend.isV6 {
|
2018-06-11 19:04:38 +02:00
|
|
|
if bind.sock4 == -1 {
|
|
|
|
return syscall.EAFNOSUPPORT
|
|
|
|
}
|
2019-02-23 14:14:09 +01:00
|
|
|
return send4(bind.sock4, nend, buff, tos)
|
2018-04-20 04:05:11 +02:00
|
|
|
} else {
|
2018-06-11 19:04:38 +02:00
|
|
|
if bind.sock6 == -1 {
|
|
|
|
return syscall.EAFNOSUPPORT
|
|
|
|
}
|
2019-02-23 14:14:09 +01:00
|
|
|
return send6(bind.sock6, nend, buff, tos)
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
func (end *NativeEndpoint) SrcIP() net.IP {
|
|
|
|
if !end.isV6 {
|
2017-10-08 22:03:32 +02:00
|
|
|
return net.IPv4(
|
2018-04-20 04:05:11 +02:00
|
|
|
end.src4().src[0],
|
|
|
|
end.src4().src[1],
|
|
|
|
end.src4().src[2],
|
|
|
|
end.src4().src[3],
|
2017-10-08 22:03:32 +02:00
|
|
|
)
|
2018-04-20 04:05:11 +02:00
|
|
|
} else {
|
|
|
|
return end.src6().src[:]
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-18 23:34:02 +01:00
|
|
|
func (end *NativeEndpoint) DstIP() net.IP {
|
2018-04-20 04:05:11 +02:00
|
|
|
if !end.isV6 {
|
|
|
|
return net.IPv4(
|
|
|
|
end.dst4().Addr[0],
|
|
|
|
end.dst4().Addr[1],
|
|
|
|
end.dst4().Addr[2],
|
|
|
|
end.dst4().Addr[3],
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return end.dst6().Addr[:]
|
|
|
|
}
|
2017-11-18 23:34:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (end *NativeEndpoint) 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
|
|
|
}
|
|
|
|
|
2017-11-18 23:34:02 +01:00
|
|
|
func (end *NativeEndpoint) SrcToString() string {
|
2018-04-20 04:05:11 +02:00
|
|
|
return end.SrcIP().String()
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2017-11-18 23:34:02 +01:00
|
|
|
func (end *NativeEndpoint) DstToString() string {
|
2018-04-20 04:05:11 +02:00
|
|
|
var udpAddr net.UDPAddr
|
|
|
|
udpAddr.IP = end.DstIP()
|
|
|
|
if !end.isV6 {
|
|
|
|
udpAddr.Port = end.dst4().Port
|
|
|
|
} else {
|
|
|
|
udpAddr.Port = end.dst6().Port
|
|
|
|
}
|
|
|
|
return udpAddr.String()
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
|
2017-11-18 23:34:02 +01:00
|
|
|
func (end *NativeEndpoint) 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
|
|
|
}
|
|
|
|
|
2017-11-18 23:34:02 +01:00
|
|
|
func (end *NativeEndpoint) 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 {
|
2018-09-16 15:05:08 +02:00
|
|
|
return FD_ERR, 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.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)
|
2018-09-16 15:05:08 +02:00
|
|
|
return FD_ERR, 0, err
|
2017-10-06 22:56:01 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-09-16 15:05:08 +02:00
|
|
|
return FD_ERR, 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.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)
|
2018-09-16 15:05:08 +02:00
|
|
|
return FD_ERR, 0, err
|
2017-10-06 22:56:01 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
return fd, uint16(addr.Port), err
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 14:14:09 +01:00
|
|
|
func send4(sock int, end *NativeEndpoint, buff []byte, tos byte) error {
|
2017-09-24 21:35:25 +02:00
|
|
|
|
2017-10-06 22:56:01 +02:00
|
|
|
// construct message header
|
2019-02-23 14:14:09 +01:00
|
|
|
type ipTos struct {
|
|
|
|
tos byte
|
|
|
|
}
|
2017-10-06 22:56:01 +02:00
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
cmsg := struct {
|
|
|
|
cmsghdr unix.Cmsghdr
|
2018-04-20 04:05:11 +02:00
|
|
|
pktinfo unix.Inet4Pktinfo
|
2019-02-23 14:14:09 +01:00
|
|
|
cmsghdr2 unix.Cmsghdr
|
|
|
|
iptos ipTos
|
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{
|
|
|
|
Spec_dst: end.src4().src,
|
|
|
|
Ifindex: end.src4().ifindex,
|
2017-09-24 21:35:25 +02:00
|
|
|
},
|
2019-02-23 14:14:09 +01:00
|
|
|
unix.Cmsghdr{
|
|
|
|
Level: unix.IPPROTO_IP,
|
|
|
|
Type: unix.IP_TOS,
|
|
|
|
Len: 1 + unix.SizeofCmsghdr,
|
|
|
|
},
|
|
|
|
ipTos{
|
|
|
|
tos: tos,
|
|
|
|
},
|
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
_, err := unix.SendmsgN(sock, buff, (*[unsafe.Sizeof(cmsg)]byte)(unsafe.Pointer(&cmsg))[:], end.dst4(), 0)
|
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{}
|
|
|
|
_, err = unix.SendmsgN(sock, buff, (*[unsafe.Sizeof(cmsg)]byte)(unsafe.Pointer(&cmsg))[:], end.dst4(), 0)
|
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
|
|
|
}
|
|
|
|
|
2019-02-23 14:14:09 +01:00
|
|
|
func send6(sock int, end *NativeEndpoint, buff []byte, tos byte) error {
|
2017-09-24 21:35:25 +02:00
|
|
|
|
2017-10-06 22:56:01 +02:00
|
|
|
// construct message header
|
2019-02-23 14:14:09 +01:00
|
|
|
type ipTos struct {
|
|
|
|
tos byte
|
|
|
|
}
|
2017-10-06 22:56:01 +02:00
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
cmsg := struct {
|
|
|
|
cmsghdr unix.Cmsghdr
|
2018-04-20 04:05:11 +02:00
|
|
|
pktinfo unix.Inet6Pktinfo
|
2019-02-23 14:14:09 +01:00
|
|
|
cmsghdr2 unix.Cmsghdr
|
|
|
|
tclass ipTos
|
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
|
|
|
},
|
2019-02-23 14:14:09 +01:00
|
|
|
unix.Cmsghdr{
|
|
|
|
Level: unix.IPPROTO_IPV6,
|
|
|
|
Type: unix.IPV6_TCLASS,
|
|
|
|
Len: 1 + unix.SizeofCmsghdr,
|
|
|
|
},
|
|
|
|
ipTos{
|
|
|
|
tos: tos,
|
|
|
|
},
|
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
|
|
|
|
2018-04-20 04:05:11 +02:00
|
|
|
_, err := unix.SendmsgN(sock, buff, (*[unsafe.Sizeof(cmsg)]byte)(unsafe.Pointer(&cmsg))[:], end.dst6(), 0)
|
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{}
|
|
|
|
_, err = unix.SendmsgN(sock, buff, (*[unsafe.Sizeof(cmsg)]byte)(unsafe.Pointer(&cmsg))[:], end.dst6(), 0)
|
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
|
|
|
}
|
|
|
|
|
2017-11-18 23:34:02 +01:00
|
|
|
func receive4(sock int, buff []byte, end *NativeEndpoint) (int, error) {
|
2017-09-24 21:35:25 +02:00
|
|
|
|
2017-10-06 22:56:01 +02:00
|
|
|
// contruct message header
|
|
|
|
|
|
|
|
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 {
|
2018-04-20 04:05:11 +02: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
|
|
|
}
|
|
|
|
|
2017-11-18 23:34:02 +01:00
|
|
|
func receive6(sock int, buff []byte, end *NativeEndpoint) (int, error) {
|
2017-10-06 22:56:01 +02:00
|
|
|
|
|
|
|
// contruct message header
|
|
|
|
|
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
|
|
|
}
|
2018-04-27 05:21:45 +02:00
|
|
|
|
2018-05-14 03:00:40 +02:00
|
|
|
func (bind *NativeBind) routineRouteListener(device *Device) {
|
2018-05-14 14:18:26 +02:00
|
|
|
type peerEndpointPtr struct {
|
|
|
|
peer *Peer
|
|
|
|
endpoint *Endpoint
|
|
|
|
}
|
|
|
|
var reqPeer map[uint32]peerEndpointPtr
|
2018-05-20 03:37:33 +02:00
|
|
|
var reqPeerLock sync.Mutex
|
2018-04-27 05:21:45 +02:00
|
|
|
|
2018-05-14 14:08:03 +02:00
|
|
|
defer unix.Close(bind.netlinkSock)
|
|
|
|
|
2018-04-27 05:21:45 +02:00
|
|
|
for msg := make([]byte, 1<<16); ; {
|
2018-05-14 14:08:03 +02:00
|
|
|
var err error
|
|
|
|
var msgn int
|
|
|
|
for {
|
|
|
|
msgn, _, _, _, err = unix.Recvmsg(bind.netlinkSock, msg[:], nil, 0)
|
2018-05-24 15:29:16 +02:00
|
|
|
if err == nil || !rwcancel.RetryAfterError(err) {
|
2018-05-14 14:08:03 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if !bind.netlinkCancel.ReadyRead() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-04-27 05:21:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for remain := msg[:msgn]; len(remain) >= unix.SizeofNlMsghdr; {
|
|
|
|
|
|
|
|
hdr := *(*unix.NlMsghdr)(unsafe.Pointer(&remain[0]))
|
|
|
|
|
|
|
|
if uint(hdr.Len) > uint(len(remain)) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
switch hdr.Type {
|
|
|
|
case unix.RTM_NEWROUTE, unix.RTM_DELROUTE:
|
2018-05-23 18:30:55 +02:00
|
|
|
if hdr.Seq <= MaxPeers && hdr.Seq > 0 {
|
2018-04-27 05:21:45 +02:00
|
|
|
if uint(len(remain)) < uint(hdr.Len) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if hdr.Len > unix.SizeofNlMsghdr+unix.SizeofRtMsg {
|
|
|
|
attr := remain[unix.SizeofNlMsghdr+unix.SizeofRtMsg:]
|
|
|
|
for {
|
|
|
|
if uint(len(attr)) < uint(unix.SizeofRtAttr) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
attrhdr := *(*unix.RtAttr)(unsafe.Pointer(&attr[0]))
|
|
|
|
if attrhdr.Len < unix.SizeofRtAttr || uint(len(attr)) < uint(attrhdr.Len) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if attrhdr.Type == unix.RTA_OIF && attrhdr.Len == unix.SizeofRtAttr+4 {
|
|
|
|
ifidx := *(*uint32)(unsafe.Pointer(&attr[unix.SizeofRtAttr]))
|
2018-05-20 03:37:33 +02:00
|
|
|
reqPeerLock.Lock()
|
2018-05-14 03:00:40 +02:00
|
|
|
if reqPeer == nil {
|
2018-05-20 03:37:33 +02:00
|
|
|
reqPeerLock.Unlock()
|
2018-05-14 03:00:40 +02:00
|
|
|
break
|
|
|
|
}
|
2018-05-14 14:18:26 +02:00
|
|
|
pePtr, ok := reqPeer[hdr.Seq]
|
2018-05-20 03:37:33 +02:00
|
|
|
reqPeerLock.Unlock()
|
2018-05-14 03:00:40 +02:00
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
2019-01-03 19:04:00 +01:00
|
|
|
pePtr.peer.Lock()
|
2018-05-14 14:18:26 +02:00
|
|
|
if &pePtr.peer.endpoint != pePtr.endpoint {
|
2019-01-03 19:04:00 +01:00
|
|
|
pePtr.peer.Unlock()
|
2018-05-14 03:00:40 +02:00
|
|
|
break
|
2018-04-27 05:21:45 +02:00
|
|
|
}
|
2018-05-14 14:18:26 +02:00
|
|
|
if uint32(pePtr.peer.endpoint.(*NativeEndpoint).src4().ifindex) == ifidx {
|
2019-01-03 19:04:00 +01:00
|
|
|
pePtr.peer.Unlock()
|
2018-05-14 03:00:40 +02:00
|
|
|
break
|
|
|
|
}
|
2018-05-14 14:18:26 +02:00
|
|
|
pePtr.peer.endpoint.(*NativeEndpoint).ClearSrc()
|
2019-01-03 19:04:00 +01:00
|
|
|
pePtr.peer.Unlock()
|
2018-04-27 05:21:45 +02:00
|
|
|
}
|
|
|
|
attr = attr[attrhdr.Len:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2018-05-20 03:37:33 +02:00
|
|
|
reqPeerLock.Lock()
|
2018-05-14 14:18:26 +02:00
|
|
|
reqPeer = make(map[uint32]peerEndpointPtr)
|
2018-05-20 03:37:33 +02:00
|
|
|
reqPeerLock.Unlock()
|
2018-05-14 03:00:40 +02:00
|
|
|
go func() {
|
2019-01-03 19:04:00 +01:00
|
|
|
device.peers.RLock()
|
2018-05-14 03:00:40 +02:00
|
|
|
i := uint32(1)
|
|
|
|
for _, peer := range device.peers.keyMap {
|
2019-01-03 19:04:00 +01:00
|
|
|
peer.RLock()
|
2018-05-14 03:00:40 +02:00
|
|
|
if peer.endpoint == nil || peer.endpoint.(*NativeEndpoint) == nil {
|
2019-01-03 19:04:00 +01:00
|
|
|
peer.RUnlock()
|
2018-05-14 03:00:40 +02:00
|
|
|
continue
|
|
|
|
}
|
2018-05-14 12:27:29 +02:00
|
|
|
if peer.endpoint.(*NativeEndpoint).isV6 || peer.endpoint.(*NativeEndpoint).src4().ifindex == 0 {
|
2019-01-03 19:04:00 +01:00
|
|
|
peer.RUnlock()
|
2018-05-14 12:27:29 +02:00
|
|
|
break
|
|
|
|
}
|
2018-05-14 03:00:40 +02:00
|
|
|
nlmsg := struct {
|
|
|
|
hdr unix.NlMsghdr
|
|
|
|
msg unix.RtMsg
|
|
|
|
dsthdr unix.RtAttr
|
|
|
|
dst [4]byte
|
|
|
|
srchdr unix.RtAttr
|
|
|
|
src [4]byte
|
|
|
|
markhdr unix.RtAttr
|
|
|
|
mark uint32
|
|
|
|
}{
|
|
|
|
unix.NlMsghdr{
|
|
|
|
Type: uint16(unix.RTM_GETROUTE),
|
|
|
|
Flags: unix.NLM_F_REQUEST,
|
|
|
|
Seq: i,
|
|
|
|
},
|
|
|
|
unix.RtMsg{
|
|
|
|
Family: unix.AF_INET,
|
|
|
|
Dst_len: 32,
|
|
|
|
Src_len: 32,
|
|
|
|
},
|
|
|
|
unix.RtAttr{
|
|
|
|
Len: 8,
|
|
|
|
Type: unix.RTA_DST,
|
|
|
|
},
|
|
|
|
peer.endpoint.(*NativeEndpoint).dst4().Addr,
|
|
|
|
unix.RtAttr{
|
|
|
|
Len: 8,
|
|
|
|
Type: unix.RTA_SRC,
|
|
|
|
},
|
|
|
|
peer.endpoint.(*NativeEndpoint).src4().src,
|
|
|
|
unix.RtAttr{
|
|
|
|
Len: 8,
|
|
|
|
Type: 0x10, //unix.RTA_MARK TODO: add this to x/sys/unix
|
|
|
|
},
|
|
|
|
uint32(bind.lastMark),
|
|
|
|
}
|
|
|
|
nlmsg.hdr.Len = uint32(unsafe.Sizeof(nlmsg))
|
2018-05-20 03:37:33 +02:00
|
|
|
reqPeerLock.Lock()
|
2018-05-14 14:18:26 +02:00
|
|
|
reqPeer[i] = peerEndpointPtr{
|
|
|
|
peer: peer,
|
|
|
|
endpoint: &peer.endpoint,
|
|
|
|
}
|
2018-05-20 03:37:33 +02:00
|
|
|
reqPeerLock.Unlock()
|
2019-01-03 19:04:00 +01:00
|
|
|
peer.RUnlock()
|
2018-05-14 03:00:40 +02:00
|
|
|
i++
|
2018-05-14 14:18:26 +02:00
|
|
|
_, err := bind.netlinkCancel.Write((*[unsafe.Sizeof(nlmsg)]byte)(unsafe.Pointer(&nlmsg))[:])
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2018-05-14 03:00:40 +02:00
|
|
|
}
|
2019-01-03 19:04:00 +01:00
|
|
|
device.peers.RUnlock()
|
2018-05-14 03:00:40 +02:00
|
|
|
}()
|
2018-04-27 05:21:45 +02:00
|
|
|
}
|
|
|
|
remain = remain[hdr.Len:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|