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.
|
2021-11-27 04:16:41 -05:00
|
|
|
TCP_FASTOPEN = 23 // nolint: revive,stylecheck
|
2018-09-10 07:23:27 -04:00
|
|
|
// For out-going connections.
|
2021-11-27 04:16:41 -05:00
|
|
|
TCP_FASTOPEN_CONNECT = 30 // nolint: revive,stylecheck
|
2018-09-10 07:23:27 -04:00
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-05-04 17:30:41 -04:00
|
|
|
|
2022-01-16 17:05:28 -05:00
|
|
|
if config.TcpKeepAliveInterval > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
|
|
|
|
return newError("failed to set TCP_KEEPINTVL", err)
|
2021-05-04 17:30:41 -04:00
|
|
|
}
|
2022-01-16 17:05:28 -05:00
|
|
|
}
|
|
|
|
if config.TcpKeepAliveIdle > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, int(config.TcpKeepAliveIdle)); err != nil {
|
|
|
|
return newError("failed to set TCP_KEEPIDLE", err)
|
2021-11-13 16:31:49 -05:00
|
|
|
}
|
2022-01-16 17:05:28 -05:00
|
|
|
}
|
|
|
|
if config.TcpKeepAliveInterval > 0 || config.TcpKeepAliveIdle > 0 {
|
2021-11-13 16:31:49 -05:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
2021-12-12 05:10:21 -05:00
|
|
|
return newError("failed to set SO_KEEPALIVE").Base(err)
|
2021-11-13 16:31:49 -05:00
|
|
|
}
|
2021-05-04 17:30:41 -04:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 17:32:23 -05:00
|
|
|
if config.BindToDevice != "" {
|
|
|
|
if err := unix.BindToDevice(int(fd), config.BindToDevice); err != nil {
|
|
|
|
return newError("failed to set SO_BINDTODEVICE").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 18:02:24 -05:00
|
|
|
if config.TxBufSize != 0 {
|
2022-01-16 17:32:23 -05:00
|
|
|
syscallTarget := unix.SO_SNDBUF
|
2022-01-16 18:02:24 -05:00
|
|
|
if config.ForceBufSize {
|
2022-01-16 17:32:23 -05:00
|
|
|
syscallTarget = unix.SO_SNDBUFFORCE
|
|
|
|
}
|
2022-01-16 18:02:24 -05:00
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, syscallTarget, int(config.TxBufSize)); err != nil {
|
2022-01-16 17:32:23 -05:00
|
|
|
return newError("failed to set SO_SNDBUF/SO_SNDBUFFORCE").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 18:02:24 -05:00
|
|
|
if config.RxBufSize != 0 {
|
2022-01-16 17:32:23 -05:00
|
|
|
syscallTarget := unix.SO_RCVBUF
|
2022-01-16 18:02:24 -05:00
|
|
|
if config.ForceBufSize {
|
2022-01-16 17:32:23 -05:00
|
|
|
syscallTarget = unix.SO_RCVBUFFORCE
|
|
|
|
}
|
2022-01-16 18:02:24 -05:00
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, syscallTarget, int(config.RxBufSize)); err != nil {
|
2022-01-16 17:32:23 -05:00
|
|
|
return newError("failed to set SO_RCVBUF/SO_RCVBUFFORCE").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:
|
2021-09-26 12:29:44 -04:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, int(config.TfoQueueLength)); err != nil {
|
|
|
|
return newError("failed to set TCP_FASTOPEN=", config.TfoQueueLength).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
|
|
|
}
|
|
|
|
}
|
2021-05-04 17:39:28 -04:00
|
|
|
|
2022-01-16 17:05:28 -05:00
|
|
|
if config.TcpKeepAliveInterval > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
|
|
|
|
return newError("failed to set TCP_KEEPINTVL", err)
|
2021-05-04 17:39:28 -04:00
|
|
|
}
|
2022-01-16 17:05:28 -05:00
|
|
|
}
|
|
|
|
if config.TcpKeepAliveIdle > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, int(config.TcpKeepAliveIdle)); err != nil {
|
|
|
|
return newError("failed to set TCP_KEEPIDLE", err)
|
2021-11-13 16:31:49 -05:00
|
|
|
}
|
2022-01-16 17:05:28 -05:00
|
|
|
}
|
|
|
|
if config.TcpKeepAliveInterval > 0 || config.TcpKeepAliveIdle > 0 {
|
2021-11-13 16:31:49 -05:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
|
|
|
return newError("failed to set SO_KEEPALIVE", err)
|
|
|
|
}
|
2021-05-04 17:39:28 -04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 17:32:23 -05:00
|
|
|
if config.BindToDevice != "" {
|
|
|
|
if err := unix.BindToDevice(int(fd), config.BindToDevice); err != nil {
|
|
|
|
return newError("failed to set SO_BINDTODEVICE").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 18:02:24 -05:00
|
|
|
if config.TxBufSize != 0 {
|
2022-01-16 17:32:23 -05:00
|
|
|
syscallTarget := unix.SO_SNDBUF
|
2022-01-16 18:02:24 -05:00
|
|
|
if config.ForceBufSize {
|
2022-01-16 17:32:23 -05:00
|
|
|
syscallTarget = unix.SO_SNDBUFFORCE
|
|
|
|
}
|
2022-01-16 18:02:24 -05:00
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, syscallTarget, int(config.TxBufSize)); err != nil {
|
2022-01-16 17:32:23 -05:00
|
|
|
return newError("failed to set SO_SNDBUF/SO_SNDBUFFORCE").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 18:02:24 -05:00
|
|
|
if config.RxBufSize != 0 {
|
2022-01-16 17:32:23 -05:00
|
|
|
syscallTarget := unix.SO_RCVBUF
|
2022-01-16 18:02:24 -05:00
|
|
|
if config.ForceBufSize {
|
2022-01-16 17:32:23 -05:00
|
|
|
syscallTarget = unix.SO_RCVBUFFORCE
|
|
|
|
}
|
2022-01-16 18:02:24 -05:00
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, syscallTarget, int(config.RxBufSize)); err != nil {
|
2022-01-16 17:32:23 -05:00
|
|
|
return newError("failed to set SO_RCVBUF/SO_RCVBUFFORCE").Base(err)
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|