ARTEX-English/traffic/transparent_linux.go
Sean Kilfoy edaf7e323a Package BaaRai v0.7.0 as the operator distribution.
The application tree is BaaRai/ARTEX v0.7.0 (22ae88af), imported as one commit because that history is not in this repository. This commit adds the cloud installer, a defender pack for the console and the known-bad image names, and a supply-chain check. The opt-in update banner points at this fork and stays off in the cloud compose file.
2026-11-26 07:33:09 +00:00

58 lines
1.4 KiB
Go

//go:build linux
package traffic
import (
"fmt"
"net"
"syscall"
"unsafe"
)
const (
transparentTCPSupported = true
// soOriginalDst is Linux SO_ORIGINAL_DST from <linux/netfilter_ipv4.h>.
soOriginalDst = 80
)
// originalDstIPv4 recovers the pre-REDIRECT IPv4 destination of an accepted
// Linux TCP connection using getsockopt(SOL_IP, SO_ORIGINAL_DST).
func originalDstIPv4(conn net.Conn) (net.IP, int, error) {
if ta, ok := conn.LocalAddr().(*net.TCPAddr); ok && ta.IP != nil && ta.IP.To4() == nil {
return nil, 0, ErrTransparentTCPIPv6Unsupported
}
sc, ok := conn.(syscall.Conn)
if !ok {
return nil, 0, fmt.Errorf("connection %T does not expose syscall.Conn", conn)
}
rc, err := sc.SyscallConn()
if err != nil {
return nil, 0, fmt.Errorf("syscall conn: %w", err)
}
var raw [sockaddrInSize]byte
optlen := uint32(len(raw))
var sockErr error
if err := rc.Control(func(fd uintptr) {
_, _, errno := syscall.Syscall6(
syscall.SYS_GETSOCKOPT,
fd,
uintptr(syscall.SOL_IP),
uintptr(soOriginalDst),
uintptr(unsafe.Pointer(&raw[0])),
uintptr(unsafe.Pointer(&optlen)),
0,
)
if errno != 0 {
sockErr = errno
}
}); err != nil {
return nil, 0, fmt.Errorf("control fd: %w", err)
}
if sockErr != nil {
return nil, 0, fmt.Errorf("getsockopt(SO_ORIGINAL_DST): %w", sockErr)
}
if int(optlen) > len(raw) {
optlen = uint32(len(raw))
}
return parseOriginalDstIPv4(raw[:optlen])
}