dup.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 Dup struct {
  22. RegAddr uint32
  23. RegDev uint32
  24. IsRegDevSet bool
  25. }
  26. func (e *Dup) marshal(fam byte) ([]byte, error) {
  27. attrs := []netlink.Attribute{
  28. {Type: unix.NFTA_DUP_SREG_ADDR, Data: binaryutil.BigEndian.PutUint32(e.RegAddr)},
  29. }
  30. if e.IsRegDevSet {
  31. attrs = append(attrs, netlink.Attribute{Type: unix.NFTA_DUP_SREG_DEV, Data: binaryutil.BigEndian.PutUint32(e.RegDev)})
  32. }
  33. data, err := netlink.MarshalAttributes(attrs)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return netlink.MarshalAttributes([]netlink.Attribute{
  38. {Type: unix.NFTA_EXPR_NAME, Data: []byte("dup\x00")},
  39. {Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: data},
  40. })
  41. }
  42. func (e *Dup) unmarshal(fam byte, data []byte) error {
  43. ad, err := netlink.NewAttributeDecoder(data)
  44. if err != nil {
  45. return err
  46. }
  47. ad.ByteOrder = binary.BigEndian
  48. for ad.Next() {
  49. switch ad.Type() {
  50. case unix.NFTA_DUP_SREG_ADDR:
  51. e.RegAddr = ad.Uint32()
  52. case unix.NFTA_DUP_SREG_DEV:
  53. e.RegDev = ad.Uint32()
  54. }
  55. }
  56. return ad.Err()
  57. }