blob: 83eaf855b4c2d07e125463966f7e792be826e119 [file] [log] [blame]
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -04001// 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
Dave Cheney7c8280c2014-02-25 09:47:42 -05005// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
Russ Cox27159562011-09-15 16:48:57 -04006
Mikio Harae8cf49f2012-11-27 00:45:42 +09007// Internet protocol family sockets for POSIX
8
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -04009package net
10
Brad Fitzpatrickef6806f2012-11-08 10:35:16 -060011import (
Mikio Haraeeb64b72015-05-14 10:18:10 +090012 "runtime"
Brad Fitzpatrickef6806f2012-11-08 10:35:16 -060013 "syscall"
14 "time"
15)
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040016
Mikio Haraeeb64b72015-05-14 10:18:10 +090017// BUG(rsc,mikio): On DragonFly BSD and OpenBSD, listening on the
18// "tcp" and "udp" networks does not listen for both IPv4 and IPv6
19// connections. This is due to the fact that IPv4 traffic will not be
20// routed to an IPv6 socket - two separate sockets are required if
21// both address families are to be supported.
22// See inet6(4) for details.
23
Mikio Haraf0291a82013-08-03 12:17:01 +090024func probeIPv4Stack() bool {
Mikio Hara29d1f3b2015-03-01 12:27:01 +090025 s, err := socketFunc(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
Mikio Haraf0291a82013-08-03 12:17:01 +090026 switch err {
27 case syscall.EAFNOSUPPORT, syscall.EPROTONOSUPPORT:
28 return false
29 case nil:
Mikio Hara29d1f3b2015-03-01 12:27:01 +090030 closeFunc(s)
Mikio Haraf0291a82013-08-03 12:17:01 +090031 }
32 return true
33}
34
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040035// Should we try to use the IPv4 socket interface if we're
36// only dealing with IPv4 sockets? As long as the host system
37// understands IPv6, it's okay to pass IPv4 addresses to the IPv6
38// interface. That simplifies our code and is most general.
39// Unfortunately, we need to run on kernels built without IPv6
40// support too. So probe the kernel to figure it out.
41//
42// probeIPv6Stack probes both basic IPv6 capability and IPv6 IPv4-
43// mapping capability which is controlled by IPV6_V6ONLY socket
44// option and/or kernel state "net.inet6.ip6.v6only".
45// It returns two boolean values. If the first boolean value is
46// true, kernel supports basic IPv6 functionality. If the second
47// boolean value is true, kernel supports IPv6 IPv4-mapping.
48func probeIPv6Stack() (supportsIPv6, supportsIPv4map bool) {
49 var probes = []struct {
Mikio Harac0a4ce52013-07-25 19:29:20 +090050 laddr TCPAddr
Mikio Haraa05ffd82014-03-28 13:27:51 +090051 value int
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040052 }{
53 // IPv6 communication capability
Mikio Haraa05ffd82014-03-28 13:27:51 +090054 {laddr: TCPAddr{IP: ParseIP("::1")}, value: 1},
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040055 // IPv6 IPv4-mapped address communication capability
Mikio Haraa05ffd82014-03-28 13:27:51 +090056 {laddr: TCPAddr{IP: IPv4(127, 0, 0, 1)}, value: 0},
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040057 }
Mikio Haraeeb64b72015-05-14 10:18:10 +090058 var supps [2]bool
59 switch runtime.GOOS {
60 case "dragonfly", "openbsd":
61 // Some released versions of DragonFly BSD pretend to
62 // accept IPV6_V6ONLY=0 successfully, but the state
63 // still stays IPV6_V6ONLY=1. Eventually DragonFly BSD
64 // stops preteding, but the transition period would
65 // cause unpredictable behavior and we need to avoid
66 // it.
67 //
68 // OpenBSD also doesn't support IPV6_V6ONLY=0 but it
69 // never pretends to accept IPV6_V6OLY=0. It always
70 // returns an error and we don't need to probe the
71 // capability.
72 probes = probes[:1]
73 }
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040074
75 for i := range probes {
Mikio Hara29d1f3b2015-03-01 12:27:01 +090076 s, err := socketFunc(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
Russ Coxc017a822011-11-13 22:44:52 -050077 if err != nil {
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040078 continue
79 }
Mikio Hara29d1f3b2015-03-01 12:27:01 +090080 defer closeFunc(s)
Mikio Haraa05ffd82014-03-28 13:27:51 +090081 syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, probes[i].value)
Mikio Harac0a4ce52013-07-25 19:29:20 +090082 sa, err := probes[i].laddr.sockaddr(syscall.AF_INET6)
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040083 if err != nil {
84 continue
85 }
Mikio Harac0a4ce52013-07-25 19:29:20 +090086 if err := syscall.Bind(s, sa); err != nil {
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040087 continue
88 }
Mikio Haraeeb64b72015-05-14 10:18:10 +090089 supps[i] = true
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040090 }
91
Mikio Haraeeb64b72015-05-14 10:18:10 +090092 return supps[0], supps[1]
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040093}
94
95// favoriteAddrFamily returns the appropriate address family to
Mikio Hara6fbe8052012-02-15 01:59:18 +090096// the given net, laddr, raddr and mode. At first it figures
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040097// address family out from the net. If mode indicates "listen"
Mikio Harab5dc8722012-03-06 00:13:10 +090098// and laddr is a wildcard, it assumes that the user wants to
99// make a passive connection with a wildcard address family, both
100// AF_INET and AF_INET6, and a wildcard address like following:
101//
102// 1. A wild-wild listen, "tcp" + ""
103// If the platform supports both IPv6 and IPv6 IPv4-mapping
104// capabilities, we assume that the user want to listen on
105// both IPv4 and IPv6 wildcard address over an AF_INET6
106// socket with IPV6_V6ONLY=0. Otherwise we prefer an IPv4
107// wildcard address listen over an AF_INET socket.
108//
109// 2. A wild-ipv4wild listen, "tcp" + "0.0.0.0"
110// Same as 1.
111//
112// 3. A wild-ipv6wild listen, "tcp" + "[::]"
113// Almost same as 1 but we prefer an IPv6 wildcard address
114// listen over an AF_INET6 socket with IPV6_V6ONLY=0 when
115// the platform supports IPv6 capability but not IPv6 IPv4-
116// mapping capability.
117//
118// 4. A ipv4-ipv4wild listen, "tcp4" + "" or "0.0.0.0"
119// We use an IPv4 (AF_INET) wildcard address listen.
120//
121// 5. A ipv6-ipv6wild listen, "tcp6" + "" or "[::]"
122// We use an IPv6 (AF_INET6, IPV6_V6ONLY=1) wildcard address
123// listen.
124//
125// Otherwise guess: if the addresses are IPv4 then returns AF_INET,
126// or else returns AF_INET6. It also returns a boolean value what
127// designates IPV6_V6ONLY option.
128//
129// Note that OpenBSD allows neither "net.inet6.ip6.v6only=1" change
130// nor IPPROTO_IPV6 level IPV6_V6ONLY socket option setting.
131func favoriteAddrFamily(net string, laddr, raddr sockaddr, mode string) (family int, ipv6only bool) {
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400132 switch net[len(net)-1] {
133 case '4':
Mikio Harab5dc8722012-03-06 00:13:10 +0900134 return syscall.AF_INET, false
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400135 case '6':
Mikio Harab5dc8722012-03-06 00:13:10 +0900136 return syscall.AF_INET6, true
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400137 }
138
Mikio Harab252fe72012-04-25 12:29:14 +0900139 if mode == "listen" && (laddr == nil || laddr.isWildcard()) {
Mikio Harab5dc8722012-03-06 00:13:10 +0900140 if supportsIPv4map {
141 return syscall.AF_INET6, false
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400142 }
Mikio Harab252fe72012-04-25 12:29:14 +0900143 if laddr == nil {
144 return syscall.AF_INET, false
145 }
Mikio Harab5dc8722012-03-06 00:13:10 +0900146 return laddr.family(), false
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400147 }
148
149 if (laddr == nil || laddr.family() == syscall.AF_INET) &&
150 (raddr == nil || raddr.family() == syscall.AF_INET) {
Mikio Harab5dc8722012-03-06 00:13:10 +0900151 return syscall.AF_INET, false
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400152 }
Mikio Harab5dc8722012-03-06 00:13:10 +0900153 return syscall.AF_INET6, false
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400154}
155
Mikio Harab5dc8722012-03-06 00:13:10 +0900156// Internet sockets (TCP, UDP, IP)
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400157
Mikio Hara5c055e72014-08-05 06:10:46 +0900158func internetSocket(net string, laddr, raddr sockaddr, deadline time.Time, sotype, proto int, mode string) (fd *netFD, err error) {
Mikio Harab5dc8722012-03-06 00:13:10 +0900159 family, ipv6only := favoriteAddrFamily(net, laddr, raddr, mode)
Mikio Hara5c055e72014-08-05 06:10:46 +0900160 return socket(net, family, sotype, proto, ipv6only, laddr, raddr, deadline)
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400161}
162
Mikio Harae8cf49f2012-11-27 00:45:42 +0900163func ipToSockaddr(family int, ip IP, port int, zone string) (syscall.Sockaddr, error) {
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400164 switch family {
165 case syscall.AF_INET:
166 if len(ip) == 0 {
167 ip = IPv4zero
168 }
169 if ip = ip.To4(); ip == nil {
Mikio Hara055ecb72015-04-21 21:20:15 +0900170 return nil, &AddrError{Err: "non-IPv4 address", Addr: ip.String()}
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400171 }
Mikio Harae8cf49f2012-11-27 00:45:42 +0900172 sa := new(syscall.SockaddrInet4)
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400173 for i := 0; i < IPv4len; i++ {
Mikio Harae8cf49f2012-11-27 00:45:42 +0900174 sa.Addr[i] = ip[i]
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400175 }
Mikio Harae8cf49f2012-11-27 00:45:42 +0900176 sa.Port = port
177 return sa, nil
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400178 case syscall.AF_INET6:
179 if len(ip) == 0 {
180 ip = IPv6zero
181 }
182 // IPv4 callers use 0.0.0.0 to mean "announce on any available address".
183 // In IPv6 mode, Linux treats that as meaning "announce on 0.0.0.0",
Mikio Harab5dc8722012-03-06 00:13:10 +0900184 // which it refuses to do. Rewrite to the IPv6 unspecified address.
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400185 if ip.Equal(IPv4zero) {
186 ip = IPv6zero
187 }
188 if ip = ip.To16(); ip == nil {
Mikio Hara055ecb72015-04-21 21:20:15 +0900189 return nil, &AddrError{Err: "non-IPv6 address", Addr: ip.String()}
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400190 }
Mikio Harae8cf49f2012-11-27 00:45:42 +0900191 sa := new(syscall.SockaddrInet6)
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400192 for i := 0; i < IPv6len; i++ {
Mikio Harae8cf49f2012-11-27 00:45:42 +0900193 sa.Addr[i] = ip[i]
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400194 }
Mikio Harae8cf49f2012-11-27 00:45:42 +0900195 sa.Port = port
196 sa.ZoneId = uint32(zoneToInt(zone))
197 return sa, nil
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400198 }
Mikio Hara055ecb72015-04-21 21:20:15 +0900199 return nil, &AddrError{Err: "invalid address family", Addr: ip.String()}
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400200}