Adding to nat expression additional parameters (#59)
* Change in dnat logic to cover all combinations Signed-off-by: Serguei Bezverkhi <sbezverk@cisco.com> * Add parameter to cover all possible nat combinations Signed-off-by: Serguei Bezverkhi <sbezverk@cisco.com>
This commit is contained in:
parent
a0a4b3333d
commit
5d14089d2e
63
expr/nat.go
63
expr/nat.go
|
@ -15,7 +15,7 @@
|
|||
package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"encoding/binary"
|
||||
|
||||
"github.com/google/nftables/binaryutil"
|
||||
"github.com/mdlayher/netlink"
|
||||
|
@ -34,8 +34,12 @@ type NAT struct {
|
|||
Type NATType
|
||||
Family uint32 // TODO: typed const
|
||||
RegAddrMin uint32
|
||||
RegAddrMax uint32
|
||||
RegProtoMin uint32
|
||||
RegProtoMax uint32
|
||||
Random bool
|
||||
FullyRandom bool
|
||||
Persistent bool
|
||||
}
|
||||
|
||||
// |00048|N-|00001| |len |flags| type|
|
||||
|
@ -55,12 +59,33 @@ func (e *NAT) marshal() ([]byte, error) {
|
|||
attrs := []netlink.Attribute{
|
||||
{Type: unix.NFTA_NAT_TYPE, Data: binaryutil.BigEndian.PutUint32(uint32(e.Type))},
|
||||
{Type: unix.NFTA_NAT_FAMILY, Data: binaryutil.BigEndian.PutUint32(e.Family)},
|
||||
{Type: unix.NFTA_NAT_REG_ADDR_MIN, Data: binaryutil.BigEndian.PutUint32(e.RegAddrMin)},
|
||||
{Type: unix.NFTA_NAT_REG_PROTO_MIN, Data: binaryutil.BigEndian.PutUint32(e.RegProtoMin)},
|
||||
}
|
||||
if e.RegProtoMax > 0 {
|
||||
attrs = append(attrs, netlink.Attribute{Type: unix.NFTA_NAT_REG_PROTO_MAX, Data: binaryutil.BigEndian.PutUint32(e.RegProtoMax)})
|
||||
if e.RegAddrMin != 0 {
|
||||
attrs = append(attrs, netlink.Attribute{Type: unix.NFTA_NAT_REG_ADDR_MIN, Data: binaryutil.BigEndian.PutUint32(e.RegAddrMin)})
|
||||
if e.RegAddrMax != 0 {
|
||||
attrs = append(attrs, netlink.Attribute{Type: unix.NFTA_NAT_REG_ADDR_MAX, Data: binaryutil.BigEndian.PutUint32(e.RegAddrMax)})
|
||||
}
|
||||
}
|
||||
if e.RegProtoMin != 0 {
|
||||
attrs = append(attrs, netlink.Attribute{Type: unix.NFTA_NAT_REG_PROTO_MIN, Data: binaryutil.BigEndian.PutUint32(e.RegProtoMin)})
|
||||
if e.RegProtoMax != 0 {
|
||||
attrs = append(attrs, netlink.Attribute{Type: unix.NFTA_NAT_REG_PROTO_MAX, Data: binaryutil.BigEndian.PutUint32(e.RegProtoMax)})
|
||||
}
|
||||
}
|
||||
flags := uint32(0)
|
||||
if e.Random {
|
||||
flags |= NF_NAT_RANGE_PROTO_RANDOM
|
||||
}
|
||||
if e.FullyRandom {
|
||||
flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY
|
||||
}
|
||||
if e.Persistent {
|
||||
flags |= NF_NAT_RANGE_PERSISTENT
|
||||
}
|
||||
if flags != 0 {
|
||||
attrs = append(attrs, netlink.Attribute{Type: unix.NFTA_NAT_FLAGS, Data: binaryutil.BigEndian.PutUint32(flags)})
|
||||
}
|
||||
|
||||
data, err := netlink.MarshalAttributes(attrs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -72,5 +97,31 @@ func (e *NAT) marshal() ([]byte, error) {
|
|||
}
|
||||
|
||||
func (e *NAT) unmarshal(data []byte) error {
|
||||
return fmt.Errorf("not yet implemented")
|
||||
ad, err := netlink.NewAttributeDecoder(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ad.ByteOrder = binary.BigEndian
|
||||
for ad.Next() {
|
||||
switch ad.Type() {
|
||||
case unix.NFTA_NAT_TYPE:
|
||||
e.Type = NATType(ad.Uint32())
|
||||
case unix.NFTA_NAT_FAMILY:
|
||||
e.Family = ad.Uint32()
|
||||
case unix.NFTA_NAT_REG_ADDR_MIN:
|
||||
e.RegAddrMin = ad.Uint32()
|
||||
case unix.NFTA_NAT_REG_ADDR_MAX:
|
||||
e.RegAddrMax = ad.Uint32()
|
||||
case unix.NFTA_NAT_REG_PROTO_MIN:
|
||||
e.RegProtoMin = ad.Uint32()
|
||||
case unix.NFTA_NAT_REG_PROTO_MAX:
|
||||
e.RegProtoMax = ad.Uint32()
|
||||
case unix.NFTA_NAT_FLAGS:
|
||||
flags := ad.Uint32()
|
||||
e.Persistent = (flags & NF_NAT_RANGE_PERSISTENT) != 0
|
||||
e.Random = (flags & NF_NAT_RANGE_PROTO_RANDOM) != 0
|
||||
e.FullyRandom = (flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY) != 0
|
||||
}
|
||||
}
|
||||
return ad.Err()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue