nftables_test: add ConfigureNATSourceAddress for illustration
related to #5
This commit is contained in:
parent
9ac63cb282
commit
fedef31bd8
|
@ -294,6 +294,94 @@ func TestConfigureNAT(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfigureNATSourceAddress(t *testing.T) {
|
||||||
|
// The want byte sequences come from stracing nft(8), e.g.:
|
||||||
|
// strace -f -v -x -s 2048 -eraw=sendto nft add table ip nat
|
||||||
|
//
|
||||||
|
// The nft(8) command sequence was taken from:
|
||||||
|
// https://wiki.nftables.org/wiki-nftables/index.php/Performing_Network_Address_Translation_(NAT)
|
||||||
|
want := [][]byte{
|
||||||
|
// batch begin
|
||||||
|
[]byte("\x00\x00\x00\x0a"),
|
||||||
|
// nft flush ruleset
|
||||||
|
[]byte("\x00\x00\x00\x00"),
|
||||||
|
// nft add table ip nat
|
||||||
|
[]byte("\x02\x00\x00\x00\x08\x00\x01\x00\x6e\x61\x74\x00\x08\x00\x02\x00\x00\x00\x00\x00"),
|
||||||
|
// nft add chain nat postrouting '{' type nat hook postrouting priority 100 \; '}'
|
||||||
|
[]byte("\x02\x00\x00\x00\x08\x00\x01\x00\x6e\x61\x74\x00\x10\x00\x03\x00\x70\x6f\x73\x74\x72\x6f\x75\x74\x69\x6e\x67\x00\x14\x00\x04\x80\x08\x00\x01\x00\x00\x00\x00\x04\x08\x00\x02\x00\x00\x00\x00\x64\x08\x00\x07\x00\x6e\x61\x74\x00"),
|
||||||
|
// nft add rule nat postrouting ip saddr 192.168.69.2 masquerade
|
||||||
|
[]byte("\x02\x00\x00\x00\x08\x00\x01\x00\x6e\x61\x74\x00\x10\x00\x02\x00\x70\x6f\x73\x74\x72\x6f\x75\x74\x69\x6e\x67\x00\x78\x00\x04\x80\x34\x00\x01\x80\x0c\x00\x01\x00\x70\x61\x79\x6c\x6f\x61\x64\x00\x24\x00\x02\x80\x08\x00\x01\x00\x00\x00\x00\x01\x08\x00\x02\x00\x00\x00\x00\x01\x08\x00\x03\x00\x00\x00\x00\x0c\x08\x00\x04\x00\x00\x00\x00\x04\x2c\x00\x01\x80\x08\x00\x01\x00\x63\x6d\x70\x00\x20\x00\x02\x80\x08\x00\x01\x00\x00\x00\x00\x01\x08\x00\x02\x00\x00\x00\x00\x00\x0c\x00\x03\x80\x08\x00\x01\x00\xc0\xa8\x45\x02\x14\x00\x01\x80\x09\x00\x01\x00\x6d\x61\x73\x71\x00\x00\x00\x00\x04\x00\x02\x80"),
|
||||||
|
// batch end
|
||||||
|
[]byte("\x00\x00\x00\x0a"),
|
||||||
|
}
|
||||||
|
|
||||||
|
c := &nftables.Conn{
|
||||||
|
TestDial: func(req []netlink.Message) ([]netlink.Message, error) {
|
||||||
|
for idx, msg := range req {
|
||||||
|
b, err := msg.MarshalBinary()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(b) < 16 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
b = b[16:]
|
||||||
|
if len(want) == 0 {
|
||||||
|
t.Errorf("no want entry for message %d: %x", idx, b)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if got, want := b, want[0]; !bytes.Equal(got, want) {
|
||||||
|
t.Errorf("message %d: %s", idx, linediff(nfdump(got), nfdump(want)))
|
||||||
|
}
|
||||||
|
want = want[1:]
|
||||||
|
}
|
||||||
|
return req, nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
c.FlushRuleset()
|
||||||
|
|
||||||
|
nat := c.AddTable(&nftables.Table{
|
||||||
|
Family: nftables.TableFamilyIPv4,
|
||||||
|
Name: "nat",
|
||||||
|
})
|
||||||
|
|
||||||
|
postrouting := c.AddChain(&nftables.Chain{
|
||||||
|
Name: "postrouting",
|
||||||
|
Hooknum: nftables.ChainHookPostrouting,
|
||||||
|
Priority: nftables.ChainPriorityNATSource,
|
||||||
|
Table: nat,
|
||||||
|
Type: nftables.ChainTypeNAT,
|
||||||
|
})
|
||||||
|
|
||||||
|
c.AddRule(&nftables.Rule{
|
||||||
|
Table: nat,
|
||||||
|
Chain: postrouting,
|
||||||
|
Exprs: []expr.Any{
|
||||||
|
// payload load 4b @ network header + 12 => reg 1
|
||||||
|
&expr.Payload{
|
||||||
|
DestRegister: 1,
|
||||||
|
Base: expr.PayloadBaseNetworkHeader,
|
||||||
|
Offset: 12,
|
||||||
|
Len: 4,
|
||||||
|
},
|
||||||
|
// cmp eq reg 1 0x0245a8c0
|
||||||
|
&expr.Cmp{
|
||||||
|
Op: expr.CmpOpEq,
|
||||||
|
Register: 1,
|
||||||
|
Data: net.ParseIP("192.168.69.2").To4(),
|
||||||
|
},
|
||||||
|
|
||||||
|
// masq
|
||||||
|
&expr.Masq{},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := c.Flush(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetRule(t *testing.T) {
|
func TestGetRule(t *testing.T) {
|
||||||
// The want byte sequences come from stracing nft(8), e.g.:
|
// The want byte sequences come from stracing nft(8), e.g.:
|
||||||
// strace -f -v -x -s 2048 -eraw=sendto nft list chain ip filter forward
|
// strace -f -v -x -s 2048 -eraw=sendto nft list chain ip filter forward
|
||||||
|
|
Loading…
Reference in New Issue