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