Skip to content

Commit

Permalink
fqdn proxy: add transparent socket options to IPFamily struct
Browse files Browse the repository at this point in the history
This commit adds the transparent socket options to the IPFamily
struct. This way it can be used in the respective functions.

Signed-off-by: Marco Hofstetter <marco.hofstetter@isovalent.com>
  • Loading branch information
mhofstetter authored and aanm committed Sep 18, 2023
1 parent 63d7db2 commit 7c32923
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
22 changes: 5 additions & 17 deletions pkg/fqdn/dnsproxy/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,11 @@ type sessionUDP struct {
// IP(V6)_RECVORIGDSTADDR tells the kernel to pass the original destination address/port on recvmsg
// By design, a socket of a DNS Server can only receive IPv4 or IPv6 traffic.
func transparentSetsockopt(fd int, ipFamily ipfamily.IPFamily) error {
switch ipFamily {
case ipfamily.IPv4():
if err := unix.SetsockoptInt(fd, unix.SOL_IP, unix.IP_TRANSPARENT, 1); err != nil {
return fmt.Errorf("setsockopt(IP_TRANSPARENT) failed: %w", err)
}
if err := unix.SetsockoptInt(fd, unix.SOL_IP, unix.IP_RECVORIGDSTADDR, 1); err != nil {
return fmt.Errorf("setsockopt(IP_RECVORIGDSTADDR) failed: %w", err)
}
case ipfamily.IPv6():
if err := unix.SetsockoptInt(fd, unix.SOL_IPV6, unix.IPV6_TRANSPARENT, 1); err != nil {
return fmt.Errorf("setsockopt(IPV6_TRANSPARENT) failed: %w", err)
}
if err := unix.SetsockoptInt(fd, unix.SOL_IPV6, unix.IPV6_RECVORIGDSTADDR, 1); err != nil {
return fmt.Errorf("setsockopt(IPV6_RECVORIGDSTADDR) failed: %w", err)
}
default:
return fmt.Errorf("unknown ipfamily: %s", ipFamily.Name)
if err := unix.SetsockoptInt(fd, ipFamily.SocketOptsFamily, ipFamily.SocketOptsTransparent, 1); err != nil {
return fmt.Errorf("setsockopt(IP_TRANSPARENT) for %s failed: %w", ipFamily.Name, err)
}
if err := unix.SetsockoptInt(fd, ipFamily.SocketOptsFamily, ipFamily.SocketOptsRecvOrigDstAddr, 1); err != nil {
return fmt.Errorf("setsockopt(IP_RECVORIGDSTADDR) for %s failed: %w", ipFamily.Name, err)
}

return nil
Expand Down
14 changes: 14 additions & 0 deletions pkg/fqdn/proxy/ipfamily/ipfamily.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@

package ipfamily

import "golang.org/x/sys/unix"

type IPFamily struct {
Name string
UDPAddress string
TCPAddress string
Localhost string

SocketOptsFamily int
SocketOptsTransparent int
SocketOptsRecvOrigDstAddr int
}

func IPv4() IPFamily {
Expand All @@ -16,6 +22,10 @@ func IPv4() IPFamily {
UDPAddress: "udp4",
TCPAddress: "tcp4",
Localhost: "127.0.0.1",

SocketOptsFamily: unix.SOL_IP,
SocketOptsTransparent: unix.IP_TRANSPARENT,
SocketOptsRecvOrigDstAddr: unix.IP_RECVORIGDSTADDR,
}
}

Expand All @@ -25,5 +35,9 @@ func IPv6() IPFamily {
UDPAddress: "udp6",
TCPAddress: "tcp6",
Localhost: "::1",

SocketOptsFamily: unix.SOL_IPV6,
SocketOptsTransparent: unix.IPV6_TRANSPARENT,
SocketOptsRecvOrigDstAddr: unix.IPV6_RECVORIGDSTADDR,
}
}

0 comments on commit 7c32923

Please sign in to comment.