1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-08 19:28:21 -05:00
v2fly/transport/internet/sockopt_linux.go

204 lines
6.3 KiB
Go
Raw Normal View History

2018-09-06 04:06:57 -04:00
package internet
2018-09-10 07:23:27 -04:00
import (
"net"
2018-09-10 07:23:27 -04:00
"syscall"
"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 // nolint: revive,stylecheck
2018-09-10 07:23:27 -04:00
// For out-going connections.
TCP_FASTOPEN_CONNECT = 30 // nolint: revive,stylecheck
2018-09-10 07:23:27 -04:00
)
func bindAddr(fd uintptr, ip []byte, port uint32) error {
setReuseAddr(fd)
setReusePort(fd)
2018-09-17 09:12:58 -04:00
var sockaddr syscall.Sockaddr
switch len(ip) {
case net.IPv4len:
2018-09-17 09:12:58 -04:00
a4 := &syscall.SockaddrInet4{
Port: int(port),
}
copy(a4.Addr[:], ip)
2018-09-17 09:12:58 -04:00
sockaddr = a4
case net.IPv6len:
2018-09-17 09:12:58 -04:00
a6 := &syscall.SockaddrInet6{
Port: int(port),
}
copy(a6.Addr[:], ip)
2018-09-17 09:12:58 -04:00
sockaddr = a6
default:
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)
}
}
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 {
syscallTarget := unix.SO_SNDBUF
2022-01-16 18:02:24 -05:00
if config.ForceBufSize {
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 {
return newError("failed to set SO_SNDBUF/SO_SNDBUFFORCE").Base(err)
}
}
2022-01-16 18:02:24 -05:00
if config.RxBufSize != 0 {
syscallTarget := unix.SO_RCVBUF
2022-01-16 18:02:24 -05:00
if config.ForceBufSize {
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 {
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 {
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, 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) {
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
}
}
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 {
syscallTarget := unix.SO_SNDBUF
2022-01-16 18:02:24 -05:00
if config.ForceBufSize {
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 {
return newError("failed to set SO_SNDBUF/SO_SNDBUFFORCE").Base(err)
}
}
2022-01-16 18:02:24 -05:00
if config.RxBufSize != 0 {
syscallTarget := unix.SO_RCVBUF
2022-01-16 18:02:24 -05:00
if config.ForceBufSize {
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 {
return newError("failed to set SO_RCVBUF/SO_RCVBUFFORCE").Base(err)
}
}
2018-09-06 04:06:57 -04:00
return nil
}
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
}