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.
5 //go:build unix || js || wasip1 || windows
17 // probe probes IPv4, IPv6 and IPv4-mapped IPv6 communication
18 // capabilities which are controlled by the IPV6_V6ONLY socket option
19 // and kernel configuration.
21 // Should we try to use the IPv4 socket interface if we're only
22 // dealing with IPv4 sockets? As long as the host system understands
23 // IPv4-mapped IPv6, it's okay to pass IPv4-mapped IPv6 addresses to
24 // the IPv6 interface. That simplifies our code and is most
25 // general. Unfortunately, we need to run on kernels built without
26 // IPv6 support too. So probe the kernel to figure it out.
27 func (p *ipStackCapabilities) probe() {
30 // Both ipv4 and ipv6 are faked; see net_fake.go.
33 p.ipv4MappedIPv6Enabled = true
37 s, err := sysSocket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
39 case syscall.EAFNOSUPPORT, syscall.EPROTONOSUPPORT:
44 var probes = []struct {
48 // IPv6 communication capability
49 {laddr: TCPAddr{IP: ParseIP("::1")}, value: 1},
50 // IPv4-mapped IPv6 address communication capability
51 {laddr: TCPAddr{IP: IPv4(127, 0, 0, 1)}, value: 0},
54 case "dragonfly", "openbsd":
55 // The latest DragonFly BSD and OpenBSD kernels don't
56 // support IPV6_V6ONLY=0. They always return an error
57 // and we don't need to probe the capability.
60 for i := range probes {
61 s, err := sysSocket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
65 defer poll.CloseFunc(s)
66 syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, probes[i].value)
67 sa, err := probes[i].laddr.sockaddr(syscall.AF_INET6)
71 if err := syscall.Bind(s, sa); err != nil {
77 p.ipv4MappedIPv6Enabled = true
82 // favoriteAddrFamily returns the appropriate address family for the
83 // given network, laddr, raddr and mode.
85 // If mode indicates "listen" and laddr is a wildcard, we assume that
86 // the user wants to make a passive-open connection with a wildcard
87 // address family, both AF_INET and AF_INET6, and a wildcard address
88 // like the following:
90 // - A listen for a wildcard communication domain, "tcp" or
91 // "udp", with a wildcard address: If the platform supports
92 // both IPv6 and IPv4-mapped IPv6 communication capabilities,
93 // or does not support IPv4, we use a dual stack, AF_INET6 and
94 // IPV6_V6ONLY=0, wildcard address listen. The dual stack
95 // wildcard address listen may fall back to an IPv6-only,
96 // AF_INET6 and IPV6_V6ONLY=1, wildcard address listen.
97 // Otherwise we prefer an IPv4-only, AF_INET, wildcard address
100 // - A listen for a wildcard communication domain, "tcp" or
101 // "udp", with an IPv4 wildcard address: same as above.
103 // - A listen for a wildcard communication domain, "tcp" or
104 // "udp", with an IPv6 wildcard address: same as above.
106 // - A listen for an IPv4 communication domain, "tcp4" or "udp4",
107 // with an IPv4 wildcard address: We use an IPv4-only, AF_INET,
108 // wildcard address listen.
110 // - A listen for an IPv6 communication domain, "tcp6" or "udp6",
111 // with an IPv6 wildcard address: We use an IPv6-only, AF_INET6
112 // and IPV6_V6ONLY=1, wildcard address listen.
114 // Otherwise guess: If the addresses are IPv4 then returns AF_INET,
115 // or else returns AF_INET6. It also returns a boolean value what
116 // designates IPV6_V6ONLY option.
118 // Note that the latest DragonFly BSD and OpenBSD kernels allow
119 // neither "net.inet6.ip6.v6only=1" change nor IPPROTO_IPV6 level
120 // IPV6_V6ONLY socket option setting.
121 func favoriteAddrFamily(network string, laddr, raddr sockaddr, mode string) (family int, ipv6only bool) {
122 switch network[len(network)-1] {
124 return syscall.AF_INET, false
126 return syscall.AF_INET6, true
129 if mode == "listen" && (laddr == nil || laddr.isWildcard()) {
130 if supportsIPv4map() || !supportsIPv4() {
131 return syscall.AF_INET6, false
134 return syscall.AF_INET, false
136 return laddr.family(), false
139 if (laddr == nil || laddr.family() == syscall.AF_INET) &&
140 (raddr == nil || raddr.family() == syscall.AF_INET) {
141 return syscall.AF_INET, false
143 return syscall.AF_INET6, false
146 func internetSocket(ctx context.Context, net string, laddr, raddr sockaddr, sotype, proto int, mode string, ctrlCtxFn func(context.Context, string, string, syscall.RawConn) error) (fd *netFD, err error) {
147 switch runtime.GOOS {
148 case "aix", "windows", "openbsd", "js", "wasip1":
149 if mode == "dial" && raddr.isWildcard() {
150 raddr = raddr.toLocal(net)
153 family, ipv6only := favoriteAddrFamily(net, laddr, raddr, mode)
154 return socket(ctx, net, family, sotype, proto, ipv6only, laddr, raddr, ctrlCtxFn)
157 func ipToSockaddrInet4(ip IP, port int) (syscall.SockaddrInet4, error) {
163 return syscall.SockaddrInet4{}, &AddrError{Err: "non-IPv4 address", Addr: ip.String()}
165 sa := syscall.SockaddrInet4{Port: port}
166 copy(sa.Addr[:], ip4)
170 func ipToSockaddrInet6(ip IP, port int, zone string) (syscall.SockaddrInet6, error) {
171 // In general, an IP wildcard address, which is either
172 // "0.0.0.0" or "::", means the entire IP addressing
173 // space. For some historical reason, it is used to
174 // specify "any available address" on some operations
177 // When the IP node supports IPv4-mapped IPv6 address,
178 // we allow a listener to listen to the wildcard
179 // address of both IP addressing spaces by specifying
180 // IPv6 wildcard address.
181 if len(ip) == 0 || ip.Equal(IPv4zero) {
184 // We accept any IPv6 address including IPv4-mapped
188 return syscall.SockaddrInet6{}, &AddrError{Err: "non-IPv6 address", Addr: ip.String()}
190 sa := syscall.SockaddrInet6{Port: port, ZoneId: uint32(zoneCache.index(zone))}
191 copy(sa.Addr[:], ip6)
195 func ipToSockaddr(family int, ip IP, port int, zone string) (syscall.Sockaddr, error) {
197 case syscall.AF_INET:
198 sa, err := ipToSockaddrInet4(ip, port)
203 case syscall.AF_INET6:
204 sa, err := ipToSockaddrInet6(ip, port, zone)
210 return nil, &AddrError{Err: "invalid address family", Addr: ip.String()}
213 func addrPortToSockaddrInet4(ap netip.AddrPort) (syscall.SockaddrInet4, error) {
214 // ipToSockaddrInet4 has special handling here for zero length slices.
215 // We do not, because netip has no concept of a generic zero IP address.
218 return syscall.SockaddrInet4{}, &AddrError{Err: "non-IPv4 address", Addr: addr.String()}
220 sa := syscall.SockaddrInet4{
222 Port: int(ap.Port()),
227 func addrPortToSockaddrInet6(ap netip.AddrPort) (syscall.SockaddrInet6, error) {
228 // ipToSockaddrInet6 has special handling here for zero length slices.
229 // We do not, because netip has no concept of a generic zero IP address.
231 // addr is allowed to be an IPv4 address, because As16 will convert it
232 // to an IPv4-mapped IPv6 address.
233 // The error message is kept consistent with ipToSockaddrInet6.
236 return syscall.SockaddrInet6{}, &AddrError{Err: "non-IPv6 address", Addr: addr.String()}
238 sa := syscall.SockaddrInet6{
240 Port: int(ap.Port()),
241 ZoneId: uint32(zoneCache.index(addr.Zone())),