limit.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "errors"
  18. "fmt"
  19. "github.com/google/nftables/binaryutil"
  20. "github.com/mdlayher/netlink"
  21. "golang.org/x/sys/unix"
  22. )
  23. // LimitType represents the type of the limit expression.
  24. type LimitType uint32
  25. // Imported from the nft_limit_type enum in netfilter/nf_tables.h.
  26. const (
  27. LimitTypePkts LimitType = unix.NFT_LIMIT_PKTS
  28. LimitTypePktBytes LimitType = unix.NFT_LIMIT_PKT_BYTES
  29. )
  30. // LimitTime represents the limit unit.
  31. type LimitTime uint64
  32. // Possible limit unit values.
  33. const (
  34. LimitTimeSecond LimitTime = 1
  35. LimitTimeMinute LimitTime = 60
  36. LimitTimeHour LimitTime = 60 * 60
  37. LimitTimeDay LimitTime = 60 * 60 * 24
  38. LimitTimeWeek LimitTime = 60 * 60 * 24 * 7
  39. )
  40. func limitTime(value uint64) (LimitTime, error) {
  41. switch LimitTime(value) {
  42. case LimitTimeSecond:
  43. return LimitTimeSecond, nil
  44. case LimitTimeMinute:
  45. return LimitTimeMinute, nil
  46. case LimitTimeHour:
  47. return LimitTimeHour, nil
  48. case LimitTimeDay:
  49. return LimitTimeDay, nil
  50. case LimitTimeWeek:
  51. return LimitTimeWeek, nil
  52. default:
  53. return 0, fmt.Errorf("expr: invalid limit unit value %d", value)
  54. }
  55. }
  56. // Limit represents a rate limit expression.
  57. type Limit struct {
  58. Type LimitType
  59. Rate uint64
  60. Over bool
  61. Unit LimitTime
  62. Burst uint32
  63. }
  64. func (l *Limit) marshal(fam byte) ([]byte, error) {
  65. var flags uint32
  66. if l.Over {
  67. flags = unix.NFT_LIMIT_F_INV
  68. }
  69. attrs := []netlink.Attribute{
  70. {Type: unix.NFTA_LIMIT_RATE, Data: binaryutil.BigEndian.PutUint64(l.Rate)},
  71. {Type: unix.NFTA_LIMIT_UNIT, Data: binaryutil.BigEndian.PutUint64(uint64(l.Unit))},
  72. {Type: unix.NFTA_LIMIT_BURST, Data: binaryutil.BigEndian.PutUint32(l.Burst)},
  73. {Type: unix.NFTA_LIMIT_TYPE, Data: binaryutil.BigEndian.PutUint32(uint32(l.Type))},
  74. {Type: unix.NFTA_LIMIT_FLAGS, Data: binaryutil.BigEndian.PutUint32(flags)},
  75. }
  76. data, err := netlink.MarshalAttributes(attrs)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return netlink.MarshalAttributes([]netlink.Attribute{
  81. {Type: unix.NFTA_EXPR_NAME, Data: []byte("limit\x00")},
  82. {Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: data},
  83. })
  84. }
  85. func (l *Limit) unmarshal(fam byte, data []byte) error {
  86. ad, err := netlink.NewAttributeDecoder(data)
  87. if err != nil {
  88. return err
  89. }
  90. ad.ByteOrder = binary.BigEndian
  91. for ad.Next() {
  92. switch ad.Type() {
  93. case unix.NFTA_LIMIT_RATE:
  94. l.Rate = ad.Uint64()
  95. case unix.NFTA_LIMIT_UNIT:
  96. l.Unit, err = limitTime(ad.Uint64())
  97. if err != nil {
  98. return err
  99. }
  100. case unix.NFTA_LIMIT_BURST:
  101. l.Burst = ad.Uint32()
  102. case unix.NFTA_LIMIT_TYPE:
  103. l.Type = LimitType(ad.Uint32())
  104. if l.Type != LimitTypePkts && l.Type != LimitTypePktBytes {
  105. return fmt.Errorf("expr: invalid limit type %d", l.Type)
  106. }
  107. case unix.NFTA_LIMIT_FLAGS:
  108. l.Over = (ad.Uint32() & unix.NFT_LIMIT_F_INV) == 1
  109. default:
  110. return errors.New("expr: unhandled limit netlink attribute")
  111. }
  112. }
  113. return ad.Err()
  114. }