message.go 3.8 KB

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