nat.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 NATType uint32
  22. // Possible NATType values.
  23. const (
  24. NATTypeSourceNAT NATType = unix.NFT_NAT_SNAT
  25. NATTypeDestNAT NATType = unix.NFT_NAT_DNAT
  26. )
  27. type NAT struct {
  28. Type NATType
  29. Family uint32 // TODO: typed const
  30. RegAddrMin uint32
  31. RegAddrMax uint32
  32. RegProtoMin uint32
  33. RegProtoMax uint32
  34. Random bool
  35. FullyRandom bool
  36. Persistent bool
  37. }
  38. // |00048|N-|00001| |len |flags| type|
  39. // |00008|--|00001| |len |flags| type|
  40. // | 6e 61 74 00 | | data | n a t
  41. // |00036|N-|00002| |len |flags| type|
  42. // |00008|--|00001| |len |flags| type| NFTA_NAT_TYPE
  43. // | 00 00 00 01 | | data | NFT_NAT_DNAT
  44. // |00008|--|00002| |len |flags| type| NFTA_NAT_FAMILY
  45. // | 00 00 00 02 | | data | NFPROTO_IPV4
  46. // |00008|--|00003| |len |flags| type| NFTA_NAT_REG_ADDR_MIN
  47. // | 00 00 00 01 | | data | reg 1
  48. // |00008|--|00005| |len |flags| type| NFTA_NAT_REG_PROTO_MIN
  49. // | 00 00 00 02 | | data | reg 2
  50. func (e *NAT) marshal(fam byte) ([]byte, error) {
  51. attrs := []netlink.Attribute{
  52. {Type: unix.NFTA_NAT_TYPE, Data: binaryutil.BigEndian.PutUint32(uint32(e.Type))},
  53. {Type: unix.NFTA_NAT_FAMILY, Data: binaryutil.BigEndian.PutUint32(e.Family)},
  54. }
  55. if e.RegAddrMin != 0 {
  56. attrs = append(attrs, netlink.Attribute{Type: unix.NFTA_NAT_REG_ADDR_MIN, Data: binaryutil.BigEndian.PutUint32(e.RegAddrMin)})
  57. if e.RegAddrMax != 0 {
  58. attrs = append(attrs, netlink.Attribute{Type: unix.NFTA_NAT_REG_ADDR_MAX, Data: binaryutil.BigEndian.PutUint32(e.RegAddrMax)})
  59. }
  60. }
  61. if e.RegProtoMin != 0 {
  62. attrs = append(attrs, netlink.Attribute{Type: unix.NFTA_NAT_REG_PROTO_MIN, Data: binaryutil.BigEndian.PutUint32(e.RegProtoMin)})
  63. if e.RegProtoMax != 0 {
  64. attrs = append(attrs, netlink.Attribute{Type: unix.NFTA_NAT_REG_PROTO_MAX, Data: binaryutil.BigEndian.PutUint32(e.RegProtoMax)})
  65. }
  66. }
  67. flags := uint32(0)
  68. if e.Random {
  69. flags |= NF_NAT_RANGE_PROTO_RANDOM
  70. }
  71. if e.FullyRandom {
  72. flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY
  73. }
  74. if e.Persistent {
  75. flags |= NF_NAT_RANGE_PERSISTENT
  76. }
  77. if flags != 0 {
  78. attrs = append(attrs, netlink.Attribute{Type: unix.NFTA_NAT_FLAGS, Data: binaryutil.BigEndian.PutUint32(flags)})
  79. }
  80. data, err := netlink.MarshalAttributes(attrs)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return netlink.MarshalAttributes([]netlink.Attribute{
  85. {Type: unix.NFTA_EXPR_NAME, Data: []byte("nat\x00")},
  86. {Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: data},
  87. })
  88. }
  89. func (e *NAT) unmarshal(fam byte, data []byte) error {
  90. ad, err := netlink.NewAttributeDecoder(data)
  91. if err != nil {
  92. return err
  93. }
  94. ad.ByteOrder = binary.BigEndian
  95. for ad.Next() {
  96. switch ad.Type() {
  97. case unix.NFTA_NAT_TYPE:
  98. e.Type = NATType(ad.Uint32())
  99. case unix.NFTA_NAT_FAMILY:
  100. e.Family = ad.Uint32()
  101. case unix.NFTA_NAT_REG_ADDR_MIN:
  102. e.RegAddrMin = ad.Uint32()
  103. case unix.NFTA_NAT_REG_ADDR_MAX:
  104. e.RegAddrMax = ad.Uint32()
  105. case unix.NFTA_NAT_REG_PROTO_MIN:
  106. e.RegProtoMin = ad.Uint32()
  107. case unix.NFTA_NAT_REG_PROTO_MAX:
  108. e.RegProtoMax = ad.Uint32()
  109. case unix.NFTA_NAT_FLAGS:
  110. flags := ad.Uint32()
  111. e.Persistent = (flags & NF_NAT_RANGE_PERSISTENT) != 0
  112. e.Random = (flags & NF_NAT_RANGE_PROTO_RANDOM) != 0
  113. e.FullyRandom = (flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY) != 0
  114. }
  115. }
  116. return ad.Err()
  117. }