Home | History | Annotate | Download | only in net
      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 darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
      6 
      7 // Internet protocol family sockets for POSIX
      8 
      9 package net
     10 
     11 import (
     12 	"runtime"
     13 	"syscall"
     14 	"time"
     15 )
     16 
     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 
     24 func probeIPv4Stack() bool {
     25 	s, err := socketFunc(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
     26 	switch err {
     27 	case syscall.EAFNOSUPPORT, syscall.EPROTONOSUPPORT:
     28 		return false
     29 	case nil:
     30 		closeFunc(s)
     31 	}
     32 	return true
     33 }
     34 
     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 {
     50 		laddr TCPAddr
     51 		value int
     52 	}{
     53 		// IPv6 communication capability
     54 		{laddr: TCPAddr{IP: ParseIP("::1")}, value: 1},
     55 		// IPv6 IPv4-mapped address communication capability
     56 		{laddr: TCPAddr{IP: IPv4(127, 0, 0, 1)}, value: 0},
     57 	}
     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 	}
     74 
     75 	for i := range probes {
     76 		s, err := socketFunc(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
     77 		if err != nil {
     78 			continue
     79 		}
     80 		defer closeFunc(s)
     81 		syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, probes[i].value)
     82 		sa, err := probes[i].laddr.sockaddr(syscall.AF_INET6)
     83 		if err != nil {
     84 			continue
     85 		}
     86 		if err := syscall.Bind(s, sa); err != nil {
     87 			continue
     88 		}
     89 		supps[i] = true
     90 	}
     91 
     92 	return supps[0], supps[1]
     93 }
     94 
     95 // favoriteAddrFamily returns the appropriate address family to
     96 // the given net, laddr, raddr and mode.  At first it figures
     97 // address family out from the net.  If mode indicates "listen"
     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) {
    132 	switch net[len(net)-1] {
    133 	case '4':
    134 		return syscall.AF_INET, false
    135 	case '6':
    136 		return syscall.AF_INET6, true
    137 	}
    138 
    139 	if mode == "listen" && (laddr == nil || laddr.isWildcard()) {
    140 		if supportsIPv4map {
    141 			return syscall.AF_INET6, false
    142 		}
    143 		if laddr == nil {
    144 			return syscall.AF_INET, false
    145 		}
    146 		return laddr.family(), false
    147 	}
    148 
    149 	if (laddr == nil || laddr.family() == syscall.AF_INET) &&
    150 		(raddr == nil || raddr.family() == syscall.AF_INET) {
    151 		return syscall.AF_INET, false
    152 	}
    153 	return syscall.AF_INET6, false
    154 }
    155 
    156 // Internet sockets (TCP, UDP, IP)
    157 
    158 func internetSocket(net string, laddr, raddr sockaddr, deadline time.Time, sotype, proto int, mode string) (fd *netFD, err error) {
    159 	family, ipv6only := favoriteAddrFamily(net, laddr, raddr, mode)
    160 	return socket(net, family, sotype, proto, ipv6only, laddr, raddr, deadline)
    161 }
    162 
    163 func ipToSockaddr(family int, ip IP, port int, zone string) (syscall.Sockaddr, error) {
    164 	switch family {
    165 	case syscall.AF_INET:
    166 		if len(ip) == 0 {
    167 			ip = IPv4zero
    168 		}
    169 		if ip = ip.To4(); ip == nil {
    170 			return nil, &AddrError{Err: "non-IPv4 address", Addr: ip.String()}
    171 		}
    172 		sa := new(syscall.SockaddrInet4)
    173 		for i := 0; i < IPv4len; i++ {
    174 			sa.Addr[i] = ip[i]
    175 		}
    176 		sa.Port = port
    177 		return sa, nil
    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",
    184 		// which it refuses to do.  Rewrite to the IPv6 unspecified address.
    185 		if ip.Equal(IPv4zero) {
    186 			ip = IPv6zero
    187 		}
    188 		if ip = ip.To16(); ip == nil {
    189 			return nil, &AddrError{Err: "non-IPv6 address", Addr: ip.String()}
    190 		}
    191 		sa := new(syscall.SockaddrInet6)
    192 		for i := 0; i < IPv6len; i++ {
    193 			sa.Addr[i] = ip[i]
    194 		}
    195 		sa.Port = port
    196 		sa.ZoneId = uint32(zoneToInt(zone))
    197 		return sa, nil
    198 	}
    199 	return nil, &AddrError{Err: "invalid address family", Addr: ip.String()}
    200 }
    201