message.go 4.3 KB

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