Skip to content

Commit d8527b5

Browse files
lubingtanygao-g
andcommitted
atunnel: support IPv6 original destination lookup
TCPOriginalDestination read only the IPv4 SOL_IP/SO_ORIGINAL_DST, so an actor's IPv6 connection redirected into the transparent egress listener had no destination to dial and the proxy failed it. Read IP6T_SO_ORIGINAL_DST too, falling back to it only when the IPv4 lookup returns ENOENT, so unrelated IPv4 failures keep their own error. One step towards dual-stack actor networking; the actor veth and its nftables rules are still IPv4-only. Co-authored-by: Yuan Gao <[email protected]>
1 parent 43fa6cc commit d8527b5

2 files changed

Lines changed: 444 additions & 19 deletions

File tree

‎internal/atunnel/original_dst_linux.go‎

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package atunnel
1818

1919
import (
2020
"encoding/binary"
21+
"errors"
2122
"fmt"
2223
"net"
2324
"strconv"
@@ -26,10 +27,12 @@ import (
2627
"golang.org/x/sys/unix"
2728
)
2829

29-
// TCPOriginalDestination reads the IPv4 destination preserved by a Linux
30-
// REDIRECT rule. Actor networking is currently IPv4-only.
31-
// TODO(liorlieberman) add the IPv6 IP6T_SO_ORIGINAL_DST variant
32-
// when actor veth setup gains dual-stack support.
30+
// IP6T_SO_ORIGINAL_DST is not generated by golang.org/x/sys/unix. It is
31+
// defined as 80 in linux/netfilter_ipv6/ip6_tables.h.
32+
const ip6tSOOriginalDst = 80
33+
34+
// TCPOriginalDestination reads the IPv4 or IPv6 destination preserved by a
35+
// Linux REDIRECT rule.
3336
func TCPOriginalDestination(conn net.Conn) (string, error) {
3437
tcpConn, ok := conn.(*net.TCPConn)
3538
if !ok {
@@ -40,33 +43,60 @@ func TCPOriginalDestination(conn net.Conn) (string, error) {
4043
return "", fmt.Errorf("atunnel: acquiring TCP syscall connection: %w", err)
4144
}
4245

43-
var addr unix.RawSockaddrInet4
4446
var sockoptErr error
47+
var destination string
4548
if err := rawConn.Control(func(fd uintptr) {
46-
size := uint32(unsafe.Sizeof(addr))
47-
_, _, errno := unix.Syscall6(
48-
unix.SYS_GETSOCKOPT,
49-
fd,
50-
unix.SOL_IP,
51-
unix.SO_ORIGINAL_DST,
52-
uintptr(unsafe.Pointer(&addr)),
53-
uintptr(unsafe.Pointer(&size)),
54-
0,
55-
)
56-
if errno != 0 {
57-
sockoptErr = errno
49+
destination, sockoptErr = originalIPv4Destination(fd)
50+
// Linux returns ENOENT when the IPv4 original-destination option is
51+
// queried on a redirected IPv6 connection. Only then try the IPv6
52+
// equivalent, so unrelated IPv4 failures retain their original error.
53+
if errors.Is(sockoptErr, unix.ENOENT) {
54+
destination, sockoptErr = originalIPv6Destination(fd)
5855
}
5956
}); err != nil {
6057
return "", fmt.Errorf("atunnel: accessing TCP socket: %w", err)
6158
}
6259
if sockoptErr != nil {
6360
return "", fmt.Errorf("atunnel: reading original TCP destination: %w", sockoptErr)
6461
}
62+
return destination, nil
63+
}
64+
65+
func originalIPv4Destination(fd uintptr) (string, error) {
66+
var addr unix.RawSockaddrInet4
67+
if errno := getOriginalDestination(fd, unix.SOL_IP, unix.SO_ORIGINAL_DST, unsafe.Pointer(&addr), unsafe.Sizeof(addr)); errno != 0 {
68+
return "", errno
69+
}
70+
return formatOriginalDestination(addr.Addr[:], addr.Port)
71+
}
72+
73+
func originalIPv6Destination(fd uintptr) (string, error) {
74+
var addr unix.RawSockaddrInet6
75+
if errno := getOriginalDestination(fd, unix.SOL_IPV6, ip6tSOOriginalDst, unsafe.Pointer(&addr), unsafe.Sizeof(addr)); errno != 0 {
76+
return "", errno
77+
}
78+
return formatOriginalDestination(addr.Addr[:], addr.Port)
79+
}
80+
81+
func getOriginalDestination(fd uintptr, level, option int, addr unsafe.Pointer, addrSize uintptr) unix.Errno {
82+
size := uint32(addrSize)
83+
_, _, errno := unix.Syscall6(
84+
unix.SYS_GETSOCKOPT,
85+
fd,
86+
uintptr(level),
87+
uintptr(option),
88+
uintptr(addr),
89+
uintptr(unsafe.Pointer(&size)),
90+
0,
91+
)
92+
return errno
93+
}
6594

66-
portBytes := (*[2]byte)(unsafe.Pointer(&addr.Port))
95+
func formatOriginalDestination(ip []byte, rawPort uint16) (string, error) {
96+
portBytes := (*[2]byte)(unsafe.Pointer(&rawPort))
6797
port := binary.BigEndian.Uint16(portBytes[:])
6898
if port == 0 {
6999
return "", fmt.Errorf("atunnel: original TCP destination has port zero")
70100
}
71-
return net.JoinHostPort(net.IP(addr.Addr[:]).String(), strconv.Itoa(int(port))), nil
101+
return net.JoinHostPort(net.IP(ip).String(), strconv.Itoa(int(port))), nil
72102
}

0 commit comments

Comments
 (0)