1 // Copyright 2013 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.
15 "golang.org/x/net/icmp"
16 "golang.org/x/net/internal/iana"
17 "golang.org/x/net/internal/nettest"
18 "golang.org/x/net/ipv6"
21 var packetConnReadWriteMulticastUDPTests = []struct {
25 {"[ff02::]:0", &net.UDPAddr{IP: net.ParseIP("ff02::114")}, nil}, // see RFC 4727
27 {"[ff30::8000:0]:0", &net.UDPAddr{IP: net.ParseIP("ff30::8000:1")}, &net.UDPAddr{IP: net.IPv6loopback}}, // see RFC 5771
30 func TestPacketConnReadWriteMulticastUDP(t *testing.T) {
32 case "nacl", "plan9", "windows":
33 t.Skipf("not supported on %s", runtime.GOOS)
36 t.Skip("ipv6 is not supported")
38 if !nettest.SupportsIPv6MulticastDeliveryOnLoopback() {
39 t.Skipf("multicast delivery doesn't work correctly on %s", runtime.GOOS)
41 ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
43 t.Skipf("not available on %s", runtime.GOOS)
46 for _, tt := range packetConnReadWriteMulticastUDPTests {
47 c, err := net.ListenPacket("udp6", tt.addr)
54 grp.Port = c.LocalAddr().(*net.UDPAddr).Port
55 p := ipv6.NewPacketConn(c)
58 if err := p.JoinGroup(ifi, &grp); err != nil {
61 defer p.LeaveGroup(ifi, &grp)
63 if err := p.JoinSourceSpecificGroup(ifi, &grp, tt.src); err != nil {
65 case "freebsd", "linux":
66 default: // platforms that don't support MLDv2 fail here
67 t.Logf("not supported on %s", runtime.GOOS)
72 defer p.LeaveSourceSpecificGroup(ifi, &grp, tt.src)
74 if err := p.SetMulticastInterface(ifi); err != nil {
77 if _, err := p.MulticastInterface(); err != nil {
80 if err := p.SetMulticastLoopback(true); err != nil {
83 if _, err := p.MulticastLoopback(); err != nil {
87 cm := ipv6.ControlMessage{
88 TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
89 Src: net.IPv6loopback,
92 cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
93 wb := []byte("HELLO-R-U-THERE")
95 for i, toggle := range []bool{true, false, true} {
96 if err := p.SetControlMessage(cf, toggle); err != nil {
97 if nettest.ProtocolNotSupported(err) {
98 t.Logf("not supported on %s", runtime.GOOS)
103 if err := p.SetDeadline(time.Now().Add(200 * time.Millisecond)); err != nil {
107 if n, err := p.WriteTo(wb, &cm, &grp); err != nil {
109 } else if n != len(wb) {
112 rb := make([]byte, 128)
113 if n, _, _, err := p.ReadFrom(rb); err != nil {
115 } else if !bytes.Equal(rb[:n], wb) {
116 t.Fatalf("got %v; want %v", rb[:n], wb)
122 var packetConnReadWriteMulticastICMPTests = []struct {
125 {&net.IPAddr{IP: net.ParseIP("ff02::114")}, nil}, // see RFC 4727
127 {&net.IPAddr{IP: net.ParseIP("ff30::8000:1")}, &net.IPAddr{IP: net.IPv6loopback}}, // see RFC 5771
130 func TestPacketConnReadWriteMulticastICMP(t *testing.T) {
131 switch runtime.GOOS {
132 case "nacl", "plan9", "windows":
133 t.Skipf("not supported on %s", runtime.GOOS)
136 t.Skip("ipv6 is not supported")
138 if !nettest.SupportsIPv6MulticastDeliveryOnLoopback() {
139 t.Skipf("multicast delivery doesn't work correctly on %s", runtime.GOOS)
141 if m, ok := nettest.SupportsRawIPSocket(); !ok {
144 ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
146 t.Skipf("not available on %s", runtime.GOOS)
149 for _, tt := range packetConnReadWriteMulticastICMPTests {
150 c, err := net.ListenPacket("ip6:ipv6-icmp", "::")
156 pshicmp := icmp.IPv6PseudoHeader(c.LocalAddr().(*net.IPAddr).IP, tt.grp.IP)
157 p := ipv6.NewPacketConn(c)
160 if err := p.JoinGroup(ifi, tt.grp); err != nil {
163 defer p.LeaveGroup(ifi, tt.grp)
165 if err := p.JoinSourceSpecificGroup(ifi, tt.grp, tt.src); err != nil {
166 switch runtime.GOOS {
167 case "freebsd", "linux":
168 default: // platforms that don't support MLDv2 fail here
169 t.Logf("not supported on %s", runtime.GOOS)
174 defer p.LeaveSourceSpecificGroup(ifi, tt.grp, tt.src)
176 if err := p.SetMulticastInterface(ifi); err != nil {
179 if _, err := p.MulticastInterface(); err != nil {
182 if err := p.SetMulticastLoopback(true); err != nil {
185 if _, err := p.MulticastLoopback(); err != nil {
189 cm := ipv6.ControlMessage{
190 TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
191 Src: net.IPv6loopback,
194 cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
196 var f ipv6.ICMPFilter
198 f.Accept(ipv6.ICMPTypeEchoReply)
199 if err := p.SetICMPFilter(&f); err != nil {
204 for i, toggle := range []bool{true, false, true} {
207 if err := p.SetChecksum(true, 2); err != nil {
208 // Solaris never allows to
209 // modify ICMP properties.
210 if runtime.GOOS != "solaris" {
216 // Some platforms never allow to
217 // disable the kernel checksum
219 p.SetChecksum(false, -1)
221 wb, err := (&icmp.Message{
222 Type: ipv6.ICMPTypeEchoRequest, Code: 0,
224 ID: os.Getpid() & 0xffff, Seq: i + 1,
225 Data: []byte("HELLO-R-U-THERE"),
231 if err := p.SetControlMessage(cf, toggle); err != nil {
232 if nettest.ProtocolNotSupported(err) {
233 t.Logf("not supported on %s", runtime.GOOS)
238 if err := p.SetDeadline(time.Now().Add(200 * time.Millisecond)); err != nil {
242 if n, err := p.WriteTo(wb, &cm, tt.grp); err != nil {
244 } else if n != len(wb) {
245 t.Fatalf("got %v; want %v", n, len(wb))
247 rb := make([]byte, 128)
248 if n, _, _, err := p.ReadFrom(rb); err != nil {
249 switch runtime.GOOS {
250 case "darwin": // older darwin kernels have some limitation on receiving icmp packet through raw socket
251 t.Logf("not supported on %s", runtime.GOOS)
256 if m, err := icmp.ParseMessage(iana.ProtocolIPv6ICMP, rb[:n]); err != nil {
258 } else if m.Type != ipv6.ICMPTypeEchoReply || m.Code != 0 {
259 t.Fatalf("got type=%v, code=%v; want type=%v, code=%v", m.Type, m.Code, ipv6.ICMPTypeEchoReply, 0)