message.go 4.2 KB

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