log.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2019 Google LLC. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package expr
  15. import (
  16. "encoding/binary"
  17. "github.com/google/nftables/binaryutil"
  18. "github.com/mdlayher/netlink"
  19. "golang.org/x/sys/unix"
  20. )
  21. type LogLevel uint32
  22. const (
  23. // See https://git.netfilter.org/nftables/tree/include/linux/netfilter/nf_tables.h?id=5b364657a35f4e4cd5d220ba2a45303d729c8eca#n1226
  24. LogLevelEmerg LogLevel = iota
  25. LogLevelAlert
  26. LogLevelCrit
  27. LogLevelErr
  28. LogLevelWarning
  29. LogLevelNotice
  30. LogLevelInfo
  31. LogLevelDebug
  32. LogLevelAudit
  33. )
  34. type LogFlags uint32
  35. const (
  36. // See https://git.netfilter.org/nftables/tree/include/linux/netfilter/nf_log.h?id=5b364657a35f4e4cd5d220ba2a45303d729c8eca
  37. LogFlagsTCPSeq LogFlags = 0x01 << iota
  38. LogFlagsTCPOpt
  39. LogFlagsIPOpt
  40. LogFlagsUID
  41. LogFlagsNFLog
  42. LogFlagsMACDecode
  43. LogFlagsMask LogFlags = 0x2f
  44. )
  45. // Log defines type for NFT logging
  46. // See https://git.netfilter.org/libnftnl/tree/src/expr/log.c?id=09456c720e9c00eecc08e41ac6b7c291b3821ee5#n25
  47. type Log struct {
  48. Level LogLevel
  49. // Refers to log flags (flags all, flags ip options, ...)
  50. Flags LogFlags
  51. // Equivalent to expression flags.
  52. // Indicates that an option is set by setting a bit
  53. // on index referred by the NFTA_LOG_* value.
  54. // See https://cs.opensource.google/go/x/sys/+/3681064d:unix/ztypes_linux.go;l=2126;drc=3681064d51587c1db0324b3d5c23c2ddbcff6e8f
  55. Key uint32
  56. Snaplen uint32
  57. Group uint16
  58. QThreshold uint16
  59. // Log prefix string content
  60. Data []byte
  61. }
  62. func (e *Log) marshal(fam byte) ([]byte, error) {
  63. // Per https://git.netfilter.org/libnftnl/tree/src/expr/log.c?id=09456c720e9c00eecc08e41ac6b7c291b3821ee5#n129
  64. attrs := make([]netlink.Attribute, 0)
  65. if e.Key&(1<<unix.NFTA_LOG_GROUP) != 0 {
  66. attrs = append(attrs, netlink.Attribute{
  67. Type: unix.NFTA_LOG_GROUP,
  68. Data: binaryutil.BigEndian.PutUint16(e.Group),
  69. })
  70. }
  71. if e.Key&(1<<unix.NFTA_LOG_PREFIX) != 0 {
  72. prefix := append(e.Data, '\x00')
  73. attrs = append(attrs, netlink.Attribute{
  74. Type: unix.NFTA_LOG_PREFIX,
  75. Data: prefix,
  76. })
  77. }
  78. if e.Key&(1<<unix.NFTA_LOG_SNAPLEN) != 0 {
  79. attrs = append(attrs, netlink.Attribute{
  80. Type: unix.NFTA_LOG_SNAPLEN,
  81. Data: binaryutil.BigEndian.PutUint32(e.Snaplen),
  82. })
  83. }
  84. if e.Key&(1<<unix.NFTA_LOG_QTHRESHOLD) != 0 {
  85. attrs = append(attrs, netlink.Attribute{
  86. Type: unix.NFTA_LOG_QTHRESHOLD,
  87. Data: binaryutil.BigEndian.PutUint16(e.QThreshold),
  88. })
  89. }
  90. if e.Key&(1<<unix.NFTA_LOG_LEVEL) != 0 {
  91. attrs = append(attrs, netlink.Attribute{
  92. Type: unix.NFTA_LOG_LEVEL,
  93. Data: binaryutil.BigEndian.PutUint32(uint32(e.Level)),
  94. })
  95. }
  96. if e.Key&(1<<unix.NFTA_LOG_FLAGS) != 0 {
  97. attrs = append(attrs, netlink.Attribute{
  98. Type: unix.NFTA_LOG_FLAGS,
  99. Data: binaryutil.BigEndian.PutUint32(uint32(e.Flags)),
  100. })
  101. }
  102. data, err := netlink.MarshalAttributes(attrs)
  103. if err != nil {
  104. return nil, err
  105. }
  106. return netlink.MarshalAttributes([]netlink.Attribute{
  107. {Type: unix.NFTA_EXPR_NAME, Data: []byte("log\x00")},
  108. {Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: data},
  109. })
  110. }
  111. func (e *Log) unmarshal(fam byte, data []byte) error {
  112. ad, err := netlink.NewAttributeDecoder(data)
  113. if err != nil {
  114. return err
  115. }
  116. ad.ByteOrder = binary.BigEndian
  117. for ad.Next() {
  118. e.Key |= 1 << uint32(ad.Type())
  119. data := ad.Bytes()
  120. switch ad.Type() {
  121. case unix.NFTA_LOG_GROUP:
  122. e.Group = binaryutil.BigEndian.Uint16(data)
  123. case unix.NFTA_LOG_PREFIX:
  124. // Getting rid of \x00 at the end of string
  125. e.Data = data[:len(data)-1]
  126. case unix.NFTA_LOG_SNAPLEN:
  127. e.Snaplen = binaryutil.BigEndian.Uint32(data)
  128. case unix.NFTA_LOG_QTHRESHOLD:
  129. e.QThreshold = binaryutil.BigEndian.Uint16(data)
  130. case unix.NFTA_LOG_LEVEL:
  131. e.Level = LogLevel(binaryutil.BigEndian.Uint32(data))
  132. case unix.NFTA_LOG_FLAGS:
  133. e.Flags = LogFlags(binaryutil.BigEndian.Uint32(data))
  134. }
  135. }
  136. return ad.Err()
  137. }