icmp4.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // Copyright 2012 Google, Inc. All rights reserved.
  2. // Copyright 2009-2011 Andreas Krennmair. All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style license
  5. // that can be found in the LICENSE file in the root of the source
  6. // tree.
  7. package layers
  8. import (
  9. "encoding/binary"
  10. "errors"
  11. "fmt"
  12. "reflect"
  13. "github.com/google/gopacket"
  14. )
  15. const (
  16. ICMPv4TypeEchoReply = 0
  17. ICMPv4TypeDestinationUnreachable = 3
  18. ICMPv4TypeSourceQuench = 4
  19. ICMPv4TypeRedirect = 5
  20. ICMPv4TypeEchoRequest = 8
  21. ICMPv4TypeRouterAdvertisement = 9
  22. ICMPv4TypeRouterSolicitation = 10
  23. ICMPv4TypeTimeExceeded = 11
  24. ICMPv4TypeParameterProblem = 12
  25. ICMPv4TypeTimestampRequest = 13
  26. ICMPv4TypeTimestampReply = 14
  27. ICMPv4TypeInfoRequest = 15
  28. ICMPv4TypeInfoReply = 16
  29. ICMPv4TypeAddressMaskRequest = 17
  30. ICMPv4TypeAddressMaskReply = 18
  31. )
  32. const (
  33. // DestinationUnreachable
  34. ICMPv4CodeNet = 0
  35. ICMPv4CodeHost = 1
  36. ICMPv4CodeProtocol = 2
  37. ICMPv4CodePort = 3
  38. ICMPv4CodeFragmentationNeeded = 4
  39. ICMPv4CodeSourceRoutingFailed = 5
  40. ICMPv4CodeNetUnknown = 6
  41. ICMPv4CodeHostUnknown = 7
  42. ICMPv4CodeSourceIsolated = 8
  43. ICMPv4CodeNetAdminProhibited = 9
  44. ICMPv4CodeHostAdminProhibited = 10
  45. ICMPv4CodeNetTOS = 11
  46. ICMPv4CodeHostTOS = 12
  47. ICMPv4CodeCommAdminProhibited = 13
  48. ICMPv4CodeHostPrecedence = 14
  49. ICMPv4CodePrecedenceCutoff = 15
  50. // TimeExceeded
  51. ICMPv4CodeTTLExceeded = 0
  52. ICMPv4CodeFragmentReassemblyTimeExceeded = 1
  53. // ParameterProblem
  54. ICMPv4CodePointerIndicatesError = 0
  55. ICMPv4CodeMissingOption = 1
  56. ICMPv4CodeBadLength = 2
  57. // Redirect
  58. // ICMPv4CodeNet = same as for DestinationUnreachable
  59. // ICMPv4CodeHost = same as for DestinationUnreachable
  60. ICMPv4CodeTOSNet = 2
  61. ICMPv4CodeTOSHost = 3
  62. )
  63. type icmpv4TypeCodeInfoStruct struct {
  64. typeStr string
  65. codeStr *map[uint8]string
  66. }
  67. var (
  68. icmpv4TypeCodeInfo = map[uint8]icmpv4TypeCodeInfoStruct{
  69. ICMPv4TypeDestinationUnreachable: icmpv4TypeCodeInfoStruct{
  70. "DestinationUnreachable", &map[uint8]string{
  71. ICMPv4CodeNet: "Net",
  72. ICMPv4CodeHost: "Host",
  73. ICMPv4CodeProtocol: "Protocol",
  74. ICMPv4CodePort: "Port",
  75. ICMPv4CodeFragmentationNeeded: "FragmentationNeeded",
  76. ICMPv4CodeSourceRoutingFailed: "SourceRoutingFailed",
  77. ICMPv4CodeNetUnknown: "NetUnknown",
  78. ICMPv4CodeHostUnknown: "HostUnknown",
  79. ICMPv4CodeSourceIsolated: "SourceIsolated",
  80. ICMPv4CodeNetAdminProhibited: "NetAdminProhibited",
  81. ICMPv4CodeHostAdminProhibited: "HostAdminProhibited",
  82. ICMPv4CodeNetTOS: "NetTOS",
  83. ICMPv4CodeHostTOS: "HostTOS",
  84. ICMPv4CodeCommAdminProhibited: "CommAdminProhibited",
  85. ICMPv4CodeHostPrecedence: "HostPrecedence",
  86. ICMPv4CodePrecedenceCutoff: "PrecedenceCutoff",
  87. },
  88. },
  89. ICMPv4TypeTimeExceeded: icmpv4TypeCodeInfoStruct{
  90. "TimeExceeded", &map[uint8]string{
  91. ICMPv4CodeTTLExceeded: "TTLExceeded",
  92. ICMPv4CodeFragmentReassemblyTimeExceeded: "FragmentReassemblyTimeExceeded",
  93. },
  94. },
  95. ICMPv4TypeParameterProblem: icmpv4TypeCodeInfoStruct{
  96. "ParameterProblem", &map[uint8]string{
  97. ICMPv4CodePointerIndicatesError: "PointerIndicatesError",
  98. ICMPv4CodeMissingOption: "MissingOption",
  99. ICMPv4CodeBadLength: "BadLength",
  100. },
  101. },
  102. ICMPv4TypeSourceQuench: icmpv4TypeCodeInfoStruct{
  103. "SourceQuench", nil,
  104. },
  105. ICMPv4TypeRedirect: icmpv4TypeCodeInfoStruct{
  106. "Redirect", &map[uint8]string{
  107. ICMPv4CodeNet: "Net",
  108. ICMPv4CodeHost: "Host",
  109. ICMPv4CodeTOSNet: "TOS+Net",
  110. ICMPv4CodeTOSHost: "TOS+Host",
  111. },
  112. },
  113. ICMPv4TypeEchoRequest: icmpv4TypeCodeInfoStruct{
  114. "EchoRequest", nil,
  115. },
  116. ICMPv4TypeEchoReply: icmpv4TypeCodeInfoStruct{
  117. "EchoReply", nil,
  118. },
  119. ICMPv4TypeTimestampRequest: icmpv4TypeCodeInfoStruct{
  120. "TimestampRequest", nil,
  121. },
  122. ICMPv4TypeTimestampReply: icmpv4TypeCodeInfoStruct{
  123. "TimestampReply", nil,
  124. },
  125. ICMPv4TypeInfoRequest: icmpv4TypeCodeInfoStruct{
  126. "InfoRequest", nil,
  127. },
  128. ICMPv4TypeInfoReply: icmpv4TypeCodeInfoStruct{
  129. "InfoReply", nil,
  130. },
  131. ICMPv4TypeRouterSolicitation: icmpv4TypeCodeInfoStruct{
  132. "RouterSolicitation", nil,
  133. },
  134. ICMPv4TypeRouterAdvertisement: icmpv4TypeCodeInfoStruct{
  135. "RouterAdvertisement", nil,
  136. },
  137. ICMPv4TypeAddressMaskRequest: icmpv4TypeCodeInfoStruct{
  138. "AddressMaskRequest", nil,
  139. },
  140. ICMPv4TypeAddressMaskReply: icmpv4TypeCodeInfoStruct{
  141. "AddressMaskReply", nil,
  142. },
  143. }
  144. )
  145. type ICMPv4TypeCode uint16
  146. // Type returns the ICMPv4 type field.
  147. func (a ICMPv4TypeCode) Type() uint8 {
  148. return uint8(a >> 8)
  149. }
  150. // Code returns the ICMPv4 code field.
  151. func (a ICMPv4TypeCode) Code() uint8 {
  152. return uint8(a)
  153. }
  154. func (a ICMPv4TypeCode) String() string {
  155. t, c := a.Type(), a.Code()
  156. strInfo, ok := icmpv4TypeCodeInfo[t]
  157. if !ok {
  158. // Unknown ICMPv4 type field
  159. return fmt.Sprintf("%d(%d)", t, c)
  160. }
  161. typeStr := strInfo.typeStr
  162. if strInfo.codeStr == nil && c == 0 {
  163. // The ICMPv4 type does not make use of the code field
  164. return fmt.Sprintf("%s", strInfo.typeStr)
  165. }
  166. if strInfo.codeStr == nil && c != 0 {
  167. // The ICMPv4 type does not make use of the code field, but it is present anyway
  168. return fmt.Sprintf("%s(Code: %d)", typeStr, c)
  169. }
  170. codeStr, ok := (*strInfo.codeStr)[c]
  171. if !ok {
  172. // We don't know this ICMPv4 code; print the numerical value
  173. return fmt.Sprintf("%s(Code: %d)", typeStr, c)
  174. }
  175. return fmt.Sprintf("%s(%s)", typeStr, codeStr)
  176. }
  177. func (a ICMPv4TypeCode) GoString() string {
  178. t := reflect.TypeOf(a)
  179. return fmt.Sprintf("%s(%d, %d)", t.String(), a.Type(), a.Code())
  180. }
  181. // SerializeTo writes the ICMPv4TypeCode value to the 'bytes' buffer.
  182. func (a ICMPv4TypeCode) SerializeTo(bytes []byte) {
  183. binary.BigEndian.PutUint16(bytes, uint16(a))
  184. }
  185. // CreateICMPv4TypeCode is a convenience function to create an ICMPv4TypeCode
  186. // gopacket type from the ICMPv4 type and code values.
  187. func CreateICMPv4TypeCode(typ uint8, code uint8) ICMPv4TypeCode {
  188. return ICMPv4TypeCode(binary.BigEndian.Uint16([]byte{typ, code}))
  189. }
  190. // ICMPv4 is the layer for IPv4 ICMP packet data.
  191. type ICMPv4 struct {
  192. BaseLayer
  193. TypeCode ICMPv4TypeCode
  194. Checksum uint16
  195. Id uint16
  196. Seq uint16
  197. }
  198. // LayerType returns LayerTypeICMPv4.
  199. func (i *ICMPv4) LayerType() gopacket.LayerType { return LayerTypeICMPv4 }
  200. // DecodeFromBytes decodes the given bytes into this layer.
  201. func (i *ICMPv4) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  202. if len(data) < 8 {
  203. df.SetTruncated()
  204. return errors.New("ICMP layer less then 8 bytes for ICMPv4 packet")
  205. }
  206. i.TypeCode = CreateICMPv4TypeCode(data[0], data[1])
  207. i.Checksum = binary.BigEndian.Uint16(data[2:4])
  208. i.Id = binary.BigEndian.Uint16(data[4:6])
  209. i.Seq = binary.BigEndian.Uint16(data[6:8])
  210. i.BaseLayer = BaseLayer{data[:8], data[8:]}
  211. return nil
  212. }
  213. // SerializeTo writes the serialized form of this layer into the
  214. // SerializationBuffer, implementing gopacket.SerializableLayer.
  215. // See the docs for gopacket.SerializableLayer for more info.
  216. func (i *ICMPv4) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  217. bytes, err := b.PrependBytes(8)
  218. if err != nil {
  219. return err
  220. }
  221. i.TypeCode.SerializeTo(bytes)
  222. binary.BigEndian.PutUint16(bytes[4:], i.Id)
  223. binary.BigEndian.PutUint16(bytes[6:], i.Seq)
  224. if opts.ComputeChecksums {
  225. bytes[2] = 0
  226. bytes[3] = 0
  227. i.Checksum = tcpipChecksum(b.Bytes(), 0)
  228. }
  229. binary.BigEndian.PutUint16(bytes[2:], i.Checksum)
  230. return nil
  231. }
  232. // CanDecode returns the set of layer types that this DecodingLayer can decode.
  233. func (i *ICMPv4) CanDecode() gopacket.LayerClass {
  234. return LayerTypeICMPv4
  235. }
  236. // NextLayerType returns the layer type contained by this DecodingLayer.
  237. func (i *ICMPv4) NextLayerType() gopacket.LayerType {
  238. return gopacket.LayerTypePayload
  239. }
  240. func decodeICMPv4(data []byte, p gopacket.PacketBuilder) error {
  241. i := &ICMPv4{}
  242. return decodingLayerDecoder(i, data, p)
  243. }