lookup.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // Lookup represents a match against the contents of a set.
  22. type Lookup struct {
  23. SourceRegister uint32
  24. DestRegister uint32
  25. IsDestRegSet bool
  26. SetID uint32
  27. SetName string
  28. Invert bool
  29. }
  30. func (e *Lookup) marshal(fam byte) ([]byte, error) {
  31. // See: https://git.netfilter.org/libnftnl/tree/src/expr/lookup.c?id=6dc1c3d8bb64077da7f3f28c7368fb087d10a492#n115
  32. var opAttrs []netlink.Attribute
  33. if e.SourceRegister != 0 {
  34. opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_LOOKUP_SREG, Data: binaryutil.BigEndian.PutUint32(e.SourceRegister)})
  35. }
  36. if e.IsDestRegSet {
  37. opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_LOOKUP_DREG, Data: binaryutil.BigEndian.PutUint32(e.DestRegister)})
  38. }
  39. if e.Invert {
  40. opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_LOOKUP_FLAGS, Data: binaryutil.BigEndian.PutUint32(unix.NFT_LOOKUP_F_INV)})
  41. }
  42. opAttrs = append(opAttrs,
  43. netlink.Attribute{Type: unix.NFTA_LOOKUP_SET, Data: []byte(e.SetName + "\x00")},
  44. netlink.Attribute{Type: unix.NFTA_LOOKUP_SET_ID, Data: binaryutil.BigEndian.PutUint32(e.SetID)},
  45. )
  46. opData, err := netlink.MarshalAttributes(opAttrs)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return netlink.MarshalAttributes([]netlink.Attribute{
  51. {Type: unix.NFTA_EXPR_NAME, Data: []byte("lookup\x00")},
  52. {Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: opData},
  53. })
  54. }
  55. func (e *Lookup) unmarshal(fam byte, data []byte) error {
  56. ad, err := netlink.NewAttributeDecoder(data)
  57. if err != nil {
  58. return err
  59. }
  60. ad.ByteOrder = binary.BigEndian
  61. for ad.Next() {
  62. switch ad.Type() {
  63. case unix.NFTA_LOOKUP_SET:
  64. e.SetName = ad.String()
  65. case unix.NFTA_LOOKUP_SET_ID:
  66. e.SetID = ad.Uint32()
  67. case unix.NFTA_LOOKUP_SREG:
  68. e.SourceRegister = ad.Uint32()
  69. case unix.NFTA_LOOKUP_DREG:
  70. e.DestRegister = ad.Uint32()
  71. e.IsDestRegSet = true
  72. case unix.NFTA_LOOKUP_FLAGS:
  73. e.Invert = (ad.Uint32() & unix.NFT_LOOKUP_F_INV) != 0
  74. }
  75. }
  76. return ad.Err()
  77. }