Dmitri Shuralyov | 5d99779 | 2016-11-07 15:05:57 -0800 | [diff] [blame] | 1 | // Copyright 2013 The Go Authors. All rights reserved. |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package ipv6 implements IP-level socket options for the Internet |
| 6 | // Protocol version 6. |
| 7 | // |
| 8 | // The package provides IP-level socket options that allow |
Mikio Hara | ca657d0 | 2015-01-15 01:52:15 +0900 | [diff] [blame] | 9 | // manipulation of IPv6 facilities. |
| 10 | // |
| 11 | // The IPv6 protocol is defined in RFC 2460. |
Mikio Hara | 1fd7727 | 2016-11-15 18:12:36 +0900 | [diff] [blame] | 12 | // Socket interface extensions are defined in RFC 3493, RFC 3542 and |
| 13 | // RFC 3678. |
Mikio Hara | ca657d0 | 2015-01-15 01:52:15 +0900 | [diff] [blame] | 14 | // MLDv1 and MLDv2 are defined in RFC 2710 and RFC 3810. |
| 15 | // Source-specific multicast is defined in RFC 4607. |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 16 | // |
Mikio Hara | 45b0d82 | 2016-11-15 06:39:53 +0900 | [diff] [blame] | 17 | // On Darwin, this package requires OS X Mavericks version 10.9 or |
| 18 | // above, or equivalent. |
| 19 | // |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 20 | // |
| 21 | // Unicasting |
| 22 | // |
| 23 | // The options for unicasting are available for net.TCPConn, |
| 24 | // net.UDPConn and net.IPConn which are created as network connections |
Dmitri Shuralyov | 357296a | 2016-11-07 15:02:51 -0800 | [diff] [blame] | 25 | // that use the IPv6 transport. When a single TCP connection carrying |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 26 | // a data flow of multiple packets needs to indicate the flow is |
Mikio Hara | 094f037 | 2016-12-28 06:30:27 +0900 | [diff] [blame] | 27 | // important, Conn is used to set the traffic class field on the IPv6 |
| 28 | // header for each packet. |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 29 | // |
| 30 | // ln, err := net.Listen("tcp6", "[::]:1024") |
| 31 | // if err != nil { |
| 32 | // // error handling |
| 33 | // } |
| 34 | // defer ln.Close() |
| 35 | // for { |
| 36 | // c, err := ln.Accept() |
| 37 | // if err != nil { |
| 38 | // // error handling |
| 39 | // } |
| 40 | // go func(c net.Conn) { |
| 41 | // defer c.Close() |
| 42 | // |
| 43 | // The outgoing packets will be labeled DiffServ assured forwarding |
Mikio Hara | d96e6bb | 2014-11-22 11:23:12 +0900 | [diff] [blame] | 44 | // class 1 low drop precedence, known as AF11 packets. |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 45 | // |
Mikio Hara | d149878 | 2015-09-26 16:59:53 +0900 | [diff] [blame] | 46 | // if err := ipv6.NewConn(c).SetTrafficClass(0x28); err != nil { |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 47 | // // error handling |
| 48 | // } |
| 49 | // if _, err := c.Write(data); err != nil { |
| 50 | // // error handling |
| 51 | // } |
| 52 | // }(c) |
| 53 | // } |
| 54 | // |
| 55 | // |
| 56 | // Multicasting |
| 57 | // |
| 58 | // The options for multicasting are available for net.UDPConn and |
| 59 | // net.IPconn which are created as network connections that use the |
Dmitri Shuralyov | 357296a | 2016-11-07 15:02:51 -0800 | [diff] [blame] | 60 | // IPv6 transport. A few network facilities must be prepared before |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 61 | // you begin multicasting, at a minimum joining network interfaces and |
Mikio Hara | 6226a2f | 2013-06-29 10:23:53 +0900 | [diff] [blame] | 62 | // multicast groups. |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 63 | // |
| 64 | // en0, err := net.InterfaceByName("en0") |
| 65 | // if err != nil { |
| 66 | // // error handling |
| 67 | // } |
| 68 | // en1, err := net.InterfaceByIndex(911) |
| 69 | // if err != nil { |
| 70 | // // error handling |
| 71 | // } |
| 72 | // group := net.ParseIP("ff02::114") |
| 73 | // |
| 74 | // First, an application listens to an appropriate address with an |
| 75 | // appropriate service port. |
| 76 | // |
| 77 | // c, err := net.ListenPacket("udp6", "[::]:1024") |
| 78 | // if err != nil { |
| 79 | // // error handling |
| 80 | // } |
| 81 | // defer c.Close() |
| 82 | // |
Mikio Hara | 6226a2f | 2013-06-29 10:23:53 +0900 | [diff] [blame] | 83 | // Second, the application joins multicast groups, starts listening to |
Dmitri Shuralyov | 357296a | 2016-11-07 15:02:51 -0800 | [diff] [blame] | 84 | // the groups on the specified network interfaces. Note that the |
Mikio Hara | 6226a2f | 2013-06-29 10:23:53 +0900 | [diff] [blame] | 85 | // service port for transport layer protocol does not matter with this |
| 86 | // operation as joining groups affects only network and link layer |
| 87 | // protocols, such as IPv6 and Ethernet. |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 88 | // |
| 89 | // p := ipv6.NewPacketConn(c) |
| 90 | // if err := p.JoinGroup(en0, &net.UDPAddr{IP: group}); err != nil { |
| 91 | // // error handling |
| 92 | // } |
| 93 | // if err := p.JoinGroup(en1, &net.UDPAddr{IP: group}); err != nil { |
| 94 | // // error handling |
| 95 | // } |
| 96 | // |
| 97 | // The application might set per packet control message transmissions |
Dmitri Shuralyov | 357296a | 2016-11-07 15:02:51 -0800 | [diff] [blame] | 98 | // between the protocol stack within the kernel. When the application |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 99 | // needs a destination address on an incoming packet, |
Mikio Hara | 094f037 | 2016-12-28 06:30:27 +0900 | [diff] [blame] | 100 | // SetControlMessage of PacketConn is used to enable control message |
| 101 | // transmissions. |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 102 | // |
| 103 | // if err := p.SetControlMessage(ipv6.FlagDst, true); err != nil { |
| 104 | // // error handling |
| 105 | // } |
| 106 | // |
| 107 | // The application could identify whether the received packets are |
| 108 | // of interest by using the control message that contains the |
| 109 | // destination address of the received packet. |
| 110 | // |
| 111 | // b := make([]byte, 1500) |
| 112 | // for { |
| 113 | // n, rcm, src, err := p.ReadFrom(b) |
| 114 | // if err != nil { |
| 115 | // // error handling |
| 116 | // } |
| 117 | // if rcm.Dst.IsMulticast() { |
Mikio Hara | b71143c | 2015-07-24 21:14:54 +0900 | [diff] [blame] | 118 | // if rcm.Dst.Equal(group) { |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 119 | // // joined group, do something |
| 120 | // } else { |
| 121 | // // unknown group, discard |
| 122 | // continue |
| 123 | // } |
| 124 | // } |
| 125 | // |
| 126 | // The application can also send both unicast and multicast packets. |
| 127 | // |
Mikio Hara | d149878 | 2015-09-26 16:59:53 +0900 | [diff] [blame] | 128 | // p.SetTrafficClass(0x0) |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 129 | // p.SetHopLimit(16) |
| 130 | // if _, err := p.WriteTo(data[:n], nil, src); err != nil { |
| 131 | // // error handling |
| 132 | // } |
| 133 | // dst := &net.UDPAddr{IP: group, Port: 1024} |
Mikio Hara | 0d2c2e1 | 2015-12-15 09:19:55 +0900 | [diff] [blame] | 134 | // wcm := ipv6.ControlMessage{TrafficClass: 0xe0, HopLimit: 1} |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 135 | // for _, ifi := range []*net.Interface{en0, en1} { |
| 136 | // wcm.IfIndex = ifi.Index |
| 137 | // if _, err := p.WriteTo(data[:n], &wcm, dst); err != nil { |
| 138 | // // error handling |
| 139 | // } |
| 140 | // } |
| 141 | // } |
| 142 | // |
| 143 | // |
| 144 | // More multicasting |
| 145 | // |
Mikio Hara | 6226a2f | 2013-06-29 10:23:53 +0900 | [diff] [blame] | 146 | // An application that uses PacketConn may join multiple multicast |
Dmitri Shuralyov | 357296a | 2016-11-07 15:02:51 -0800 | [diff] [blame] | 147 | // groups. For example, a UDP listener with port 1024 might join two |
Mikio Hara | 6226a2f | 2013-06-29 10:23:53 +0900 | [diff] [blame] | 148 | // different groups across over two different network interfaces by |
| 149 | // using: |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 150 | // |
| 151 | // c, err := net.ListenPacket("udp6", "[::]:1024") |
| 152 | // if err != nil { |
| 153 | // // error handling |
| 154 | // } |
| 155 | // defer c.Close() |
| 156 | // p := ipv6.NewPacketConn(c) |
| 157 | // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::1:114")}); err != nil { |
| 158 | // // error handling |
| 159 | // } |
| 160 | // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::2:114")}); err != nil { |
| 161 | // // error handling |
| 162 | // } |
| 163 | // if err := p.JoinGroup(en1, &net.UDPAddr{IP: net.ParseIP("ff02::2:114")}); err != nil { |
| 164 | // // error handling |
| 165 | // } |
| 166 | // |
| 167 | // It is possible for multiple UDP listeners that listen on the same |
Dmitri Shuralyov | 357296a | 2016-11-07 15:02:51 -0800 | [diff] [blame] | 168 | // UDP port to join the same multicast group. The net package will |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 169 | // provide a socket that listens to a wildcard address with reusable |
| 170 | // UDP port when an appropriate multicast address prefix is passed to |
| 171 | // the net.ListenPacket or net.ListenUDP. |
| 172 | // |
| 173 | // c1, err := net.ListenPacket("udp6", "[ff02::]:1024") |
| 174 | // if err != nil { |
| 175 | // // error handling |
| 176 | // } |
| 177 | // defer c1.Close() |
| 178 | // c2, err := net.ListenPacket("udp6", "[ff02::]:1024") |
| 179 | // if err != nil { |
| 180 | // // error handling |
| 181 | // } |
| 182 | // defer c2.Close() |
| 183 | // p1 := ipv6.NewPacketConn(c1) |
| 184 | // if err := p1.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::114")}); err != nil { |
| 185 | // // error handling |
| 186 | // } |
| 187 | // p2 := ipv6.NewPacketConn(c2) |
| 188 | // if err := p2.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::114")}); err != nil { |
| 189 | // // error handling |
| 190 | // } |
| 191 | // |
| 192 | // Also it is possible for the application to leave or rejoin a |
| 193 | // multicast group on the network interface. |
| 194 | // |
| 195 | // if err := p.LeaveGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::114")}); err != nil { |
| 196 | // // error handling |
| 197 | // } |
| 198 | // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff01::114")}); err != nil { |
| 199 | // // error handling |
| 200 | // } |
Mikio Hara | d96e6bb | 2014-11-22 11:23:12 +0900 | [diff] [blame] | 201 | // |
| 202 | // |
| 203 | // Source-specific multicasting |
| 204 | // |
| 205 | // An application that uses PacketConn on MLDv2 supported platform is |
Mikio Hara | ca657d0 | 2015-01-15 01:52:15 +0900 | [diff] [blame] | 206 | // able to join source-specific multicast groups. |
| 207 | // The application may use JoinSourceSpecificGroup and |
Mikio Hara | d96e6bb | 2014-11-22 11:23:12 +0900 | [diff] [blame] | 208 | // LeaveSourceSpecificGroup for the operation known as "include" mode, |
| 209 | // |
| 210 | // ssmgroup := net.UDPAddr{IP: net.ParseIP("ff32::8000:9")} |
| 211 | // ssmsource := net.UDPAddr{IP: net.ParseIP("fe80::cafe")} |
| 212 | // if err := p.JoinSourceSpecificGroup(en0, &ssmgroup, &ssmsource); err != nil { |
| 213 | // // error handling |
| 214 | // } |
| 215 | // if err := p.LeaveSourceSpecificGroup(en0, &ssmgroup, &ssmsource); err != nil { |
| 216 | // // error handling |
| 217 | // } |
| 218 | // |
| 219 | // or JoinGroup, ExcludeSourceSpecificGroup, |
| 220 | // IncludeSourceSpecificGroup and LeaveGroup for the operation known |
| 221 | // as "exclude" mode. |
| 222 | // |
| 223 | // exclsource := net.UDPAddr{IP: net.ParseIP("fe80::dead")} |
| 224 | // if err := p.JoinGroup(en0, &ssmgroup); err != nil { |
| 225 | // // error handling |
| 226 | // } |
| 227 | // if err := p.ExcludeSourceSpecificGroup(en0, &ssmgroup, &exclsource); err != nil { |
| 228 | // // error handling |
| 229 | // } |
| 230 | // if err := p.LeaveGroup(en0, &ssmgroup); err != nil { |
| 231 | // // error handling |
| 232 | // } |
| 233 | // |
| 234 | // Note that it depends on each platform implementation what happens |
| 235 | // when an application which runs on MLDv2 unsupported platform uses |
| 236 | // JoinSourceSpecificGroup and LeaveSourceSpecificGroup. |
| 237 | // In general the platform tries to fall back to conversations using |
| 238 | // MLDv1 and starts to listen to multicast traffic. |
| 239 | // In the fallback case, ExcludeSourceSpecificGroup and |
| 240 | // IncludeSourceSpecificGroup may return an error. |
David Symonds | 8aa6e20 | 2014-12-09 14:17:11 +1100 | [diff] [blame] | 241 | package ipv6 // import "golang.org/x/net/ipv6" |
Mikio Hara | f483ac3 | 2016-12-15 07:23:17 +0900 | [diff] [blame] | 242 | |
| 243 | // BUG(mikio): This package is not implemented on NaCl and Plan 9. |