ipsec.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2012 Google, Inc. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the LICENSE file in the root of the source
  5. // tree.
  6. package layers
  7. import (
  8. "encoding/binary"
  9. "github.com/google/gopacket"
  10. )
  11. // IPSecAH is the authentication header for IPv4/6 defined in
  12. // http://tools.ietf.org/html/rfc2402
  13. type IPSecAH struct {
  14. // While the auth header can be used for both IPv4 and v6, its format is that of
  15. // an IPv6 extension (NextHeader, PayloadLength, etc...), so we use ipv6ExtensionBase
  16. // to build it.
  17. ipv6ExtensionBase
  18. Reserved uint16
  19. SPI, Seq uint32
  20. AuthenticationData []byte
  21. }
  22. // LayerType returns LayerTypeIPSecAH.
  23. func (i *IPSecAH) LayerType() gopacket.LayerType { return LayerTypeIPSecAH }
  24. func decodeIPSecAH(data []byte, p gopacket.PacketBuilder) error {
  25. i := &IPSecAH{
  26. ipv6ExtensionBase: ipv6ExtensionBase{
  27. NextHeader: IPProtocol(data[0]),
  28. HeaderLength: data[1],
  29. },
  30. Reserved: binary.BigEndian.Uint16(data[2:4]),
  31. SPI: binary.BigEndian.Uint32(data[4:8]),
  32. Seq: binary.BigEndian.Uint32(data[8:12]),
  33. }
  34. i.ActualLength = (int(i.HeaderLength) + 2) * 4
  35. i.AuthenticationData = data[12:i.ActualLength]
  36. i.Contents = data[:i.ActualLength]
  37. i.Payload = data[i.ActualLength:]
  38. p.AddLayer(i)
  39. return p.NextDecoder(i.NextHeader)
  40. }
  41. // IPSecESP is the encapsulating security payload defined in
  42. // http://tools.ietf.org/html/rfc2406
  43. type IPSecESP struct {
  44. BaseLayer
  45. SPI, Seq uint32
  46. // Encrypted contains the encrypted set of bytes sent in an ESP
  47. Encrypted []byte
  48. }
  49. // LayerType returns LayerTypeIPSecESP.
  50. func (i *IPSecESP) LayerType() gopacket.LayerType { return LayerTypeIPSecESP }
  51. func decodeIPSecESP(data []byte, p gopacket.PacketBuilder) error {
  52. i := &IPSecESP{
  53. BaseLayer: BaseLayer{data, nil},
  54. SPI: binary.BigEndian.Uint32(data[:4]),
  55. Seq: binary.BigEndian.Uint32(data[4:8]),
  56. Encrypted: data[8:],
  57. }
  58. p.AddLayer(i)
  59. return nil
  60. }