blob: 4419aaf8a06d70483c69a33aa744bed58ec0e5d6 [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
Paul Marks5a3ff6c2015-09-04 18:33:35 -0700104// capabilities, or does not support IPv4, we assume that
105// the user wants to listen on both IPv4 and IPv6 wildcard
106// addresses over an AF_INET6 socket with IPV6_V6ONLY=0.
107// Otherwise we prefer an IPv4 wildcard address listen over
108// an AF_INET socket.
Mikio Harab5dc8722012-03-06 00:13:10 +0900109//
110// 2. A wild-ipv4wild listen, "tcp" + "0.0.0.0"
111// Same as 1.
112//
113// 3. A wild-ipv6wild listen, "tcp" + "[::]"
114// Almost same as 1 but we prefer an IPv6 wildcard address
115// listen over an AF_INET6 socket with IPV6_V6ONLY=0 when
116// the platform supports IPv6 capability but not IPv6 IPv4-
117// mapping capability.
118//
119// 4. A ipv4-ipv4wild listen, "tcp4" + "" or "0.0.0.0"
120// We use an IPv4 (AF_INET) wildcard address listen.
121//
122// 5. A ipv6-ipv6wild listen, "tcp6" + "" or "[::]"
123// We use an IPv6 (AF_INET6, IPV6_V6ONLY=1) wildcard address
124// listen.
125//
126// Otherwise guess: if the addresses are IPv4 then returns AF_INET,
127// or else returns AF_INET6. It also returns a boolean value what
128// designates IPV6_V6ONLY option.
129//
130// Note that OpenBSD allows neither "net.inet6.ip6.v6only=1" change
131// nor IPPROTO_IPV6 level IPV6_V6ONLY socket option setting.
132func favoriteAddrFamily(net string, laddr, raddr sockaddr, mode string) (family int, ipv6only bool) {
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400133 switch net[len(net)-1] {
134 case '4':
Mikio Harab5dc8722012-03-06 00:13:10 +0900135 return syscall.AF_INET, false
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400136 case '6':
Mikio Harab5dc8722012-03-06 00:13:10 +0900137 return syscall.AF_INET6, true
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400138 }
139
Mikio Harab252fe72012-04-25 12:29:14 +0900140 if mode == "listen" && (laddr == nil || laddr.isWildcard()) {
Paul Marks5a3ff6c2015-09-04 18:33:35 -0700141 if supportsIPv4map || !supportsIPv4 {
Mikio Harab5dc8722012-03-06 00:13:10 +0900142 return syscall.AF_INET6, false
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400143 }
Mikio Harab252fe72012-04-25 12:29:14 +0900144 if laddr == nil {
145 return syscall.AF_INET, false
146 }
Mikio Harab5dc8722012-03-06 00:13:10 +0900147 return laddr.family(), false
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400148 }
149
150 if (laddr == nil || laddr.family() == syscall.AF_INET) &&
151 (raddr == nil || raddr.family() == syscall.AF_INET) {
Mikio Harab5dc8722012-03-06 00:13:10 +0900152 return syscall.AF_INET, false
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400153 }
Mikio Harab5dc8722012-03-06 00:13:10 +0900154 return syscall.AF_INET6, false
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400155}
156
Mikio Harab5dc8722012-03-06 00:13:10 +0900157// Internet sockets (TCP, UDP, IP)
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400158
Mikio Hara5c055e72014-08-05 06:10:46 +0900159func 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 +0900160 family, ipv6only := favoriteAddrFamily(net, laddr, raddr, mode)
Mikio Hara5c055e72014-08-05 06:10:46 +0900161 return socket(net, family, sotype, proto, ipv6only, laddr, raddr, deadline)
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400162}
163
Mikio Harae8cf49f2012-11-27 00:45:42 +0900164func ipToSockaddr(family int, ip IP, port int, zone string) (syscall.Sockaddr, error) {
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400165 switch family {
166 case syscall.AF_INET:
167 if len(ip) == 0 {
168 ip = IPv4zero
169 }
170 if ip = ip.To4(); ip == nil {
Mikio Hara055ecb72015-04-21 21:20:15 +0900171 return nil, &AddrError{Err: "non-IPv4 address", Addr: ip.String()}
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400172 }
Mikio Harae8cf49f2012-11-27 00:45:42 +0900173 sa := new(syscall.SockaddrInet4)
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400174 for i := 0; i < IPv4len; i++ {
Mikio Harae8cf49f2012-11-27 00:45:42 +0900175 sa.Addr[i] = ip[i]
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400176 }
Mikio Harae8cf49f2012-11-27 00:45:42 +0900177 sa.Port = port
178 return sa, nil
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400179 case syscall.AF_INET6:
180 if len(ip) == 0 {
181 ip = IPv6zero
182 }
183 // IPv4 callers use 0.0.0.0 to mean "announce on any available address".
184 // In IPv6 mode, Linux treats that as meaning "announce on 0.0.0.0",
Mikio Harab5dc8722012-03-06 00:13:10 +0900185 // which it refuses to do. Rewrite to the IPv6 unspecified address.
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400186 if ip.Equal(IPv4zero) {
187 ip = IPv6zero
188 }
189 if ip = ip.To16(); ip == nil {
Mikio Hara055ecb72015-04-21 21:20:15 +0900190 return nil, &AddrError{Err: "non-IPv6 address", Addr: ip.String()}
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400191 }
Mikio Harae8cf49f2012-11-27 00:45:42 +0900192 sa := new(syscall.SockaddrInet6)
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400193 for i := 0; i < IPv6len; i++ {
Mikio Harae8cf49f2012-11-27 00:45:42 +0900194 sa.Addr[i] = ip[i]
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400195 }
Mikio Harae8cf49f2012-11-27 00:45:42 +0900196 sa.Port = port
197 sa.ZoneId = uint32(zoneToInt(zone))
198 return sa, nil
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400199 }
Mikio Hara055ecb72015-04-21 21:20:15 +0900200 return nil, &AddrError{Err: "invalid address family", Addr: ip.String()}
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400201}