dynset.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2020 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. "time"
  18. "github.com/google/nftables/binaryutil"
  19. "github.com/google/nftables/internal/parseexprfunc"
  20. "github.com/mdlayher/netlink"
  21. "golang.org/x/sys/unix"
  22. )
  23. // Not yet supported by unix package
  24. // https://cs.opensource.google/go/x/sys/+/c6bc011c:unix/ztypes_linux.go;l=2027-2036
  25. const (
  26. NFTA_DYNSET_EXPRESSIONS = 0xa
  27. NFT_DYNSET_F_EXPR = (1 << 1)
  28. )
  29. // Dynset represent a rule dynamically adding or updating a set or a map based on an incoming packet.
  30. type Dynset struct {
  31. SrcRegKey uint32
  32. SrcRegData uint32
  33. SetID uint32
  34. SetName string
  35. Operation uint32
  36. Timeout time.Duration
  37. Invert bool
  38. Exprs []Any
  39. }
  40. func (e *Dynset) marshal(fam byte) ([]byte, error) {
  41. // See: https://git.netfilter.org/libnftnl/tree/src/expr/dynset.c
  42. var opAttrs []netlink.Attribute
  43. opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_SREG_KEY, Data: binaryutil.BigEndian.PutUint32(e.SrcRegKey)})
  44. if e.SrcRegData != 0 {
  45. opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_SREG_DATA, Data: binaryutil.BigEndian.PutUint32(e.SrcRegData)})
  46. }
  47. opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_OP, Data: binaryutil.BigEndian.PutUint32(e.Operation)})
  48. if e.Timeout != 0 {
  49. opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_TIMEOUT, Data: binaryutil.BigEndian.PutUint64(uint64(e.Timeout.Milliseconds()))})
  50. }
  51. var flags uint32
  52. if e.Invert {
  53. flags |= unix.NFT_DYNSET_F_INV
  54. }
  55. opAttrs = append(opAttrs,
  56. netlink.Attribute{Type: unix.NFTA_DYNSET_SET_NAME, Data: []byte(e.SetName + "\x00")},
  57. netlink.Attribute{Type: unix.NFTA_DYNSET_SET_ID, Data: binaryutil.BigEndian.PutUint32(e.SetID)})
  58. // Per https://git.netfilter.org/libnftnl/tree/src/expr/dynset.c?id=84d12cfacf8ddd857a09435f3d982ab6250d250c#n170
  59. if len(e.Exprs) > 0 {
  60. flags |= NFT_DYNSET_F_EXPR
  61. switch len(e.Exprs) {
  62. case 1:
  63. exprData, err := Marshal(fam, e.Exprs[0])
  64. if err != nil {
  65. return nil, err
  66. }
  67. opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_EXPR, Data: exprData})
  68. default:
  69. var elemAttrs []netlink.Attribute
  70. for _, ex := range e.Exprs {
  71. exprData, err := Marshal(fam, ex)
  72. if err != nil {
  73. return nil, err
  74. }
  75. elemAttrs = append(elemAttrs, netlink.Attribute{Type: unix.NFTA_LIST_ELEM, Data: exprData})
  76. }
  77. elemData, err := netlink.MarshalAttributes(elemAttrs)
  78. if err != nil {
  79. return nil, err
  80. }
  81. opAttrs = append(opAttrs, netlink.Attribute{Type: NFTA_DYNSET_EXPRESSIONS, Data: elemData})
  82. }
  83. }
  84. opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_FLAGS, Data: binaryutil.BigEndian.PutUint32(flags)})
  85. opData, err := netlink.MarshalAttributes(opAttrs)
  86. if err != nil {
  87. return nil, err
  88. }
  89. return netlink.MarshalAttributes([]netlink.Attribute{
  90. {Type: unix.NFTA_EXPR_NAME, Data: []byte("dynset\x00")},
  91. {Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: opData},
  92. })
  93. }
  94. func (e *Dynset) unmarshal(fam byte, data []byte) error {
  95. ad, err := netlink.NewAttributeDecoder(data)
  96. if err != nil {
  97. return err
  98. }
  99. ad.ByteOrder = binary.BigEndian
  100. for ad.Next() {
  101. switch ad.Type() {
  102. case unix.NFTA_DYNSET_SET_NAME:
  103. e.SetName = ad.String()
  104. case unix.NFTA_DYNSET_SET_ID:
  105. e.SetID = ad.Uint32()
  106. case unix.NFTA_DYNSET_SREG_KEY:
  107. e.SrcRegKey = ad.Uint32()
  108. case unix.NFTA_DYNSET_SREG_DATA:
  109. e.SrcRegData = ad.Uint32()
  110. case unix.NFTA_DYNSET_OP:
  111. e.Operation = ad.Uint32()
  112. case unix.NFTA_DYNSET_TIMEOUT:
  113. e.Timeout = time.Duration(time.Millisecond * time.Duration(ad.Uint64()))
  114. case unix.NFTA_DYNSET_FLAGS:
  115. e.Invert = (ad.Uint32() & unix.NFT_DYNSET_F_INV) != 0
  116. case unix.NFTA_DYNSET_EXPR:
  117. exprs, err := parseexprfunc.ParseExprBytesFunc(fam, ad, ad.Bytes())
  118. if err != nil {
  119. return err
  120. }
  121. e.setInterfaceExprs(exprs)
  122. case NFTA_DYNSET_EXPRESSIONS:
  123. exprs, err := parseexprfunc.ParseExprMsgFunc(fam, ad.Bytes())
  124. if err != nil {
  125. return err
  126. }
  127. e.setInterfaceExprs(exprs)
  128. }
  129. }
  130. return ad.Err()
  131. }
  132. func (e *Dynset) setInterfaceExprs(exprs []interface{}) {
  133. e.Exprs = make([]Any, len(exprs))
  134. for i := range exprs {
  135. e.Exprs[i] = exprs[i].(Any)
  136. }
  137. }