multicast_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.
  4. package ipv6_test
  5. import (
  6. "bytes"
  7. "net"
  8. "os"
  9. "runtime"
  10. "testing"
  11. "time"
  12. "golang.org/x/net/icmp"
  13. "golang.org/x/net/internal/iana"
  14. "golang.org/x/net/ipv6"
  15. "golang.org/x/net/nettest"
  16. )
  17. var packetConnReadWriteMulticastUDPTests = []struct {
  18. addr string
  19. grp, src *net.UDPAddr
  20. }{
  21. {"[ff02::]:0", &net.UDPAddr{IP: net.ParseIP("ff02::114")}, nil}, // see RFC 4727
  22. {"[ff30::8000:0]:0", &net.UDPAddr{IP: net.ParseIP("ff30::8000:1")}, &net.UDPAddr{IP: net.IPv6loopback}}, // see RFC 5771
  23. }
  24. func TestPacketConnReadWriteMulticastUDP(t *testing.T) {
  25. switch runtime.GOOS {
  26. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  27. t.Skipf("not supported on %s", runtime.GOOS)
  28. }
  29. if !nettest.SupportsIPv6() {
  30. t.Skip("ipv6 is not supported")
  31. }
  32. if m, ok := supportsIPv6MulticastDeliveryOnLoopback(); !ok {
  33. t.Skip(m)
  34. }
  35. ifi, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
  36. if err != nil {
  37. t.Skipf("not available on %s", runtime.GOOS)
  38. }
  39. for _, tt := range packetConnReadWriteMulticastUDPTests {
  40. c, err := net.ListenPacket("udp6", tt.addr)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. defer c.Close()
  45. grp := *tt.grp
  46. grp.Port = c.LocalAddr().(*net.UDPAddr).Port
  47. p := ipv6.NewPacketConn(c)
  48. defer p.Close()
  49. if tt.src == nil {
  50. if err := p.JoinGroup(ifi, &grp); err != nil {
  51. t.Fatal(err)
  52. }
  53. defer p.LeaveGroup(ifi, &grp)
  54. } else {
  55. if err := p.JoinSourceSpecificGroup(ifi, &grp, tt.src); err != nil {
  56. switch runtime.GOOS {
  57. case "freebsd", "linux":
  58. default: // platforms that don't support MLDv2 fail here
  59. t.Logf("not supported on %s", runtime.GOOS)
  60. continue
  61. }
  62. t.Fatal(err)
  63. }
  64. defer p.LeaveSourceSpecificGroup(ifi, &grp, tt.src)
  65. }
  66. if err := p.SetMulticastInterface(ifi); err != nil {
  67. t.Fatal(err)
  68. }
  69. if _, err := p.MulticastInterface(); err != nil {
  70. t.Fatal(err)
  71. }
  72. if err := p.SetMulticastLoopback(true); err != nil {
  73. t.Fatal(err)
  74. }
  75. if _, err := p.MulticastLoopback(); err != nil {
  76. t.Fatal(err)
  77. }
  78. cm := ipv6.ControlMessage{
  79. TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
  80. Src: net.IPv6loopback,
  81. IfIndex: ifi.Index,
  82. }
  83. cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
  84. wb := []byte("HELLO-R-U-THERE")
  85. for i, toggle := range []bool{true, false, true} {
  86. if err := p.SetControlMessage(cf, toggle); err != nil {
  87. if protocolNotSupported(err) {
  88. t.Logf("not supported on %s", runtime.GOOS)
  89. continue
  90. }
  91. t.Fatal(err)
  92. }
  93. if err := p.SetDeadline(time.Now().Add(200 * time.Millisecond)); err != nil {
  94. t.Fatal(err)
  95. }
  96. cm.HopLimit = i + 1
  97. if n, err := p.WriteTo(wb, &cm, &grp); err != nil {
  98. t.Fatal(err)
  99. } else if n != len(wb) {
  100. t.Fatal(err)
  101. }
  102. rb := make([]byte, 128)
  103. if n, _, _, err := p.ReadFrom(rb); err != nil {
  104. t.Fatal(err)
  105. } else if !bytes.Equal(rb[:n], wb) {
  106. t.Fatalf("got %v; want %v", rb[:n], wb)
  107. }
  108. }
  109. }
  110. }
  111. var packetConnReadWriteMulticastICMPTests = []struct {
  112. grp, src *net.IPAddr
  113. }{
  114. {&net.IPAddr{IP: net.ParseIP("ff02::114")}, nil}, // see RFC 4727
  115. {&net.IPAddr{IP: net.ParseIP("ff30::8000:1")}, &net.IPAddr{IP: net.IPv6loopback}}, // see RFC 5771
  116. }
  117. func TestPacketConnReadWriteMulticastICMP(t *testing.T) {
  118. switch runtime.GOOS {
  119. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  120. t.Skipf("not supported on %s", runtime.GOOS)
  121. }
  122. if !nettest.SupportsIPv6() {
  123. t.Skip("ipv6 is not supported")
  124. }
  125. if m, ok := supportsIPv6MulticastDeliveryOnLoopback(); !ok {
  126. t.Skip(m)
  127. }
  128. if !nettest.SupportsRawSocket() {
  129. t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
  130. }
  131. ifi, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
  132. if err != nil {
  133. t.Skipf("not available on %s", runtime.GOOS)
  134. }
  135. for _, tt := range packetConnReadWriteMulticastICMPTests {
  136. c, err := net.ListenPacket("ip6:ipv6-icmp", "::")
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. defer c.Close()
  141. pshicmp := icmp.IPv6PseudoHeader(c.LocalAddr().(*net.IPAddr).IP, tt.grp.IP)
  142. p := ipv6.NewPacketConn(c)
  143. defer p.Close()
  144. if tt.src == nil {
  145. if err := p.JoinGroup(ifi, tt.grp); err != nil {
  146. t.Fatal(err)
  147. }
  148. defer p.LeaveGroup(ifi, tt.grp)
  149. } else {
  150. if err := p.JoinSourceSpecificGroup(ifi, tt.grp, tt.src); err != nil {
  151. switch runtime.GOOS {
  152. case "freebsd", "linux":
  153. default: // platforms that don't support MLDv2 fail here
  154. t.Logf("not supported on %s", runtime.GOOS)
  155. continue
  156. }
  157. t.Fatal(err)
  158. }
  159. defer p.LeaveSourceSpecificGroup(ifi, tt.grp, tt.src)
  160. }
  161. if err := p.SetMulticastInterface(ifi); err != nil {
  162. t.Fatal(err)
  163. }
  164. if _, err := p.MulticastInterface(); err != nil {
  165. t.Fatal(err)
  166. }
  167. if err := p.SetMulticastLoopback(true); err != nil {
  168. t.Fatal(err)
  169. }
  170. if _, err := p.MulticastLoopback(); err != nil {
  171. t.Fatal(err)
  172. }
  173. cm := ipv6.ControlMessage{
  174. TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
  175. Src: net.IPv6loopback,
  176. IfIndex: ifi.Index,
  177. }
  178. cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
  179. var f ipv6.ICMPFilter
  180. f.SetAll(true)
  181. f.Accept(ipv6.ICMPTypeEchoReply)
  182. if err := p.SetICMPFilter(&f); err != nil {
  183. t.Fatal(err)
  184. }
  185. var psh []byte
  186. for i, toggle := range []bool{true, false, true} {
  187. if toggle {
  188. psh = nil
  189. if err := p.SetChecksum(true, 2); err != nil {
  190. // Illumos and Solaris never allow
  191. // modification of ICMP properties.
  192. if runtime.GOOS != "illumos" && runtime.GOOS != "solaris" {
  193. t.Fatal(err)
  194. }
  195. }
  196. } else {
  197. psh = pshicmp
  198. // Some platforms never allow to
  199. // disable the kernel checksum
  200. // processing.
  201. p.SetChecksum(false, -1)
  202. }
  203. wb, err := (&icmp.Message{
  204. Type: ipv6.ICMPTypeEchoRequest, Code: 0,
  205. Body: &icmp.Echo{
  206. ID: os.Getpid() & 0xffff, Seq: i + 1,
  207. Data: []byte("HELLO-R-U-THERE"),
  208. },
  209. }).Marshal(psh)
  210. if err != nil {
  211. t.Fatal(err)
  212. }
  213. if err := p.SetControlMessage(cf, toggle); err != nil {
  214. if protocolNotSupported(err) {
  215. t.Logf("not supported on %s", runtime.GOOS)
  216. continue
  217. }
  218. t.Fatal(err)
  219. }
  220. if err := p.SetDeadline(time.Now().Add(200 * time.Millisecond)); err != nil {
  221. t.Fatal(err)
  222. }
  223. cm.HopLimit = i + 1
  224. if n, err := p.WriteTo(wb, &cm, tt.grp); err != nil {
  225. t.Fatal(err)
  226. } else if n != len(wb) {
  227. t.Fatalf("got %v; want %v", n, len(wb))
  228. }
  229. rb := make([]byte, 128)
  230. if n, _, _, err := p.ReadFrom(rb); err != nil {
  231. switch runtime.GOOS {
  232. case "darwin": // older darwin kernels have some limitation on receiving icmp packet through raw socket
  233. t.Logf("not supported on %s", runtime.GOOS)
  234. continue
  235. }
  236. t.Fatal(err)
  237. } else {
  238. if m, err := icmp.ParseMessage(iana.ProtocolIPv6ICMP, rb[:n]); err != nil {
  239. t.Fatal(err)
  240. } else if m.Type != ipv6.ICMPTypeEchoReply || m.Code != 0 {
  241. t.Fatalf("got type=%v, code=%v; want type=%v, code=%v", m.Type, m.Code, ipv6.ICMPTypeEchoReply, 0)
  242. }
  243. }
  244. }
  245. }
  246. }