ct.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2018 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. // CtKey specifies which piece of conntrack information should be loaded. See
  22. // also https://wiki.nftables.org/wiki-nftables/index.php/Matching_connection_tracking_stateful_metainformation
  23. type CtKey uint32
  24. // Possible CtKey values.
  25. const (
  26. CtKeySTATE CtKey = unix.NFT_CT_STATE
  27. CtKeyDIRECTION CtKey = unix.NFT_CT_DIRECTION
  28. CtKeySTATUS CtKey = unix.NFT_CT_STATUS
  29. CtKeyMARK CtKey = unix.NFT_CT_MARK
  30. CtKeySECMARK CtKey = unix.NFT_CT_SECMARK
  31. CtKeyEXPIRATION CtKey = unix.NFT_CT_EXPIRATION
  32. CtKeyHELPER CtKey = unix.NFT_CT_HELPER
  33. CtKeyL3PROTOCOL CtKey = unix.NFT_CT_L3PROTOCOL
  34. CtKeySRC CtKey = unix.NFT_CT_SRC
  35. CtKeyDST CtKey = unix.NFT_CT_DST
  36. CtKeyPROTOCOL CtKey = unix.NFT_CT_PROTOCOL
  37. CtKeyPROTOSRC CtKey = unix.NFT_CT_PROTO_SRC
  38. CtKeyPROTODST CtKey = unix.NFT_CT_PROTO_DST
  39. CtKeyLABELS CtKey = unix.NFT_CT_LABELS
  40. CtKeyPKTS CtKey = unix.NFT_CT_PKTS
  41. CtKeyBYTES CtKey = unix.NFT_CT_BYTES
  42. CtKeyAVGPKT CtKey = unix.NFT_CT_AVGPKT
  43. CtKeyZONE CtKey = unix.NFT_CT_ZONE
  44. CtKeyEVENTMASK CtKey = unix.NFT_CT_EVENTMASK
  45. // https://sources.debian.org/src//nftables/0.9.8-3/src/ct.c/?hl=39#L39
  46. CtStateBitINVALID uint32 = 1
  47. CtStateBitESTABLISHED uint32 = 2
  48. CtStateBitRELATED uint32 = 4
  49. CtStateBitNEW uint32 = 8
  50. CtStateBitUNTRACKED uint32 = 64
  51. )
  52. // Ct defines type for NFT connection tracking
  53. type Ct struct {
  54. Register uint32
  55. SourceRegister bool
  56. Key CtKey
  57. }
  58. func (e *Ct) marshal(fam byte) ([]byte, error) {
  59. regData := []byte{}
  60. exprData, err := netlink.MarshalAttributes(
  61. []netlink.Attribute{
  62. {Type: unix.NFTA_CT_KEY, Data: binaryutil.BigEndian.PutUint32(uint32(e.Key))},
  63. },
  64. )
  65. if err != nil {
  66. return nil, err
  67. }
  68. if e.SourceRegister {
  69. regData, err = netlink.MarshalAttributes(
  70. []netlink.Attribute{
  71. {Type: unix.NFTA_CT_SREG, Data: binaryutil.BigEndian.PutUint32(e.Register)},
  72. },
  73. )
  74. } else {
  75. regData, err = netlink.MarshalAttributes(
  76. []netlink.Attribute{
  77. {Type: unix.NFTA_CT_DREG, Data: binaryutil.BigEndian.PutUint32(e.Register)},
  78. },
  79. )
  80. }
  81. if err != nil {
  82. return nil, err
  83. }
  84. exprData = append(exprData, regData...)
  85. return netlink.MarshalAttributes([]netlink.Attribute{
  86. {Type: unix.NFTA_EXPR_NAME, Data: []byte("ct\x00")},
  87. {Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: exprData},
  88. })
  89. }
  90. func (e *Ct) unmarshal(fam byte, data []byte) error {
  91. ad, err := netlink.NewAttributeDecoder(data)
  92. if err != nil {
  93. return err
  94. }
  95. ad.ByteOrder = binary.BigEndian
  96. for ad.Next() {
  97. switch ad.Type() {
  98. case unix.NFTA_CT_KEY:
  99. e.Key = CtKey(ad.Uint32())
  100. case unix.NFTA_CT_DREG:
  101. e.Register = ad.Uint32()
  102. }
  103. }
  104. return ad.Err()
  105. }