blob: eaa24c580ec3f7cfc85a31dea2254dae49500fe8 [file] [log] [blame]
Dmitri Shuralyov5d997792016-11-07 15:05:57 -08001// Copyright 2013 The Go Authors. All rights reserved.
Mikio Haracdfc4ce2013-06-04 17:42:58 +09002// 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 Haraca657d02015-01-15 01:52:15 +09009// manipulation of IPv6 facilities.
10//
11// The IPv6 protocol is defined in RFC 2460.
Mikio Hara1fd77272016-11-15 18:12:36 +090012// Socket interface extensions are defined in RFC 3493, RFC 3542 and
13// RFC 3678.
Mikio Haraca657d02015-01-15 01:52:15 +090014// MLDv1 and MLDv2 are defined in RFC 2710 and RFC 3810.
15// Source-specific multicast is defined in RFC 4607.
Mikio Haracdfc4ce2013-06-04 17:42:58 +090016//
Mikio Hara45b0d822016-11-15 06:39:53 +090017// On Darwin, this package requires OS X Mavericks version 10.9 or
18// above, or equivalent.
19//
Mikio Haracdfc4ce2013-06-04 17:42:58 +090020//
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 Shuralyov357296a2016-11-07 15:02:51 -080025// that use the IPv6 transport. When a single TCP connection carrying
Mikio Haracdfc4ce2013-06-04 17:42:58 +090026// a data flow of multiple packets needs to indicate the flow is
Mikio Hara094f0372016-12-28 06:30:27 +090027// important, Conn is used to set the traffic class field on the IPv6
28// header for each packet.
Mikio Haracdfc4ce2013-06-04 17:42:58 +090029//
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 Harad96e6bb2014-11-22 11:23:12 +090044// class 1 low drop precedence, known as AF11 packets.
Mikio Haracdfc4ce2013-06-04 17:42:58 +090045//
Mikio Harad1498782015-09-26 16:59:53 +090046// if err := ipv6.NewConn(c).SetTrafficClass(0x28); err != nil {
Mikio Haracdfc4ce2013-06-04 17:42:58 +090047// // 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 Shuralyov357296a2016-11-07 15:02:51 -080060// IPv6 transport. A few network facilities must be prepared before
Mikio Haracdfc4ce2013-06-04 17:42:58 +090061// you begin multicasting, at a minimum joining network interfaces and
Mikio Hara6226a2f2013-06-29 10:23:53 +090062// multicast groups.
Mikio Haracdfc4ce2013-06-04 17:42:58 +090063//
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 Hara6226a2f2013-06-29 10:23:53 +090083// Second, the application joins multicast groups, starts listening to
Dmitri Shuralyov357296a2016-11-07 15:02:51 -080084// the groups on the specified network interfaces. Note that the
Mikio Hara6226a2f2013-06-29 10:23:53 +090085// 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 Haracdfc4ce2013-06-04 17:42:58 +090088//
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 Shuralyov357296a2016-11-07 15:02:51 -080098// between the protocol stack within the kernel. When the application
Mikio Haracdfc4ce2013-06-04 17:42:58 +090099// needs a destination address on an incoming packet,
Mikio Hara094f0372016-12-28 06:30:27 +0900100// SetControlMessage of PacketConn is used to enable control message
101// transmissions.
Mikio Haracdfc4ce2013-06-04 17:42:58 +0900102//
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 Harab71143c2015-07-24 21:14:54 +0900118// if rcm.Dst.Equal(group) {
Mikio Haracdfc4ce2013-06-04 17:42:58 +0900119// // 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 Harad1498782015-09-26 16:59:53 +0900128// p.SetTrafficClass(0x0)
Mikio Haracdfc4ce2013-06-04 17:42:58 +0900129// 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 Hara0d2c2e12015-12-15 09:19:55 +0900134// wcm := ipv6.ControlMessage{TrafficClass: 0xe0, HopLimit: 1}
Mikio Haracdfc4ce2013-06-04 17:42:58 +0900135// 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 Hara6226a2f2013-06-29 10:23:53 +0900146// An application that uses PacketConn may join multiple multicast
Dmitri Shuralyov357296a2016-11-07 15:02:51 -0800147// groups. For example, a UDP listener with port 1024 might join two
Mikio Hara6226a2f2013-06-29 10:23:53 +0900148// different groups across over two different network interfaces by
149// using:
Mikio Haracdfc4ce2013-06-04 17:42:58 +0900150//
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 Shuralyov357296a2016-11-07 15:02:51 -0800168// UDP port to join the same multicast group. The net package will
Mikio Haracdfc4ce2013-06-04 17:42:58 +0900169// 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 Harad96e6bb2014-11-22 11:23:12 +0900201//
202//
203// Source-specific multicasting
204//
205// An application that uses PacketConn on MLDv2 supported platform is
Mikio Haraca657d02015-01-15 01:52:15 +0900206// able to join source-specific multicast groups.
207// The application may use JoinSourceSpecificGroup and
Mikio Harad96e6bb2014-11-22 11:23:12 +0900208// 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 Symonds8aa6e202014-12-09 14:17:11 +1100241package ipv6 // import "golang.org/x/net/ipv6"
Mikio Haraf483ac32016-12-15 07:23:17 +0900242
243// BUG(mikio): This package is not implemented on NaCl and Plan 9.