message.go 4.7 KB

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