payload.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 PayloadBase uint32
  22. type PayloadCsumType uint32
  23. type PayloadOperationType uint32
  24. // Possible PayloadBase values.
  25. const (
  26. PayloadBaseLLHeader PayloadBase = unix.NFT_PAYLOAD_LL_HEADER
  27. PayloadBaseNetworkHeader PayloadBase = unix.NFT_PAYLOAD_NETWORK_HEADER
  28. PayloadBaseTransportHeader PayloadBase = unix.NFT_PAYLOAD_TRANSPORT_HEADER
  29. )
  30. // Possible PayloadCsumType values.
  31. const (
  32. CsumTypeNone PayloadCsumType = unix.NFT_PAYLOAD_CSUM_NONE
  33. CsumTypeInet PayloadCsumType = unix.NFT_PAYLOAD_CSUM_INET
  34. )
  35. // Possible PayloadOperationType values.
  36. const (
  37. PayloadLoad PayloadOperationType = iota
  38. PayloadWrite
  39. )
  40. type Payload struct {
  41. OperationType PayloadOperationType
  42. DestRegister uint32
  43. SourceRegister uint32
  44. Base PayloadBase
  45. Offset uint32
  46. Len uint32
  47. CsumType PayloadCsumType
  48. CsumOffset uint32
  49. CsumFlags uint32
  50. }
  51. func (e *Payload) marshal(fam byte) ([]byte, error) {
  52. var attrs []netlink.Attribute
  53. if e.OperationType == PayloadWrite {
  54. attrs = []netlink.Attribute{
  55. {Type: unix.NFTA_PAYLOAD_SREG, Data: binaryutil.BigEndian.PutUint32(e.SourceRegister)},
  56. }
  57. } else {
  58. attrs = []netlink.Attribute{
  59. {Type: unix.NFTA_PAYLOAD_DREG, Data: binaryutil.BigEndian.PutUint32(e.DestRegister)},
  60. }
  61. }
  62. attrs = append(attrs,
  63. netlink.Attribute{Type: unix.NFTA_PAYLOAD_BASE, Data: binaryutil.BigEndian.PutUint32(uint32(e.Base))},
  64. netlink.Attribute{Type: unix.NFTA_PAYLOAD_OFFSET, Data: binaryutil.BigEndian.PutUint32(e.Offset)},
  65. netlink.Attribute{Type: unix.NFTA_PAYLOAD_LEN, Data: binaryutil.BigEndian.PutUint32(e.Len)},
  66. )
  67. if e.CsumType > 0 {
  68. attrs = append(attrs,
  69. netlink.Attribute{Type: unix.NFTA_PAYLOAD_CSUM_TYPE, Data: binaryutil.BigEndian.PutUint32(uint32(e.CsumType))},
  70. netlink.Attribute{Type: unix.NFTA_PAYLOAD_CSUM_OFFSET, Data: binaryutil.BigEndian.PutUint32(uint32(e.CsumOffset))},
  71. )
  72. if e.CsumFlags > 0 {
  73. attrs = append(attrs,
  74. netlink.Attribute{Type: unix.NFTA_PAYLOAD_CSUM_FLAGS, Data: binaryutil.BigEndian.PutUint32(e.CsumFlags)},
  75. )
  76. }
  77. }
  78. data, err := netlink.MarshalAttributes(attrs)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return netlink.MarshalAttributes([]netlink.Attribute{
  83. {Type: unix.NFTA_EXPR_NAME, Data: []byte("payload\x00")},
  84. {Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: data},
  85. })
  86. }
  87. func (e *Payload) unmarshal(fam byte, data []byte) error {
  88. ad, err := netlink.NewAttributeDecoder(data)
  89. if err != nil {
  90. return err
  91. }
  92. ad.ByteOrder = binary.BigEndian
  93. for ad.Next() {
  94. switch ad.Type() {
  95. case unix.NFTA_PAYLOAD_DREG:
  96. e.DestRegister = ad.Uint32()
  97. case unix.NFTA_PAYLOAD_SREG:
  98. e.SourceRegister = ad.Uint32()
  99. e.OperationType = PayloadWrite
  100. case unix.NFTA_PAYLOAD_BASE:
  101. e.Base = PayloadBase(ad.Uint32())
  102. case unix.NFTA_PAYLOAD_OFFSET:
  103. e.Offset = ad.Uint32()
  104. case unix.NFTA_PAYLOAD_LEN:
  105. e.Len = ad.Uint32()
  106. case unix.NFTA_PAYLOAD_CSUM_TYPE:
  107. e.CsumType = PayloadCsumType(ad.Uint32())
  108. case unix.NFTA_PAYLOAD_CSUM_OFFSET:
  109. e.CsumOffset = ad.Uint32()
  110. case unix.NFTA_PAYLOAD_CSUM_FLAGS:
  111. e.CsumFlags = ad.Uint32()
  112. }
  113. }
  114. return ad.Err()
  115. }