sockopt_darwin.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package internet
  2. import (
  3. "context"
  4. gonet "net"
  5. "os"
  6. "runtime"
  7. "strconv"
  8. "strings"
  9. "syscall"
  10. "unsafe"
  11. "github.com/xtls/xray-core/common/errors"
  12. "github.com/xtls/xray-core/common/net"
  13. "golang.org/x/sys/unix"
  14. )
  15. const (
  16. // TCP_FASTOPEN_SERVER is the value to enable TCP fast open on darwin for server connections.
  17. TCP_FASTOPEN_SERVER = 0x01
  18. // TCP_FASTOPEN_CLIENT is the value to enable TCP fast open on darwin for client connections.
  19. TCP_FASTOPEN_CLIENT = 0x02 // nolint: revive,stylecheck
  20. // syscall.TCP_KEEPINTVL is missing on some darwin architectures.
  21. sysTCP_KEEPINTVL = 0x101 // nolint: revive,stylecheck
  22. )
  23. const (
  24. PfOut = 2
  25. IOCOut = 0x40000000
  26. IOCIn = 0x80000000
  27. IOCInOut = IOCIn | IOCOut
  28. IOCPARMMask = 0x1FFF
  29. LEN = 4*16 + 4*4 + 4*1
  30. // #define _IOC(inout,group,num,len) (inout | ((len & IOCPARMMask) << 16) | ((group) << 8) | (num))
  31. // #define _IOWR(g,n,t) _IOC(IOCInOut, (g), (n), sizeof(t))
  32. // #define DIOCNATLOOK _IOWR('D', 23, struct pfioc_natlook)
  33. DIOCNATLOOK = IOCInOut | ((LEN & IOCPARMMask) << 16) | ('D' << 8) | 23
  34. )
  35. // OriginalDst uses ioctl to read original destination from /dev/pf
  36. func OriginalDst(la, ra net.Addr) (net.IP, int, error) {
  37. f, err := os.Open("/dev/pf")
  38. if err != nil {
  39. return net.IP{}, -1, errors.New("failed to open device /dev/pf").Base(err)
  40. }
  41. defer f.Close()
  42. fd := f.Fd()
  43. nl := struct { // struct pfioc_natlook
  44. saddr, daddr, rsaddr, rdaddr [16]byte
  45. sxport, dxport, rsxport, rdxport [4]byte
  46. af, proto, protoVariant, direction uint8
  47. }{
  48. af: syscall.AF_INET,
  49. proto: syscall.IPPROTO_TCP,
  50. direction: PfOut,
  51. }
  52. var raIP, laIP net.IP
  53. var raPort, laPort int
  54. switch la.(type) {
  55. case *net.TCPAddr:
  56. raIP = ra.(*net.TCPAddr).IP
  57. laIP = la.(*net.TCPAddr).IP
  58. raPort = ra.(*net.TCPAddr).Port
  59. laPort = la.(*net.TCPAddr).Port
  60. case *net.UDPAddr:
  61. raIP = ra.(*net.UDPAddr).IP
  62. laIP = la.(*net.UDPAddr).IP
  63. raPort = ra.(*net.UDPAddr).Port
  64. laPort = la.(*net.UDPAddr).Port
  65. }
  66. if raIP.To4() != nil {
  67. if laIP.IsUnspecified() {
  68. laIP = net.ParseIP("127.0.0.1")
  69. }
  70. copy(nl.saddr[:net.IPv4len], raIP.To4())
  71. copy(nl.daddr[:net.IPv4len], laIP.To4())
  72. }
  73. if raIP.To16() != nil && raIP.To4() == nil {
  74. if laIP.IsUnspecified() {
  75. laIP = net.ParseIP("::1")
  76. }
  77. copy(nl.saddr[:], raIP)
  78. copy(nl.daddr[:], laIP)
  79. }
  80. nl.sxport[0], nl.sxport[1] = byte(raPort>>8), byte(raPort)
  81. nl.dxport[0], nl.dxport[1] = byte(laPort>>8), byte(laPort)
  82. if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, DIOCNATLOOK, uintptr(unsafe.Pointer(&nl))); errno != 0 {
  83. return net.IP{}, -1, os.NewSyscallError("ioctl", err)
  84. }
  85. odPort := nl.rdxport
  86. var odIP net.IP
  87. switch nl.af {
  88. case syscall.AF_INET:
  89. odIP = make(net.IP, net.IPv4len)
  90. copy(odIP, nl.rdaddr[:net.IPv4len])
  91. case syscall.AF_INET6:
  92. odIP = make(net.IP, net.IPv6len)
  93. copy(odIP, nl.rdaddr[:])
  94. }
  95. return odIP, int(net.PortFromBytes(odPort[:2])), nil
  96. }
  97. func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
  98. if isTCPSocket(network) {
  99. tfo := config.ParseTFOValue()
  100. if tfo > 0 {
  101. tfo = TCP_FASTOPEN_CLIENT
  102. }
  103. if tfo >= 0 {
  104. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_FASTOPEN, tfo); err != nil {
  105. return err
  106. }
  107. }
  108. if config.TcpKeepAliveIdle > 0 || config.TcpKeepAliveInterval > 0 {
  109. if config.TcpKeepAliveIdle > 0 {
  110. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPALIVE, int(config.TcpKeepAliveInterval)); err != nil {
  111. return errors.New("failed to set TCP_KEEPINTVL", err)
  112. }
  113. }
  114. if config.TcpKeepAliveInterval > 0 {
  115. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, sysTCP_KEEPINTVL, int(config.TcpKeepAliveIdle)); err != nil {
  116. return errors.New("failed to set TCP_KEEPIDLE", err)
  117. }
  118. }
  119. if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 1); err != nil {
  120. return errors.New("failed to set SO_KEEPALIVE", err)
  121. }
  122. } else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 {
  123. if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 0); err != nil {
  124. return errors.New("failed to unset SO_KEEPALIVE", err)
  125. }
  126. }
  127. }
  128. if config.Interface != "" {
  129. iface, err := gonet.InterfaceByName(config.Interface)
  130. if err != nil {
  131. return errors.New("failed to get interface ", config.Interface).Base(err)
  132. }
  133. if network == "tcp6" || network == "udp6" {
  134. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, iface.Index); err != nil {
  135. return errors.New("failed to set IPV6_BOUND_IF").Base(err)
  136. }
  137. } else {
  138. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_BOUND_IF, iface.Index); err != nil {
  139. return errors.New("failed to set IP_BOUND_IF").Base(err)
  140. }
  141. }
  142. }
  143. if len(config.CustomSockopt) > 0 {
  144. for _, custom := range config.CustomSockopt {
  145. if custom.System != "" && custom.System != runtime.GOOS {
  146. errors.LogDebug(context.Background(), "CustomSockopt system not match: ", "want ", custom.System, " got ", runtime.GOOS)
  147. continue
  148. }
  149. // Skip unwanted network type
  150. // network might be tcp4 or tcp6
  151. // use HasPrefix so that "tcp" can match tcp4/6 with "tcp" if user want to control all tcp (udp is also the same)
  152. // if it is empty, strings.HasPrefix will always return true to make it apply for all networks
  153. if !strings.HasPrefix(network, custom.Network) {
  154. continue
  155. }
  156. var level = 0x6 // default TCP
  157. var opt int
  158. if len(custom.Opt) == 0 {
  159. return errors.New("No opt!")
  160. } else {
  161. opt, _ = strconv.Atoi(custom.Opt)
  162. }
  163. if custom.Level != "" {
  164. level, _ = strconv.Atoi(custom.Level)
  165. }
  166. if custom.Type == "int" {
  167. value, _ := strconv.Atoi(custom.Value)
  168. if err := syscall.SetsockoptInt(int(fd), level, opt, value); err != nil {
  169. return errors.New("failed to set CustomSockoptInt", opt, value, err)
  170. }
  171. } else if custom.Type == "str" {
  172. if err := syscall.SetsockoptString(int(fd), level, opt, custom.Value); err != nil {
  173. return errors.New("failed to set CustomSockoptString", opt, custom.Value, err)
  174. }
  175. } else {
  176. return errors.New("unknown CustomSockopt type:", custom.Type)
  177. }
  178. }
  179. }
  180. return nil
  181. }
  182. func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
  183. if isTCPSocket(network) {
  184. tfo := config.ParseTFOValue()
  185. if tfo > 0 {
  186. tfo = TCP_FASTOPEN_SERVER
  187. }
  188. if tfo >= 0 {
  189. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_FASTOPEN, tfo); err != nil {
  190. return err
  191. }
  192. }
  193. if config.TcpKeepAliveIdle > 0 || config.TcpKeepAliveInterval > 0 {
  194. if config.TcpKeepAliveIdle > 0 {
  195. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPALIVE, int(config.TcpKeepAliveInterval)); err != nil {
  196. return errors.New("failed to set TCP_KEEPINTVL", err)
  197. }
  198. }
  199. if config.TcpKeepAliveInterval > 0 {
  200. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, sysTCP_KEEPINTVL, int(config.TcpKeepAliveIdle)); err != nil {
  201. return errors.New("failed to set TCP_KEEPIDLE", err)
  202. }
  203. }
  204. if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 1); err != nil {
  205. return errors.New("failed to set SO_KEEPALIVE", err)
  206. }
  207. } else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 {
  208. if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 0); err != nil {
  209. return errors.New("failed to unset SO_KEEPALIVE", err)
  210. }
  211. }
  212. }
  213. if config.Interface != "" {
  214. iface, err := gonet.InterfaceByName(config.Interface)
  215. if err != nil {
  216. return errors.New("failed to get interface ", config.Interface).Base(err)
  217. }
  218. if network == "tcp6" || network == "udp6" {
  219. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, iface.Index); err != nil {
  220. return errors.New("failed to set IPV6_BOUND_IF").Base(err)
  221. }
  222. } else {
  223. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_BOUND_IF, iface.Index); err != nil {
  224. return errors.New("failed to set IP_BOUND_IF").Base(err)
  225. }
  226. }
  227. }
  228. if config.V6Only {
  229. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_V6ONLY, 1); err != nil {
  230. return errors.New("failed to set IPV6_V6ONLY").Base(err)
  231. }
  232. }
  233. if len(config.CustomSockopt) > 0 {
  234. for _, custom := range config.CustomSockopt {
  235. if custom.System != "" && custom.System != runtime.GOOS {
  236. errors.LogDebug(context.Background(), "CustomSockopt system not match: ", "want ", custom.System, " got ", runtime.GOOS)
  237. continue
  238. }
  239. // Skip unwanted network type
  240. // network might be tcp4 or tcp6
  241. // use HasPrefix so that "tcp" can match tcp4/6 with "tcp" if user want to control all tcp (udp is also the same)
  242. // if it is empty, strings.HasPrefix will always return true to make it apply for all networks
  243. if !strings.HasPrefix(network, custom.Network) {
  244. continue
  245. }
  246. var level = 0x6 // default TCP
  247. var opt int
  248. if len(custom.Opt) == 0 {
  249. return errors.New("No opt!")
  250. } else {
  251. opt, _ = strconv.Atoi(custom.Opt)
  252. }
  253. if custom.Level != "" {
  254. level, _ = strconv.Atoi(custom.Level)
  255. }
  256. if custom.Type == "int" {
  257. value, _ := strconv.Atoi(custom.Value)
  258. if err := syscall.SetsockoptInt(int(fd), level, opt, value); err != nil {
  259. return errors.New("failed to set CustomSockoptInt", opt, value, err)
  260. }
  261. } else if custom.Type == "str" {
  262. if err := syscall.SetsockoptString(int(fd), level, opt, custom.Value); err != nil {
  263. return errors.New("failed to set CustomSockoptString", opt, custom.Value, err)
  264. }
  265. } else {
  266. return errors.New("unknown CustomSockopt type:", custom.Type)
  267. }
  268. }
  269. }
  270. return nil
  271. }
  272. func bindAddr(fd uintptr, address []byte, port uint32) error {
  273. setReuseAddr(fd)
  274. setReusePort(fd)
  275. var sockaddr unix.Sockaddr
  276. switch len(address) {
  277. case net.IPv4len:
  278. a4 := &unix.SockaddrInet4{
  279. Port: int(port),
  280. }
  281. copy(a4.Addr[:], address)
  282. sockaddr = a4
  283. case net.IPv6len:
  284. a6 := &unix.SockaddrInet6{
  285. Port: int(port),
  286. }
  287. copy(a6.Addr[:], address)
  288. sockaddr = a6
  289. default:
  290. return errors.New("unexpected length of ip")
  291. }
  292. return unix.Bind(int(fd), sockaddr)
  293. }
  294. func setReuseAddr(fd uintptr) error {
  295. if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1); err != nil {
  296. return errors.New("failed to set SO_REUSEADDR").Base(err).AtWarning()
  297. }
  298. return nil
  299. }
  300. func setReusePort(fd uintptr) error {
  301. if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
  302. return errors.New("failed to set SO_REUSEPORT").Base(err).AtWarning()
  303. }
  304. return nil
  305. }