forked from riobard/go-shadowsocks2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split Netfilter and pf code into subpackages
- Loading branch information
Showing
6 changed files
with
123 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package nfutil | ||
|
||
import ( | ||
"net" | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
// Get the original destination of a TCP connection redirected by Netfilter. | ||
func GetOrigDst(c *net.TCPConn, ipv6 bool) (*net.TCPAddr, error) { | ||
rc, err := c.SyscallConn() | ||
if err != nil { | ||
return nil, err | ||
} | ||
var addr *net.TCPAddr | ||
rc.Control(func(fd uintptr) { | ||
if ipv6 { | ||
addr, err = ipv6_getorigdst(fd) | ||
} else { | ||
addr, err = getorigdst(fd) | ||
} | ||
}) | ||
return addr, err | ||
} | ||
|
||
// Call getorigdst() from linux/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c | ||
func getorigdst(fd uintptr) (*net.TCPAddr, error) { | ||
const _SO_ORIGINAL_DST = 80 // from linux/include/uapi/linux/netfilter_ipv4.h | ||
var raw syscall.RawSockaddrInet4 | ||
siz := unsafe.Sizeof(raw) | ||
if err := socketcall(GETSOCKOPT, fd, syscall.IPPROTO_IP, _SO_ORIGINAL_DST, uintptr(unsafe.Pointer(&raw)), uintptr(unsafe.Pointer(&siz)), 0); err != nil { | ||
return nil, err | ||
} | ||
var addr net.TCPAddr | ||
addr.IP = raw.Addr[:] | ||
port := (*[2]byte)(unsafe.Pointer(&raw.Port)) // raw.Port is big-endian | ||
addr.Port = int(port[0])<<8 | int(port[1]) | ||
return &addr, nil | ||
} | ||
|
||
// Call ipv6_getorigdst() from linux/net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c | ||
// NOTE: I haven't tried yet but it should work since Linux 3.8. | ||
func ipv6_getorigdst(fd uintptr) (*net.TCPAddr, error) { | ||
const _IP6T_SO_ORIGINAL_DST = 80 // from linux/include/uapi/linux/netfilter_ipv6/ip6_tables.h | ||
var raw syscall.RawSockaddrInet6 | ||
siz := unsafe.Sizeof(raw) | ||
if err := socketcall(GETSOCKOPT, fd, syscall.IPPROTO_IPV6, _IP6T_SO_ORIGINAL_DST, uintptr(unsafe.Pointer(&raw)), uintptr(unsafe.Pointer(&siz)), 0); err != nil { | ||
return nil, err | ||
} | ||
var addr net.TCPAddr | ||
addr.IP = raw.Addr[:] | ||
port := (*[2]byte)(unsafe.Pointer(&raw.Port)) // raw.Port is big-endian | ||
addr.Port = int(port[0])<<8 | int(port[1]) | ||
return &addr, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package nfutil | ||
|
||
import ( | ||
"syscall" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// +build linux,!386 | ||
|
||
package main | ||
package nfutil | ||
|
||
import "syscall" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package pfutil | ||
|
||
import ( | ||
"net" | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
func NatLookup(c *net.TCPConn) (*net.TCPAddr, error) { | ||
const ( | ||
PF_INOUT = 0 | ||
PF_IN = 1 | ||
PF_OUT = 2 | ||
IOC_OUT = 0x40000000 | ||
IOC_IN = 0x80000000 | ||
IOC_INOUT = IOC_IN | IOC_OUT | ||
IOCPARM_MASK = 0x1FFF | ||
LEN = 4*16 + 4*4 + 4*1 | ||
// #define _IOC(inout,group,num,len) (inout | ((len & IOCPARM_MASK) << 16) | ((group) << 8) | (num)) | ||
// #define _IOWR(g,n,t) _IOC(IOC_INOUT, (g), (n), sizeof(t)) | ||
// #define DIOCNATLOOK _IOWR('D', 23, struct pfioc_natlook) | ||
DIOCNATLOOK = IOC_INOUT | ((LEN & IOCPARM_MASK) << 16) | ('D' << 8) | 23 | ||
) | ||
fd, err := syscall.Open("/dev/pf", 0, syscall.O_RDONLY) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer syscall.Close(fd) | ||
nl := struct { // struct pfioc_natlook | ||
saddr, daddr, rsaddr, rdaddr [16]byte | ||
sxport, dxport, rsxport, rdxport [4]byte | ||
af, proto, protoVariant, direction uint8 | ||
}{ | ||
af: syscall.AF_INET, | ||
proto: syscall.IPPROTO_TCP, | ||
direction: PF_OUT, | ||
} | ||
saddr := c.RemoteAddr().(*net.TCPAddr) | ||
daddr := c.LocalAddr().(*net.TCPAddr) | ||
copy(nl.saddr[:], saddr.IP) | ||
copy(nl.daddr[:], daddr.IP) | ||
nl.sxport[0], nl.sxport[1] = byte(saddr.Port>>8), byte(saddr.Port) | ||
nl.dxport[0], nl.dxport[1] = byte(daddr.Port>>8), byte(daddr.Port) | ||
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), DIOCNATLOOK, uintptr(unsafe.Pointer(&nl))); errno != 0 { | ||
return nil, errno | ||
} | ||
var addr net.TCPAddr | ||
addr.IP = nl.rdaddr[:4] | ||
addr.Port = int(nl.rdxport[0])<<8 | int(nl.rdxport[1]) | ||
return &addr, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters