bitwise.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. type Bitwise struct {
  22. SourceRegister uint32
  23. DestRegister uint32
  24. Len uint32
  25. Mask []byte
  26. Xor []byte
  27. }
  28. func (e *Bitwise) marshal(fam byte) ([]byte, error) {
  29. mask, err := netlink.MarshalAttributes([]netlink.Attribute{
  30. {Type: unix.NFTA_DATA_VALUE, Data: e.Mask},
  31. })
  32. if err != nil {
  33. return nil, err
  34. }
  35. xor, err := netlink.MarshalAttributes([]netlink.Attribute{
  36. {Type: unix.NFTA_DATA_VALUE, Data: e.Xor},
  37. })
  38. if err != nil {
  39. return nil, err
  40. }
  41. data, err := netlink.MarshalAttributes([]netlink.Attribute{
  42. {Type: unix.NFTA_BITWISE_SREG, Data: binaryutil.BigEndian.PutUint32(e.SourceRegister)},
  43. {Type: unix.NFTA_BITWISE_DREG, Data: binaryutil.BigEndian.PutUint32(e.DestRegister)},
  44. {Type: unix.NFTA_BITWISE_LEN, Data: binaryutil.BigEndian.PutUint32(e.Len)},
  45. {Type: unix.NLA_F_NESTED | unix.NFTA_BITWISE_MASK, Data: mask},
  46. {Type: unix.NLA_F_NESTED | unix.NFTA_BITWISE_XOR, Data: xor},
  47. })
  48. if err != nil {
  49. return nil, err
  50. }
  51. return netlink.MarshalAttributes([]netlink.Attribute{
  52. {Type: unix.NFTA_EXPR_NAME, Data: []byte("bitwise\x00")},
  53. {Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: data},
  54. })
  55. }
  56. func (e *Bitwise) unmarshal(fam byte, data []byte) error {
  57. ad, err := netlink.NewAttributeDecoder(data)
  58. if err != nil {
  59. return err
  60. }
  61. ad.ByteOrder = binary.BigEndian
  62. for ad.Next() {
  63. switch ad.Type() {
  64. case unix.NFTA_BITWISE_SREG:
  65. e.SourceRegister = ad.Uint32()
  66. case unix.NFTA_BITWISE_DREG:
  67. e.DestRegister = ad.Uint32()
  68. case unix.NFTA_BITWISE_LEN:
  69. e.Len = ad.Uint32()
  70. case unix.NFTA_BITWISE_MASK:
  71. // Since NFTA_BITWISE_MASK is nested, it requires additional decoding
  72. ad.Nested(func(nad *netlink.AttributeDecoder) error {
  73. for nad.Next() {
  74. switch nad.Type() {
  75. case unix.NFTA_DATA_VALUE:
  76. e.Mask = nad.Bytes()
  77. }
  78. }
  79. return nil
  80. })
  81. case unix.NFTA_BITWISE_XOR:
  82. // Since NFTA_BITWISE_XOR is nested, it requires additional decoding
  83. ad.Nested(func(nad *netlink.AttributeDecoder) error {
  84. for nad.Next() {
  85. switch nad.Type() {
  86. case unix.NFTA_DATA_VALUE:
  87. e.Xor = nad.Bytes()
  88. }
  89. }
  90. return nil
  91. })
  92. }
  93. }
  94. return ad.Err()
  95. }