message.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2012 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 icmp provides basic functions for the manipulation of
  5. // messages used in the Internet Control Message Protocols,
  6. // ICMPv4 and ICMPv6.
  7. //
  8. // ICMPv4 and ICMPv6 are defined in RFC 792 and RFC 4443.
  9. // Multi-part message support for ICMP is defined in RFC 4884.
  10. // ICMP extensions for MPLS are defined in RFC 4950.
  11. // ICMP extensions for interface and next-hop identification are
  12. // defined in RFC 5837.
  13. // PROBE: A utility for probing interfaces is defined in RFC 8335.
  14. package icmp // import "golang.org/x/net/icmp"
  15. import (
  16. "encoding/binary"
  17. "errors"
  18. "net"
  19. "runtime"
  20. "golang.org/x/net/internal/iana"
  21. "golang.org/x/net/ipv4"
  22. "golang.org/x/net/ipv6"
  23. )
  24. // BUG(mikio): This package is not implemented on JS, NaCl and Plan 9.
  25. var (
  26. errInvalidConn = errors.New("invalid connection")
  27. errInvalidProtocol = errors.New("invalid protocol")
  28. errMessageTooShort = errors.New("message too short")
  29. errHeaderTooShort = errors.New("header too short")
  30. errBufferTooShort = errors.New("buffer too short")
  31. errInvalidBody = errors.New("invalid body")
  32. errNoExtension = errors.New("no extension")
  33. errInvalidExtension = errors.New("invalid extension")
  34. errNotImplemented = errors.New("not implemented on " + runtime.GOOS + "/" + runtime.GOARCH)
  35. )
  36. func checksum(b []byte) uint16 {
  37. csumcv := len(b) - 1 // checksum coverage
  38. s := uint32(0)
  39. for i := 0; i < csumcv; i += 2 {
  40. s += uint32(b[i+1])<<8 | uint32(b[i])
  41. }
  42. if csumcv&1 == 0 {
  43. s += uint32(b[csumcv])
  44. }
  45. s = s>>16 + s&0xffff
  46. s = s + s>>16
  47. return ^uint16(s)
  48. }
  49. // A Type represents an ICMP message type.
  50. type Type interface {
  51. Protocol() int
  52. }
  53. // A Message represents an ICMP message.
  54. type Message struct {
  55. Type Type // type, either ipv4.ICMPType or ipv6.ICMPType
  56. Code int // code
  57. Checksum int // checksum
  58. Body MessageBody // body
  59. }
  60. // Marshal returns the binary encoding of the ICMP message m.
  61. //
  62. // For an ICMPv4 message, the returned message always contains the
  63. // calculated checksum field.
  64. //
  65. // For an ICMPv6 message, the returned message contains the calculated
  66. // checksum field when psh is not nil, otherwise the kernel will
  67. // compute the checksum field during the message transmission.
  68. // When psh is not nil, it must be the pseudo header for IPv6.
  69. func (m *Message) Marshal(psh []byte) ([]byte, error) {
  70. var mtype byte
  71. switch typ := m.Type.(type) {
  72. case ipv4.ICMPType:
  73. mtype = byte(typ)
  74. case ipv6.ICMPType:
  75. mtype = byte(typ)
  76. default:
  77. return nil, errInvalidProtocol
  78. }
  79. b := []byte{mtype, byte(m.Code), 0, 0}
  80. proto := m.Type.Protocol()
  81. if proto == iana.ProtocolIPv6ICMP && psh != nil {
  82. b = append(psh, b...)
  83. }
  84. if m.Body != nil && m.Body.Len(proto) != 0 {
  85. mb, err := m.Body.Marshal(proto)
  86. if err != nil {
  87. return nil, err
  88. }
  89. b = append(b, mb...)
  90. }
  91. if proto == iana.ProtocolIPv6ICMP {
  92. if psh == nil { // cannot calculate checksum here
  93. return b, nil
  94. }
  95. off, l := 2*net.IPv6len, len(b)-len(psh)
  96. binary.BigEndian.PutUint32(b[off:off+4], uint32(l))
  97. }
  98. s := checksum(b)
  99. // Place checksum back in header; using ^= avoids the
  100. // assumption the checksum bytes are zero.
  101. b[len(psh)+2] ^= byte(s)
  102. b[len(psh)+3] ^= byte(s >> 8)
  103. return b[len(psh):], nil
  104. }
  105. var parseFns = map[Type]func(int, Type, []byte) (MessageBody, error){
  106. ipv4.ICMPTypeDestinationUnreachable: parseDstUnreach,
  107. ipv4.ICMPTypeTimeExceeded: parseTimeExceeded,
  108. ipv4.ICMPTypeParameterProblem: parseParamProb,
  109. ipv4.ICMPTypeEcho: parseEcho,
  110. ipv4.ICMPTypeEchoReply: parseEcho,
  111. ipv4.ICMPTypeExtendedEchoRequest: parseExtendedEchoRequest,
  112. ipv4.ICMPTypeExtendedEchoReply: parseExtendedEchoReply,
  113. ipv6.ICMPTypeDestinationUnreachable: parseDstUnreach,
  114. ipv6.ICMPTypePacketTooBig: parsePacketTooBig,
  115. ipv6.ICMPTypeTimeExceeded: parseTimeExceeded,
  116. ipv6.ICMPTypeParameterProblem: parseParamProb,
  117. ipv6.ICMPTypeEchoRequest: parseEcho,
  118. ipv6.ICMPTypeEchoReply: parseEcho,
  119. ipv6.ICMPTypeExtendedEchoRequest: parseExtendedEchoRequest,
  120. ipv6.ICMPTypeExtendedEchoReply: parseExtendedEchoReply,
  121. }
  122. // ParseMessage parses b as an ICMP message.
  123. // The provided proto must be either the ICMPv4 or ICMPv6 protocol
  124. // number.
  125. func ParseMessage(proto int, b []byte) (*Message, error) {
  126. if len(b) < 4 {
  127. return nil, errMessageTooShort
  128. }
  129. var err error
  130. m := &Message{Code: int(b[1]), Checksum: int(binary.BigEndian.Uint16(b[2:4]))}
  131. switch proto {
  132. case iana.ProtocolICMP:
  133. m.Type = ipv4.ICMPType(b[0])
  134. case iana.ProtocolIPv6ICMP:
  135. m.Type = ipv6.ICMPType(b[0])
  136. default:
  137. return nil, errInvalidProtocol
  138. }
  139. if fn, ok := parseFns[m.Type]; !ok {
  140. m.Body, err = parseRawBody(proto, b[4:])
  141. } else {
  142. m.Body, err = fn(proto, m.Type, b[4:])
  143. }
  144. if err != nil {
  145. return nil, err
  146. }
  147. return m, nil
  148. }