ipsock_posix.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build aix || darwin || dragonfly || freebsd || hurd || (js && wasm) || linux || netbsd || openbsd || solaris || windows
  5. package net
  6. import (
  7. "context"
  8. "internal/poll"
  9. "net/netip"
  10. "runtime"
  11. "syscall"
  12. )
  13. // probe probes IPv4, IPv6 and IPv4-mapped IPv6 communication
  14. // capabilities which are controlled by the IPV6_V6ONLY socket option
  15. // and kernel configuration.
  16. //
  17. // Should we try to use the IPv4 socket interface if we're only
  18. // dealing with IPv4 sockets? As long as the host system understands
  19. // IPv4-mapped IPv6, it's okay to pass IPv4-mapped IPv6 addresses to
  20. // the IPv6 interface. That simplifies our code and is most
  21. // general. Unfortunately, we need to run on kernels built without
  22. // IPv6 support too. So probe the kernel to figure it out.
  23. func (p *ipStackCapabilities) probe() {
  24. s, err := sysSocket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
  25. switch err {
  26. case syscall.EAFNOSUPPORT, syscall.EPROTONOSUPPORT:
  27. case nil:
  28. poll.CloseFunc(s)
  29. p.ipv4Enabled = true
  30. }
  31. var probes = []struct {
  32. laddr TCPAddr
  33. value int
  34. }{
  35. // IPv6 communication capability
  36. {laddr: TCPAddr{IP: ParseIP("::1")}, value: 1},
  37. // IPv4-mapped IPv6 address communication capability
  38. {laddr: TCPAddr{IP: IPv4(127, 0, 0, 1)}, value: 0},
  39. }
  40. switch runtime.GOOS {
  41. case "dragonfly", "openbsd":
  42. // The latest DragonFly BSD and OpenBSD kernels don't
  43. // support IPV6_V6ONLY=0. They always return an error
  44. // and we don't need to probe the capability.
  45. probes = probes[:1]
  46. }
  47. for i := range probes {
  48. s, err := sysSocket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
  49. if err != nil {
  50. continue
  51. }
  52. defer poll.CloseFunc(s)
  53. syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, probes[i].value)
  54. sa, err := probes[i].laddr.sockaddr(syscall.AF_INET6)
  55. if err != nil {
  56. continue
  57. }
  58. if err := syscall.Bind(s, sa); err != nil {
  59. continue
  60. }
  61. if i == 0 {
  62. p.ipv6Enabled = true
  63. } else {
  64. p.ipv4MappedIPv6Enabled = true
  65. }
  66. }
  67. }
  68. // favoriteAddrFamily returns the appropriate address family for the
  69. // given network, laddr, raddr and mode.
  70. //
  71. // If mode indicates "listen" and laddr is a wildcard, we assume that
  72. // the user wants to make a passive-open connection with a wildcard
  73. // address family, both AF_INET and AF_INET6, and a wildcard address
  74. // like the following:
  75. //
  76. // - A listen for a wildcard communication domain, "tcp" or
  77. // "udp", with a wildcard address: If the platform supports
  78. // both IPv6 and IPv4-mapped IPv6 communication capabilities,
  79. // or does not support IPv4, we use a dual stack, AF_INET6 and
  80. // IPV6_V6ONLY=0, wildcard address listen. The dual stack
  81. // wildcard address listen may fall back to an IPv6-only,
  82. // AF_INET6 and IPV6_V6ONLY=1, wildcard address listen.
  83. // Otherwise we prefer an IPv4-only, AF_INET, wildcard address
  84. // listen.
  85. //
  86. // - A listen for a wildcard communication domain, "tcp" or
  87. // "udp", with an IPv4 wildcard address: same as above.
  88. //
  89. // - A listen for a wildcard communication domain, "tcp" or
  90. // "udp", with an IPv6 wildcard address: same as above.
  91. //
  92. // - A listen for an IPv4 communication domain, "tcp4" or "udp4",
  93. // with an IPv4 wildcard address: We use an IPv4-only, AF_INET,
  94. // wildcard address listen.
  95. //
  96. // - A listen for an IPv6 communication domain, "tcp6" or "udp6",
  97. // with an IPv6 wildcard address: We use an IPv6-only, AF_INET6
  98. // and IPV6_V6ONLY=1, wildcard address listen.
  99. //
  100. // Otherwise guess: If the addresses are IPv4 then returns AF_INET,
  101. // or else returns AF_INET6. It also returns a boolean value what
  102. // designates IPV6_V6ONLY option.
  103. //
  104. // Note that the latest DragonFly BSD and OpenBSD kernels allow
  105. // neither "net.inet6.ip6.v6only=1" change nor IPPROTO_IPV6 level
  106. // IPV6_V6ONLY socket option setting.
  107. func favoriteAddrFamily(network string, laddr, raddr sockaddr, mode string) (family int, ipv6only bool) {
  108. switch network[len(network)-1] {
  109. case '4':
  110. return syscall.AF_INET, false
  111. case '6':
  112. return syscall.AF_INET6, true
  113. }
  114. if mode == "listen" && (laddr == nil || laddr.isWildcard()) {
  115. if supportsIPv4map() || !supportsIPv4() {
  116. return syscall.AF_INET6, false
  117. }
  118. if laddr == nil {
  119. return syscall.AF_INET, false
  120. }
  121. return laddr.family(), false
  122. }
  123. if (laddr == nil || laddr.family() == syscall.AF_INET) &&
  124. (raddr == nil || raddr.family() == syscall.AF_INET) {
  125. return syscall.AF_INET, false
  126. }
  127. return syscall.AF_INET6, false
  128. }
  129. func internetSocket(ctx context.Context, net string, laddr, raddr sockaddr, sotype, proto int, mode string, ctrlFn func(string, string, syscall.RawConn) error) (fd *netFD, err error) {
  130. if (runtime.GOOS == "aix" || runtime.GOOS == "windows" || runtime.GOOS == "openbsd") && mode == "dial" && raddr.isWildcard() {
  131. raddr = raddr.toLocal(net)
  132. }
  133. family, ipv6only := favoriteAddrFamily(net, laddr, raddr, mode)
  134. return socket(ctx, net, family, sotype, proto, ipv6only, laddr, raddr, ctrlFn)
  135. }
  136. func ipToSockaddrInet4(ip IP, port int) (syscall.SockaddrInet4, error) {
  137. if len(ip) == 0 {
  138. ip = IPv4zero
  139. }
  140. ip4 := ip.To4()
  141. if ip4 == nil {
  142. return syscall.SockaddrInet4{}, &AddrError{Err: "non-IPv4 address", Addr: ip.String()}
  143. }
  144. sa := syscall.SockaddrInet4{Port: port}
  145. copy(sa.Addr[:], ip4)
  146. return sa, nil
  147. }
  148. func ipToSockaddrInet6(ip IP, port int, zone string) (syscall.SockaddrInet6, error) {
  149. // In general, an IP wildcard address, which is either
  150. // "0.0.0.0" or "::", means the entire IP addressing
  151. // space. For some historical reason, it is used to
  152. // specify "any available address" on some operations
  153. // of IP node.
  154. //
  155. // When the IP node supports IPv4-mapped IPv6 address,
  156. // we allow a listener to listen to the wildcard
  157. // address of both IP addressing spaces by specifying
  158. // IPv6 wildcard address.
  159. if len(ip) == 0 || ip.Equal(IPv4zero) {
  160. ip = IPv6zero
  161. }
  162. // We accept any IPv6 address including IPv4-mapped
  163. // IPv6 address.
  164. ip6 := ip.To16()
  165. if ip6 == nil {
  166. return syscall.SockaddrInet6{}, &AddrError{Err: "non-IPv6 address", Addr: ip.String()}
  167. }
  168. sa := syscall.SockaddrInet6{Port: port, ZoneId: uint32(zoneCache.index(zone))}
  169. copy(sa.Addr[:], ip6)
  170. return sa, nil
  171. }
  172. func ipToSockaddr(family int, ip IP, port int, zone string) (syscall.Sockaddr, error) {
  173. switch family {
  174. case syscall.AF_INET:
  175. sa, err := ipToSockaddrInet4(ip, port)
  176. if err != nil {
  177. return nil, err
  178. }
  179. return &sa, nil
  180. case syscall.AF_INET6:
  181. sa, err := ipToSockaddrInet6(ip, port, zone)
  182. if err != nil {
  183. return nil, err
  184. }
  185. return &sa, nil
  186. }
  187. return nil, &AddrError{Err: "invalid address family", Addr: ip.String()}
  188. }
  189. func addrPortToSockaddrInet4(ap netip.AddrPort) (syscall.SockaddrInet4, error) {
  190. // ipToSockaddrInet4 has special handling here for zero length slices.
  191. // We do not, because netip has no concept of a generic zero IP address.
  192. addr := ap.Addr()
  193. if !addr.Is4() {
  194. return syscall.SockaddrInet4{}, &AddrError{Err: "non-IPv4 address", Addr: addr.String()}
  195. }
  196. sa := syscall.SockaddrInet4{
  197. Addr: addr.As4(),
  198. Port: int(ap.Port()),
  199. }
  200. return sa, nil
  201. }
  202. func addrPortToSockaddrInet6(ap netip.AddrPort) (syscall.SockaddrInet6, error) {
  203. // ipToSockaddrInet6 has special handling here for zero length slices.
  204. // We do not, because netip has no concept of a generic zero IP address.
  205. addr := ap.Addr()
  206. if !addr.Is6() {
  207. return syscall.SockaddrInet6{}, &AddrError{Err: "non-IPv6 address", Addr: addr.String()}
  208. }
  209. sa := syscall.SockaddrInet6{
  210. Addr: addr.As16(),
  211. Port: int(ap.Port()),
  212. ZoneId: uint32(zoneCache.index(addr.Zone())),
  213. }
  214. return sa, nil
  215. }