fib.go 3.3 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. "github.com/google/nftables/binaryutil"
  18. "github.com/mdlayher/netlink"
  19. "golang.org/x/sys/unix"
  20. )
  21. // Fib defines fib expression structure
  22. type Fib struct {
  23. Register uint32
  24. ResultOIF bool
  25. ResultOIFNAME bool
  26. ResultADDRTYPE bool
  27. FlagSADDR bool
  28. FlagDADDR bool
  29. FlagMARK bool
  30. FlagIIF bool
  31. FlagOIF bool
  32. FlagPRESENT bool
  33. }
  34. func (e *Fib) marshal(fam byte) ([]byte, error) {
  35. data := []byte{}
  36. reg, err := netlink.MarshalAttributes([]netlink.Attribute{
  37. {Type: unix.NFTA_FIB_DREG, Data: binaryutil.BigEndian.PutUint32(e.Register)},
  38. })
  39. if err != nil {
  40. return nil, err
  41. }
  42. data = append(data, reg...)
  43. flags := uint32(0)
  44. if e.FlagSADDR {
  45. flags |= unix.NFTA_FIB_F_SADDR
  46. }
  47. if e.FlagDADDR {
  48. flags |= unix.NFTA_FIB_F_DADDR
  49. }
  50. if e.FlagMARK {
  51. flags |= unix.NFTA_FIB_F_MARK
  52. }
  53. if e.FlagIIF {
  54. flags |= unix.NFTA_FIB_F_IIF
  55. }
  56. if e.FlagOIF {
  57. flags |= unix.NFTA_FIB_F_OIF
  58. }
  59. if e.FlagPRESENT {
  60. flags |= unix.NFTA_FIB_F_PRESENT
  61. }
  62. if flags != 0 {
  63. flg, err := netlink.MarshalAttributes([]netlink.Attribute{
  64. {Type: unix.NFTA_FIB_FLAGS, Data: binaryutil.BigEndian.PutUint32(flags)},
  65. })
  66. if err != nil {
  67. return nil, err
  68. }
  69. data = append(data, flg...)
  70. }
  71. results := uint32(0)
  72. if e.ResultOIF {
  73. results |= unix.NFT_FIB_RESULT_OIF
  74. }
  75. if e.ResultOIFNAME {
  76. results |= unix.NFT_FIB_RESULT_OIFNAME
  77. }
  78. if e.ResultADDRTYPE {
  79. results |= unix.NFT_FIB_RESULT_ADDRTYPE
  80. }
  81. if results != 0 {
  82. rslt, err := netlink.MarshalAttributes([]netlink.Attribute{
  83. {Type: unix.NFTA_FIB_RESULT, Data: binaryutil.BigEndian.PutUint32(results)},
  84. })
  85. if err != nil {
  86. return nil, err
  87. }
  88. data = append(data, rslt...)
  89. }
  90. return netlink.MarshalAttributes([]netlink.Attribute{
  91. {Type: unix.NFTA_EXPR_NAME, Data: []byte("fib\x00")},
  92. {Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: data},
  93. })
  94. }
  95. func (e *Fib) unmarshal(fam byte, data []byte) error {
  96. ad, err := netlink.NewAttributeDecoder(data)
  97. if err != nil {
  98. return err
  99. }
  100. ad.ByteOrder = binary.BigEndian
  101. for ad.Next() {
  102. switch ad.Type() {
  103. case unix.NFTA_FIB_DREG:
  104. e.Register = ad.Uint32()
  105. case unix.NFTA_FIB_RESULT:
  106. result := ad.Uint32()
  107. e.ResultOIF = (result & unix.NFT_FIB_RESULT_OIF) == 1
  108. e.ResultOIFNAME = (result & unix.NFT_FIB_RESULT_OIFNAME) == 1
  109. e.ResultADDRTYPE = (result & unix.NFT_FIB_RESULT_ADDRTYPE) == 1
  110. case unix.NFTA_FIB_FLAGS:
  111. flags := ad.Uint32()
  112. e.FlagSADDR = (flags & unix.NFTA_FIB_F_SADDR) == 1
  113. e.FlagDADDR = (flags & unix.NFTA_FIB_F_DADDR) == 1
  114. e.FlagMARK = (flags & unix.NFTA_FIB_F_MARK) == 1
  115. e.FlagIIF = (flags & unix.NFTA_FIB_F_IIF) == 1
  116. e.FlagOIF = (flags & unix.NFTA_FIB_F_OIF) == 1
  117. e.FlagPRESENT = (flags & unix.NFTA_FIB_F_PRESENT) == 1
  118. }
  119. }
  120. return ad.Err()
  121. }