message.go 4.3 KB

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