message.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 byte
  70. switch typ := m.Type.(type) {
  71. case ipv4.ICMPType:
  72. mtype = byte(typ)
  73. case ipv6.ICMPType:
  74. mtype = byte(typ)
  75. default:
  76. return nil, errInvalidProtocol
  77. }
  78. b := []byte{mtype, byte(m.Code), 0, 0}
  79. proto := m.Type.Protocol()
  80. if proto == iana.ProtocolIPv6ICMP && psh != nil {
  81. b = append(psh, b...)
  82. }
  83. if m.Body != nil && m.Body.Len(proto) != 0 {
  84. mb, err := m.Body.Marshal(proto)
  85. if err != nil {
  86. return nil, err
  87. }
  88. b = append(b, mb...)
  89. }
  90. if proto == iana.ProtocolIPv6ICMP {
  91. if psh == nil { // cannot calculate checksum here
  92. return b, nil
  93. }
  94. off, l := 2*net.IPv6len, len(b)-len(psh)
  95. binary.BigEndian.PutUint32(b[off:off+4], uint32(l))
  96. }
  97. s := checksum(b)
  98. // Place checksum back in header; using ^= avoids the
  99. // assumption the checksum bytes are zero.
  100. b[len(psh)+2] ^= byte(s)
  101. b[len(psh)+3] ^= byte(s >> 8)
  102. return b[len(psh):], nil
  103. }
  104. var parseFns = map[Type]func(int, Type, []byte) (MessageBody, error){
  105. ipv4.ICMPTypeDestinationUnreachable: parseDstUnreach,
  106. ipv4.ICMPTypeTimeExceeded: parseTimeExceeded,
  107. ipv4.ICMPTypeParameterProblem: parseParamProb,
  108. ipv4.ICMPTypeEcho: parseEcho,
  109. ipv4.ICMPTypeEchoReply: parseEcho,
  110. ipv4.ICMPTypeExtendedEchoRequest: parseExtendedEchoRequest,
  111. ipv4.ICMPTypeExtendedEchoReply: parseExtendedEchoReply,
  112. ipv6.ICMPTypeDestinationUnreachable: parseDstUnreach,
  113. ipv6.ICMPTypePacketTooBig: parsePacketTooBig,
  114. ipv6.ICMPTypeTimeExceeded: parseTimeExceeded,
  115. ipv6.ICMPTypeParameterProblem: parseParamProb,
  116. ipv6.ICMPTypeEchoRequest: parseEcho,
  117. ipv6.ICMPTypeEchoReply: parseEcho,
  118. ipv6.ICMPTypeExtendedEchoRequest: parseExtendedEchoRequest,
  119. ipv6.ICMPTypeExtendedEchoReply: parseExtendedEchoReply,
  120. }
  121. // ParseMessage parses b as an ICMP message.
  122. // The provided proto must be either the ICMPv4 or ICMPv6 protocol
  123. // number.
  124. func ParseMessage(proto int, b []byte) (*Message, error) {
  125. if len(b) < 4 {
  126. return nil, errMessageTooShort
  127. }
  128. var err error
  129. m := &Message{Code: int(b[1]), Checksum: int(binary.BigEndian.Uint16(b[2:4]))}
  130. switch proto {
  131. case iana.ProtocolICMP:
  132. m.Type = ipv4.ICMPType(b[0])
  133. case iana.ProtocolIPv6ICMP:
  134. m.Type = ipv6.ICMPType(b[0])
  135. default:
  136. return nil, errInvalidProtocol
  137. }
  138. if fn, ok := parseFns[m.Type]; !ok {
  139. m.Body, err = parseDefaultMessageBody(proto, b[4:])
  140. } else {
  141. m.Body, err = fn(proto, m.Type, b[4:])
  142. }
  143. if err != nil {
  144. return nil, err
  145. }
  146. return m, nil
  147. }