2018-09-06 04:06:57 -04:00
|
|
|
package internet
|
|
|
|
|
2018-09-10 07:23:27 -04:00
|
|
|
import (
|
2018-11-21 08:54:40 -05:00
|
|
|
"net"
|
2018-09-10 07:23:27 -04:00
|
|
|
"syscall"
|
2020-02-24 22:58:49 -05:00
|
|
|
|
2019-10-13 00:12:49 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2018-09-10 07:23:27 -04:00
|
|
|
)
|
2018-09-06 04:06:57 -04:00
|
|
|
|
2018-09-10 07:23:27 -04:00
|
|
|
const (
|
|
|
|
// For incoming connections.
|
|
|
|
TCP_FASTOPEN = 23
|
|
|
|
// For out-going connections.
|
|
|
|
TCP_FASTOPEN_CONNECT = 30
|
|
|
|
)
|
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
func bindAddr(fd uintptr, ip []byte, port uint32) error {
|
2020-07-30 13:20:12 -04:00
|
|
|
setReuseAddr(fd)
|
|
|
|
setReusePort(fd)
|
2019-10-13 00:12:49 -04:00
|
|
|
|
2018-09-17 09:12:58 -04:00
|
|
|
var sockaddr syscall.Sockaddr
|
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
switch len(ip) {
|
|
|
|
case net.IPv4len:
|
2018-09-17 09:12:58 -04:00
|
|
|
a4 := &syscall.SockaddrInet4{
|
|
|
|
Port: int(port),
|
|
|
|
}
|
2018-11-21 08:54:40 -05:00
|
|
|
copy(a4.Addr[:], ip)
|
2018-09-17 09:12:58 -04:00
|
|
|
sockaddr = a4
|
2018-11-21 08:54:40 -05:00
|
|
|
case net.IPv6len:
|
2018-09-17 09:12:58 -04:00
|
|
|
a6 := &syscall.SockaddrInet6{
|
|
|
|
Port: int(port),
|
|
|
|
}
|
2018-11-21 08:54:40 -05:00
|
|
|
copy(a6.Addr[:], ip)
|
2018-09-17 09:12:58 -04:00
|
|
|
sockaddr = a6
|
|
|
|
default:
|
2018-11-21 08:54:40 -05:00
|
|
|
return newError("unexpected length of ip")
|
2018-09-17 09:12:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return syscall.Bind(int(fd), sockaddr)
|
|
|
|
}
|
|
|
|
|
2018-09-10 07:23:27 -04:00
|
|
|
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
|
2018-09-06 04:06:57 -04:00
|
|
|
if config.Mark != 0 {
|
2018-09-06 04:18:11 -04:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(config.Mark)); err != nil {
|
2018-09-14 05:05:42 -04:00
|
|
|
return newError("failed to set SO_MARK").Base(err)
|
2018-09-06 04:06:57 -04:00
|
|
|
}
|
|
|
|
}
|
2018-09-10 07:23:27 -04:00
|
|
|
|
2018-09-11 04:03:26 -04:00
|
|
|
if isTCPSocket(network) {
|
2018-09-10 07:23:27 -04:00
|
|
|
switch config.Tfo {
|
|
|
|
case SocketConfig_Enable:
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN_CONNECT, 1); err != nil {
|
2018-09-14 05:05:42 -04:00
|
|
|
return newError("failed to set TCP_FASTOPEN_CONNECT=1").Base(err)
|
2018-09-10 07:23:27 -04:00
|
|
|
}
|
|
|
|
case SocketConfig_Disable:
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN_CONNECT, 0); err != nil {
|
2018-09-14 05:05:42 -04:00
|
|
|
return newError("failed to set TCP_FASTOPEN_CONNECT=0").Base(err)
|
2018-09-10 07:23:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 09:12:58 -04:00
|
|
|
if config.Tproxy.IsEnabled() {
|
2018-09-14 05:05:42 -04:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_TRANSPARENT, 1); err != nil {
|
|
|
|
return newError("failed to set IP_TRANSPARENT").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 07:23:27 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
|
2019-01-25 02:53:24 -05:00
|
|
|
if config.Mark != 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(config.Mark)); err != nil {
|
|
|
|
return newError("failed to set SO_MARK").Base(err)
|
|
|
|
}
|
|
|
|
}
|
2018-09-11 04:03:26 -04:00
|
|
|
if isTCPSocket(network) {
|
2018-09-10 07:23:27 -04:00
|
|
|
switch config.Tfo {
|
|
|
|
case SocketConfig_Enable:
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, 1); err != nil {
|
2018-09-14 05:05:42 -04:00
|
|
|
return newError("failed to set TCP_FASTOPEN=1").Base(err)
|
2018-09-10 07:23:27 -04:00
|
|
|
}
|
|
|
|
case SocketConfig_Disable:
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, 0); err != nil {
|
2018-09-14 05:05:42 -04:00
|
|
|
return newError("failed to set TCP_FASTOPEN=0").Base(err)
|
2018-09-10 07:23:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 09:12:58 -04:00
|
|
|
if config.Tproxy.IsEnabled() {
|
2018-09-14 05:05:42 -04:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_TRANSPARENT, 1); err != nil {
|
|
|
|
return newError("failed to set IP_TRANSPARENT").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 09:12:58 -04:00
|
|
|
if config.ReceiveOriginalDestAddress && isUDPSocket(network) {
|
2020-02-28 23:08:02 -05:00
|
|
|
err1 := syscall.SetsockoptInt(int(fd), syscall.SOL_IPV6, unix.IPV6_RECVORIGDSTADDR, 1)
|
|
|
|
err2 := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_RECVORIGDSTADDR, 1)
|
|
|
|
if err1 != nil && err2 != nil {
|
|
|
|
return err1
|
2018-09-17 09:12:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 04:06:57 -04:00
|
|
|
return nil
|
|
|
|
}
|
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, unix.SO_REUSEPORT, 1); err != nil {
|
|
|
|
return newError("failed to set SO_REUSEPORT").Base(err).AtWarning()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|