pflog.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. "errors"
  10. "github.com/google/gopacket"
  11. )
  12. type PFDirection uint8
  13. const (
  14. PFDirectionInOut PFDirection = 0
  15. PFDirectionIn PFDirection = 1
  16. PFDirectionOut PFDirection = 2
  17. )
  18. // PFLog provides the layer for 'pf' packet-filter logging, as described at
  19. // http://www.freebsd.org/cgi/man.cgi?query=pflog&sektion=4
  20. type PFLog struct {
  21. BaseLayer
  22. Length uint8
  23. Family ProtocolFamily
  24. Action, Reason uint8
  25. IFName, Ruleset []byte
  26. RuleNum, SubruleNum uint32
  27. UID uint32
  28. PID int32
  29. RuleUID uint32
  30. RulePID int32
  31. Direction PFDirection
  32. // The remainder is padding
  33. }
  34. func (pf *PFLog) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  35. pf.Length = data[0]
  36. pf.Family = ProtocolFamily(data[1])
  37. pf.Action = data[2]
  38. pf.Reason = data[3]
  39. pf.IFName = data[4:20]
  40. pf.Ruleset = data[20:36]
  41. pf.RuleNum = binary.BigEndian.Uint32(data[36:40])
  42. pf.SubruleNum = binary.BigEndian.Uint32(data[40:44])
  43. pf.UID = binary.BigEndian.Uint32(data[44:48])
  44. pf.PID = int32(binary.BigEndian.Uint32(data[48:52]))
  45. pf.RuleUID = binary.BigEndian.Uint32(data[52:56])
  46. pf.RulePID = int32(binary.BigEndian.Uint32(data[56:60]))
  47. pf.Direction = PFDirection(data[60])
  48. if pf.Length%4 != 1 {
  49. return errors.New("PFLog header length should be 3 less than multiple of 4")
  50. }
  51. actualLength := int(pf.Length) + 3
  52. pf.Contents = data[:actualLength]
  53. pf.Payload = data[actualLength:]
  54. return nil
  55. }
  56. // LayerType returns layers.LayerTypePFLog
  57. func (pf *PFLog) LayerType() gopacket.LayerType { return LayerTypePFLog }
  58. func (pf *PFLog) CanDecode() gopacket.LayerClass { return LayerTypePFLog }
  59. func (pf *PFLog) NextLayerType() gopacket.LayerType {
  60. return pf.Family.LayerType()
  61. }
  62. func decodePFLog(data []byte, p gopacket.PacketBuilder) error {
  63. pf := &PFLog{}
  64. return decodingLayerDecoder(pf, data, p)
  65. }