message.go 4.8 KB

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