message.go 4.8 KB

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