Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 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 | |
Dave Cheney | 7c8280c | 2014-02-25 09:47:42 -0500 | [diff] [blame] | 5 | // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows |
Russ Cox | 2715956 | 2011-09-15 16:48:57 -0400 | [diff] [blame] | 6 | |
Mikio Hara | e8cf49f | 2012-11-27 00:45:42 +0900 | [diff] [blame] | 7 | // Internet protocol family sockets for POSIX |
| 8 | |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 9 | package net |
| 10 | |
Brad Fitzpatrick | ef6806f | 2012-11-08 10:35:16 -0600 | [diff] [blame] | 11 | import ( |
Mikio Hara | eeb64b7 | 2015-05-14 10:18:10 +0900 | [diff] [blame] | 12 | "runtime" |
Brad Fitzpatrick | ef6806f | 2012-11-08 10:35:16 -0600 | [diff] [blame] | 13 | "syscall" |
| 14 | "time" |
| 15 | ) |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 16 | |
Mikio Hara | eeb64b7 | 2015-05-14 10:18:10 +0900 | [diff] [blame] | 17 | // 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 Hara | f0291a8 | 2013-08-03 12:17:01 +0900 | [diff] [blame] | 24 | func probeIPv4Stack() bool { |
Mikio Hara | 29d1f3b | 2015-03-01 12:27:01 +0900 | [diff] [blame] | 25 | s, err := socketFunc(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP) |
Mikio Hara | f0291a8 | 2013-08-03 12:17:01 +0900 | [diff] [blame] | 26 | switch err { |
| 27 | case syscall.EAFNOSUPPORT, syscall.EPROTONOSUPPORT: |
| 28 | return false |
| 29 | case nil: |
Mikio Hara | 29d1f3b | 2015-03-01 12:27:01 +0900 | [diff] [blame] | 30 | closeFunc(s) |
Mikio Hara | f0291a8 | 2013-08-03 12:17:01 +0900 | [diff] [blame] | 31 | } |
| 32 | return true |
| 33 | } |
| 34 | |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 35 | // 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. |
| 48 | func probeIPv6Stack() (supportsIPv6, supportsIPv4map bool) { |
| 49 | var probes = []struct { |
Mikio Hara | c0a4ce5 | 2013-07-25 19:29:20 +0900 | [diff] [blame] | 50 | laddr TCPAddr |
Mikio Hara | a05ffd8 | 2014-03-28 13:27:51 +0900 | [diff] [blame] | 51 | value int |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 52 | }{ |
| 53 | // IPv6 communication capability |
Mikio Hara | a05ffd8 | 2014-03-28 13:27:51 +0900 | [diff] [blame] | 54 | {laddr: TCPAddr{IP: ParseIP("::1")}, value: 1}, |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 55 | // IPv6 IPv4-mapped address communication capability |
Mikio Hara | a05ffd8 | 2014-03-28 13:27:51 +0900 | [diff] [blame] | 56 | {laddr: TCPAddr{IP: IPv4(127, 0, 0, 1)}, value: 0}, |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 57 | } |
Mikio Hara | eeb64b7 | 2015-05-14 10:18:10 +0900 | [diff] [blame] | 58 | 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 Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 74 | |
| 75 | for i := range probes { |
Mikio Hara | 29d1f3b | 2015-03-01 12:27:01 +0900 | [diff] [blame] | 76 | s, err := socketFunc(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP) |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 77 | if err != nil { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 78 | continue |
| 79 | } |
Mikio Hara | 29d1f3b | 2015-03-01 12:27:01 +0900 | [diff] [blame] | 80 | defer closeFunc(s) |
Mikio Hara | a05ffd8 | 2014-03-28 13:27:51 +0900 | [diff] [blame] | 81 | syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, probes[i].value) |
Mikio Hara | c0a4ce5 | 2013-07-25 19:29:20 +0900 | [diff] [blame] | 82 | sa, err := probes[i].laddr.sockaddr(syscall.AF_INET6) |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 83 | if err != nil { |
| 84 | continue |
| 85 | } |
Mikio Hara | c0a4ce5 | 2013-07-25 19:29:20 +0900 | [diff] [blame] | 86 | if err := syscall.Bind(s, sa); err != nil { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 87 | continue |
| 88 | } |
Mikio Hara | eeb64b7 | 2015-05-14 10:18:10 +0900 | [diff] [blame] | 89 | supps[i] = true |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Mikio Hara | eeb64b7 | 2015-05-14 10:18:10 +0900 | [diff] [blame] | 92 | return supps[0], supps[1] |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // favoriteAddrFamily returns the appropriate address family to |
Mikio Hara | 6fbe805 | 2012-02-15 01:59:18 +0900 | [diff] [blame] | 96 | // the given net, laddr, raddr and mode. At first it figures |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 97 | // address family out from the net. If mode indicates "listen" |
Mikio Hara | b5dc872 | 2012-03-06 00:13:10 +0900 | [diff] [blame] | 98 | // 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. |
| 131 | func favoriteAddrFamily(net string, laddr, raddr sockaddr, mode string) (family int, ipv6only bool) { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 132 | switch net[len(net)-1] { |
| 133 | case '4': |
Mikio Hara | b5dc872 | 2012-03-06 00:13:10 +0900 | [diff] [blame] | 134 | return syscall.AF_INET, false |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 135 | case '6': |
Mikio Hara | b5dc872 | 2012-03-06 00:13:10 +0900 | [diff] [blame] | 136 | return syscall.AF_INET6, true |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Mikio Hara | b252fe7 | 2012-04-25 12:29:14 +0900 | [diff] [blame] | 139 | if mode == "listen" && (laddr == nil || laddr.isWildcard()) { |
Mikio Hara | b5dc872 | 2012-03-06 00:13:10 +0900 | [diff] [blame] | 140 | if supportsIPv4map { |
| 141 | return syscall.AF_INET6, false |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 142 | } |
Mikio Hara | b252fe7 | 2012-04-25 12:29:14 +0900 | [diff] [blame] | 143 | if laddr == nil { |
| 144 | return syscall.AF_INET, false |
| 145 | } |
Mikio Hara | b5dc872 | 2012-03-06 00:13:10 +0900 | [diff] [blame] | 146 | return laddr.family(), false |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | if (laddr == nil || laddr.family() == syscall.AF_INET) && |
| 150 | (raddr == nil || raddr.family() == syscall.AF_INET) { |
Mikio Hara | b5dc872 | 2012-03-06 00:13:10 +0900 | [diff] [blame] | 151 | return syscall.AF_INET, false |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 152 | } |
Mikio Hara | b5dc872 | 2012-03-06 00:13:10 +0900 | [diff] [blame] | 153 | return syscall.AF_INET6, false |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Mikio Hara | b5dc872 | 2012-03-06 00:13:10 +0900 | [diff] [blame] | 156 | // Internet sockets (TCP, UDP, IP) |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 157 | |
Mikio Hara | 5c055e7 | 2014-08-05 06:10:46 +0900 | [diff] [blame] | 158 | func internetSocket(net string, laddr, raddr sockaddr, deadline time.Time, sotype, proto int, mode string) (fd *netFD, err error) { |
Mikio Hara | b5dc872 | 2012-03-06 00:13:10 +0900 | [diff] [blame] | 159 | family, ipv6only := favoriteAddrFamily(net, laddr, raddr, mode) |
Mikio Hara | 5c055e7 | 2014-08-05 06:10:46 +0900 | [diff] [blame] | 160 | return socket(net, family, sotype, proto, ipv6only, laddr, raddr, deadline) |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 161 | } |
| 162 | |
Mikio Hara | e8cf49f | 2012-11-27 00:45:42 +0900 | [diff] [blame] | 163 | func ipToSockaddr(family int, ip IP, port int, zone string) (syscall.Sockaddr, error) { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 164 | switch family { |
| 165 | case syscall.AF_INET: |
| 166 | if len(ip) == 0 { |
| 167 | ip = IPv4zero |
| 168 | } |
| 169 | if ip = ip.To4(); ip == nil { |
Mikio Hara | 055ecb7 | 2015-04-21 21:20:15 +0900 | [diff] [blame] | 170 | return nil, &AddrError{Err: "non-IPv4 address", Addr: ip.String()} |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 171 | } |
Mikio Hara | e8cf49f | 2012-11-27 00:45:42 +0900 | [diff] [blame] | 172 | sa := new(syscall.SockaddrInet4) |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 173 | for i := 0; i < IPv4len; i++ { |
Mikio Hara | e8cf49f | 2012-11-27 00:45:42 +0900 | [diff] [blame] | 174 | sa.Addr[i] = ip[i] |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 175 | } |
Mikio Hara | e8cf49f | 2012-11-27 00:45:42 +0900 | [diff] [blame] | 176 | sa.Port = port |
| 177 | return sa, nil |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 178 | 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 Hara | b5dc872 | 2012-03-06 00:13:10 +0900 | [diff] [blame] | 184 | // which it refuses to do. Rewrite to the IPv6 unspecified address. |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 185 | if ip.Equal(IPv4zero) { |
| 186 | ip = IPv6zero |
| 187 | } |
| 188 | if ip = ip.To16(); ip == nil { |
Mikio Hara | 055ecb7 | 2015-04-21 21:20:15 +0900 | [diff] [blame] | 189 | return nil, &AddrError{Err: "non-IPv6 address", Addr: ip.String()} |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 190 | } |
Mikio Hara | e8cf49f | 2012-11-27 00:45:42 +0900 | [diff] [blame] | 191 | sa := new(syscall.SockaddrInet6) |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 192 | for i := 0; i < IPv6len; i++ { |
Mikio Hara | e8cf49f | 2012-11-27 00:45:42 +0900 | [diff] [blame] | 193 | sa.Addr[i] = ip[i] |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 194 | } |
Mikio Hara | e8cf49f | 2012-11-27 00:45:42 +0900 | [diff] [blame] | 195 | sa.Port = port |
| 196 | sa.ZoneId = uint32(zoneToInt(zone)) |
| 197 | return sa, nil |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 198 | } |
Mikio Hara | 055ecb7 | 2015-04-21 21:20:15 +0900 | [diff] [blame] | 199 | return nil, &AddrError{Err: "invalid address family", Addr: ip.String()} |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 200 | } |