verdict.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. "bytes"
  17. "encoding/binary"
  18. "fmt"
  19. "github.com/google/nftables/binaryutil"
  20. "github.com/mdlayher/netlink"
  21. "golang.org/x/sys/unix"
  22. )
  23. // This code assembles the verdict structure, as expected by the
  24. // nftables netlink API.
  25. // For further information, consult:
  26. // - netfilter.h (Linux kernel)
  27. // - net/netfilter/nf_tables_api.c (Linux kernel)
  28. // - src/expr/data_reg.c (linbnftnl)
  29. type Verdict struct {
  30. Kind VerdictKind
  31. Chain string
  32. }
  33. type VerdictKind int64
  34. // Verdicts, as per netfilter.h and netfilter/nf_tables.h.
  35. const (
  36. VerdictReturn VerdictKind = iota - 5
  37. VerdictGoto
  38. VerdictJump
  39. VerdictBreak
  40. VerdictContinue
  41. VerdictDrop
  42. VerdictAccept
  43. VerdictStolen
  44. VerdictQueue
  45. VerdictRepeat
  46. VerdictStop
  47. )
  48. func (e *Verdict) marshal(fam byte) ([]byte, error) {
  49. // A verdict is a tree of netlink attributes structured as follows:
  50. // NFTA_LIST_ELEM | NLA_F_NESTED {
  51. // NFTA_EXPR_NAME { "immediate\x00" }
  52. // NFTA_EXPR_DATA | NLA_F_NESTED {
  53. // NFTA_IMMEDIATE_DREG { NFT_REG_VERDICT }
  54. // NFTA_IMMEDIATE_DATA | NLA_F_NESTED {
  55. // the verdict code
  56. // }
  57. // }
  58. // }
  59. attrs := []netlink.Attribute{
  60. {Type: unix.NFTA_VERDICT_CODE, Data: binaryutil.BigEndian.PutUint32(uint32(e.Kind))},
  61. }
  62. if e.Chain != "" {
  63. attrs = append(attrs, netlink.Attribute{Type: unix.NFTA_VERDICT_CHAIN, Data: []byte(e.Chain + "\x00")})
  64. }
  65. codeData, err := netlink.MarshalAttributes(attrs)
  66. if err != nil {
  67. return nil, err
  68. }
  69. immData, err := netlink.MarshalAttributes([]netlink.Attribute{
  70. {Type: unix.NLA_F_NESTED | unix.NFTA_DATA_VERDICT, Data: codeData},
  71. })
  72. if err != nil {
  73. return nil, err
  74. }
  75. data, err := netlink.MarshalAttributes([]netlink.Attribute{
  76. {Type: unix.NFTA_IMMEDIATE_DREG, Data: binaryutil.BigEndian.PutUint32(unix.NFT_REG_VERDICT)},
  77. {Type: unix.NLA_F_NESTED | unix.NFTA_IMMEDIATE_DATA, Data: immData},
  78. })
  79. if err != nil {
  80. return nil, err
  81. }
  82. return netlink.MarshalAttributes([]netlink.Attribute{
  83. {Type: unix.NFTA_EXPR_NAME, Data: []byte("immediate\x00")},
  84. {Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: data},
  85. })
  86. }
  87. func (e *Verdict) 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_IMMEDIATE_DATA:
  96. nestedAD, err := netlink.NewAttributeDecoder(ad.Bytes())
  97. if err != nil {
  98. return fmt.Errorf("nested NewAttributeDecoder() failed: %v", err)
  99. }
  100. for nestedAD.Next() {
  101. switch nestedAD.Type() {
  102. case unix.NFTA_DATA_VERDICT:
  103. e.Kind = VerdictKind(int32(binaryutil.BigEndian.Uint32(nestedAD.Bytes()[4:8])))
  104. if len(nestedAD.Bytes()) > 12 {
  105. e.Chain = string(bytes.Trim(nestedAD.Bytes()[12:], "\x00"))
  106. }
  107. }
  108. }
  109. if nestedAD.Err() != nil {
  110. return fmt.Errorf("decoding immediate: %v", nestedAD.Err())
  111. }
  112. }
  113. }
  114. return ad.Err()
  115. }