Add expression and tests for numgen (#77)
Signed-off-by: Serguei Bezverkhi <sbezverk@cisco.com>
This commit is contained in:
parent
3ba45f5d78
commit
9dee196925
|
@ -0,0 +1,78 @@
|
|||
// Copyright 2018 Google LLC. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package expr
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/nftables/binaryutil"
|
||||
"github.com/mdlayher/netlink"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// Numgen defines Numgen expression structure
|
||||
type Numgen struct {
|
||||
Register uint32
|
||||
Modulus uint32
|
||||
Type uint32
|
||||
Offset uint32
|
||||
}
|
||||
|
||||
func (e *Numgen) marshal() ([]byte, error) {
|
||||
// Currently only two types are supported, failing if Type is not of two known types
|
||||
switch e.Type {
|
||||
case unix.NFT_NG_INCREMENTAL:
|
||||
case unix.NFT_NG_RANDOM:
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported numgen type %d", e.Type)
|
||||
}
|
||||
|
||||
data, err := netlink.MarshalAttributes([]netlink.Attribute{
|
||||
{Type: unix.NFTA_NG_DREG, Data: binaryutil.BigEndian.PutUint32(e.Register)},
|
||||
{Type: unix.NFTA_NG_MODULUS, Data: binaryutil.BigEndian.PutUint32(e.Modulus)},
|
||||
{Type: unix.NFTA_NG_TYPE, Data: binaryutil.BigEndian.PutUint32(e.Type)},
|
||||
{Type: unix.NFTA_NG_OFFSET, Data: binaryutil.BigEndian.PutUint32(e.Offset)},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return netlink.MarshalAttributes([]netlink.Attribute{
|
||||
{Type: unix.NFTA_EXPR_NAME, Data: []byte("numgen\x00")},
|
||||
{Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: data},
|
||||
})
|
||||
}
|
||||
|
||||
func (e *Numgen) unmarshal(data []byte) error {
|
||||
ad, err := netlink.NewAttributeDecoder(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ad.ByteOrder = binary.BigEndian
|
||||
for ad.Next() {
|
||||
switch ad.Type() {
|
||||
case unix.NFTA_NG_DREG:
|
||||
e.Register = ad.Uint32()
|
||||
case unix.NFTA_NG_MODULUS:
|
||||
e.Modulus = ad.Uint32()
|
||||
case unix.NFTA_NG_TYPE:
|
||||
e.Type = ad.Uint32()
|
||||
case unix.NFTA_NG_OFFSET:
|
||||
e.Offset = ad.Uint32()
|
||||
}
|
||||
}
|
||||
return ad.Err()
|
||||
}
|
104
nftables_test.go
104
nftables_test.go
|
@ -3231,6 +3231,110 @@ func TestFib(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestNumgen(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
chain *nftables.Chain
|
||||
want [][]byte
|
||||
numgenExprs []expr.Any
|
||||
}{
|
||||
{
|
||||
name: "numgen random",
|
||||
chain: &nftables.Chain{
|
||||
Name: "base-chain",
|
||||
},
|
||||
want: [][]byte{
|
||||
// batch begin
|
||||
[]byte("\x00\x00\x00\x0a"),
|
||||
// nft add table ip filter
|
||||
[]byte("\x02\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x08\x00\x02\x00\x00\x00\x00\x00"),
|
||||
// nft add chain ip filter base-chain
|
||||
[]byte("\x02\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x0f\x00\x03\x00\x62\x61\x73\x65\x2d\x63\x68\x61\x69\x6e\x00\x00"),
|
||||
// nft add rule ip filter base-chain numgen random mod 1 offset 0 vmap { 0 : drop }
|
||||
[]byte("\x02\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x0f\x00\x02\x00\x62\x61\x73\x65\x2d\x63\x68\x61\x69\x6e\x00\x00\x38\x00\x04\x80\x34\x00\x01\x80\x0b\x00\x01\x00\x6e\x75\x6d\x67\x65\x6e\x00\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\x01\x08\x00\x04\x00\x00\x00\x00\x00"),
|
||||
// batch end
|
||||
[]byte("\x00\x00\x00\x0a"),
|
||||
},
|
||||
numgenExprs: []expr.Any{
|
||||
&expr.Numgen{
|
||||
Register: 1,
|
||||
Type: unix.NFT_NG_RANDOM,
|
||||
Modulus: 0x1,
|
||||
Offset: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "numgen incremental",
|
||||
chain: &nftables.Chain{
|
||||
Name: "base-chain",
|
||||
},
|
||||
want: [][]byte{
|
||||
// batch begin
|
||||
[]byte("\x00\x00\x00\x0a"),
|
||||
// nft add table ip filter
|
||||
[]byte("\x02\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x08\x00\x02\x00\x00\x00\x00\x00"),
|
||||
// nft add chain ip filter base-chain
|
||||
[]byte("\x02\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x0f\x00\x03\x00\x62\x61\x73\x65\x2d\x63\x68\x61\x69\x6e\x00\x00"),
|
||||
// nft add rule ip filter base-chain numgen inc mod 1 offset 0 vmap { 0 : drop }
|
||||
[]byte("\x02\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x0f\x00\x02\x00\x62\x61\x73\x65\x2d\x63\x68\x61\x69\x6e\x00\x00\x38\x00\x04\x80\x34\x00\x01\x80\x0b\x00\x01\x00\x6e\x75\x6d\x67\x65\x6e\x00\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\x00\x08\x00\x04\x00\x00\x00\x00\x00"),
|
||||
// batch end
|
||||
[]byte("\x00\x00\x00\x0a"),
|
||||
},
|
||||
numgenExprs: []expr.Any{
|
||||
&expr.Numgen{
|
||||
Register: 1,
|
||||
Type: unix.NFT_NG_INCREMENTAL,
|
||||
Modulus: 0x1,
|
||||
Offset: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
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(tt.want[idx]) == 0 {
|
||||
t.Errorf("no want entry for message %d: %x", idx, b)
|
||||
continue
|
||||
}
|
||||
got := b
|
||||
if !bytes.Equal(got, tt.want[idx]) {
|
||||
t.Errorf("message %d: %s", idx, linediff(nfdump(got), nfdump(tt.want[idx])))
|
||||
}
|
||||
}
|
||||
return req, nil
|
||||
},
|
||||
}
|
||||
|
||||
filter := c.AddTable(&nftables.Table{
|
||||
Family: nftables.TableFamilyIPv4,
|
||||
Name: "filter",
|
||||
})
|
||||
|
||||
tt.chain.Table = filter
|
||||
chain := c.AddChain(tt.chain)
|
||||
c.AddRule(&nftables.Rule{
|
||||
Table: filter,
|
||||
Chain: chain,
|
||||
Exprs: tt.numgenExprs,
|
||||
})
|
||||
if err := c.Flush(); err != nil {
|
||||
t.Fatalf("Test \"%s\" failed with error: %+v", tt.name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
Loading…
Reference in New Issue