2020-02-13 09:35:11 -05:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2020-02-13 22:00:11 -05:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2020-02-13 09:35:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-02-13 22:00:11 -05:00
|
|
|
sysPFINOUT = 0x0
|
|
|
|
sysPFIN = 0x1
|
|
|
|
sysPFOUT = 0x2
|
|
|
|
sysPFFWD = 0x3
|
|
|
|
sysDIOCNATLOOK = 0xc04c4417
|
2020-02-13 09:35:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type pfiocNatlook struct {
|
|
|
|
Saddr [16]byte /* pf_addr */
|
|
|
|
Daddr [16]byte /* pf_addr */
|
|
|
|
Rsaddr [16]byte /* pf_addr */
|
|
|
|
Rdaddr [16]byte /* pf_addr */
|
|
|
|
Sport uint16
|
|
|
|
Dport uint16
|
|
|
|
Rsport uint16
|
|
|
|
Rdport uint16
|
|
|
|
Af uint8
|
|
|
|
Proto uint8
|
|
|
|
Direction uint8
|
2020-02-13 22:00:11 -05:00
|
|
|
Pad [1]byte
|
2020-02-13 09:35:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
sizeofPfiocNatlook = 0x4c
|
2020-03-23 09:52:19 -04:00
|
|
|
soReUsePort = 0x00000200
|
2020-02-13 22:00:11 -05:00
|
|
|
soReUsePortLB = 0x00010000
|
2020-02-13 09:35:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func ioctl(s uintptr, ioc int, b []byte) error {
|
|
|
|
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, s, uintptr(ioc), uintptr(unsafe.Pointer(&b[0]))); errno != 0 {
|
|
|
|
return error(errno)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (nl *pfiocNatlook) rdPort() int {
|
|
|
|
return int(binary.BigEndian.Uint16((*[2]byte)(unsafe.Pointer(&nl.Rdport))[:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nl *pfiocNatlook) setPort(remote, local int) {
|
|
|
|
binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&nl.Sport))[:], uint16(remote))
|
|
|
|
binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&nl.Dport))[:], uint16(local))
|
|
|
|
}
|
2020-02-13 22:00:11 -05:00
|
|
|
|
2020-02-13 22:11:33 -05:00
|
|
|
// OriginalDst uses ioctl to read original destination from /dev/pf
|
2020-02-13 09:35:11 -05:00
|
|
|
func OriginalDst(la, ra net.Addr) (net.IP, int, error) {
|
|
|
|
f, err := os.Open("/dev/pf")
|
|
|
|
if err != nil {
|
|
|
|
return net.IP{}, -1, newError("failed to open device /dev/pf").Base(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
fd := f.Fd()
|
|
|
|
b := make([]byte, sizeofPfiocNatlook)
|
|
|
|
nl := (*pfiocNatlook)(unsafe.Pointer(&b[0]))
|
|
|
|
var raIP, laIP net.IP
|
|
|
|
var raPort, laPort int
|
|
|
|
switch la.(type) {
|
|
|
|
case *net.TCPAddr:
|
|
|
|
raIP = ra.(*net.TCPAddr).IP
|
|
|
|
laIP = la.(*net.TCPAddr).IP
|
|
|
|
raPort = ra.(*net.TCPAddr).Port
|
|
|
|
laPort = la.(*net.TCPAddr).Port
|
2020-02-13 22:00:11 -05:00
|
|
|
nl.Proto = syscall.IPPROTO_TCP
|
2020-02-13 09:35:11 -05:00
|
|
|
case *net.UDPAddr:
|
|
|
|
raIP = ra.(*net.UDPAddr).IP
|
|
|
|
laIP = la.(*net.UDPAddr).IP
|
|
|
|
raPort = ra.(*net.UDPAddr).Port
|
|
|
|
laPort = la.(*net.UDPAddr).Port
|
2020-02-13 22:00:11 -05:00
|
|
|
nl.Proto = syscall.IPPROTO_UDP
|
2020-02-13 09:35:11 -05:00
|
|
|
}
|
|
|
|
if raIP.To4() != nil {
|
|
|
|
if laIP.IsUnspecified() {
|
|
|
|
laIP = net.ParseIP("127.0.0.1")
|
|
|
|
}
|
|
|
|
copy(nl.Saddr[:net.IPv4len], raIP.To4())
|
|
|
|
copy(nl.Daddr[:net.IPv4len], laIP.To4())
|
2020-02-13 22:00:11 -05:00
|
|
|
nl.Af = syscall.AF_INET
|
2020-02-13 09:35:11 -05:00
|
|
|
}
|
|
|
|
if raIP.To16() != nil && raIP.To4() == nil {
|
|
|
|
if laIP.IsUnspecified() {
|
|
|
|
laIP = net.ParseIP("::1")
|
|
|
|
}
|
|
|
|
copy(nl.Saddr[:], raIP)
|
|
|
|
copy(nl.Daddr[:], laIP)
|
2020-02-13 22:00:11 -05:00
|
|
|
nl.Af = syscall.AF_INET6
|
2020-02-13 09:35:11 -05:00
|
|
|
}
|
|
|
|
nl.setPort(raPort, laPort)
|
|
|
|
ioc := uintptr(sysDIOCNATLOOK)
|
2020-02-13 22:00:11 -05:00
|
|
|
for _, dir := range []byte{sysPFOUT, sysPFIN} {
|
2020-02-13 09:35:11 -05:00
|
|
|
nl.Direction = dir
|
|
|
|
err = ioctl(fd, int(ioc), b)
|
|
|
|
if err == nil || err != syscall.ENOENT {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return net.IP{}, -1, os.NewSyscallError("ioctl", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
odPort := nl.rdPort()
|
|
|
|
var odIP net.IP
|
|
|
|
switch nl.Af {
|
2020-02-13 22:00:11 -05:00
|
|
|
case syscall.AF_INET:
|
2020-02-13 09:35:11 -05:00
|
|
|
odIP = make(net.IP, net.IPv4len)
|
|
|
|
copy(odIP, nl.Rdaddr[:net.IPv4len])
|
2020-02-13 22:00:11 -05:00
|
|
|
case syscall.AF_INET6:
|
2020-02-13 09:35:11 -05:00
|
|
|
odIP = make(net.IP, net.IPv6len)
|
|
|
|
copy(odIP, nl.Rdaddr[:])
|
|
|
|
}
|
|
|
|
return odIP, odPort, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
|
|
|
|
if config.Mark != 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_USER_COOKIE, int(config.Mark)); err != nil {
|
|
|
|
return newError("failed to set SO_USER_COOKIE").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isTCPSocket(network) {
|
|
|
|
switch config.Tfo {
|
|
|
|
case SocketConfig_Enable:
|
2020-02-13 22:00:11 -05:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_FASTOPEN, 1); err != nil {
|
2020-02-13 09:35:11 -05:00
|
|
|
return newError("failed to set TCP_FASTOPEN_CONNECT=1").Base(err)
|
|
|
|
}
|
|
|
|
case SocketConfig_Disable:
|
2020-02-13 22:00:11 -05:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_FASTOPEN, 0); err != nil {
|
2020-02-13 09:35:11 -05:00
|
|
|
return newError("failed to set TCP_FASTOPEN_CONNECT=0").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Tproxy.IsEnabled() {
|
|
|
|
ip, _, _ := net.SplitHostPort(address)
|
|
|
|
if net.ParseIP(ip).To4() != nil {
|
2020-02-13 22:00:11 -05:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_BINDANY, 1); err != nil {
|
2020-02-13 09:35:11 -05:00
|
|
|
return newError("failed to set outbound IP_BINDANY").Base(err)
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-13 22:00:11 -05:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_BINDANY, 1); err != nil {
|
2020-02-13 09:35:11 -05:00
|
|
|
return newError("failed to set outbound IPV6_BINDANY").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
|
|
|
|
if config.Mark != 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_USER_COOKIE, int(config.Mark)); err != nil {
|
|
|
|
return newError("failed to set SO_USER_COOKIE").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isTCPSocket(network) {
|
|
|
|
switch config.Tfo {
|
|
|
|
case SocketConfig_Enable:
|
2020-02-13 22:00:11 -05:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_FASTOPEN, 1); err != nil {
|
2020-02-13 09:35:11 -05:00
|
|
|
return newError("failed to set TCP_FASTOPEN=1").Base(err)
|
|
|
|
}
|
|
|
|
case SocketConfig_Disable:
|
2020-02-13 22:00:11 -05:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_FASTOPEN, 0); err != nil {
|
2020-02-13 09:35:11 -05:00
|
|
|
return newError("failed to set TCP_FASTOPEN=0").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Tproxy.IsEnabled() {
|
2020-02-13 22:00:11 -05:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_BINDANY, 1); err != nil {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_BINDANY, 1); err != nil {
|
2020-02-13 09:35:11 -05:00
|
|
|
return newError("failed to set inbound IP_BINDANY").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func bindAddr(fd uintptr, ip []byte, port uint32) error {
|
2020-07-30 13:20:12 -04:00
|
|
|
setReuseAddr(fd)
|
|
|
|
setReusePort(fd)
|
2020-02-13 09:35:11 -05:00
|
|
|
|
|
|
|
var sockaddr syscall.Sockaddr
|
|
|
|
|
|
|
|
switch len(ip) {
|
|
|
|
case net.IPv4len:
|
|
|
|
a4 := &syscall.SockaddrInet4{
|
|
|
|
Port: int(port),
|
|
|
|
}
|
|
|
|
copy(a4.Addr[:], ip)
|
|
|
|
sockaddr = a4
|
|
|
|
case net.IPv6len:
|
|
|
|
a6 := &syscall.SockaddrInet6{
|
|
|
|
Port: int(port),
|
|
|
|
}
|
|
|
|
copy(a6.Addr[:], ip)
|
|
|
|
sockaddr = a6
|
|
|
|
default:
|
|
|
|
return newError("unexpected length of ip")
|
|
|
|
}
|
|
|
|
|
|
|
|
return syscall.Bind(int(fd), sockaddr)
|
|
|
|
}
|
2020-07-30 13:20:12 -04:00
|
|
|
|
|
|
|
func setReuseAddr(fd uintptr) error {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
|
|
|
|
return newError("failed to set SO_REUSEADDR").Base(err).AtWarning()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setReusePort(fd uintptr) error {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, soReUsePortLB, 1); err != nil {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, soReUsePort, 1); err != nil {
|
|
|
|
return newError("failed to set SO_REUSEPORT").Base(err).AtWarning()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|