message.go 3.9 KB

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