Skip to content

Commit

Permalink
objFromMsg: correctly set Table.Family
Browse files Browse the repository at this point in the history
This fixes an issue in router7 where firewall rules would fail to be installed
on all but the first tries (the first try had no counter values, so the
passed-in Table would be returned as-is, with correct family).
  • Loading branch information
stapelberg committed Oct 3, 2018
1 parent 409eade commit 695079e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion nftables.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func objFromMsg(msg netlink.Message) (Obj, error) {
for ad.Next() {
switch ad.Type() {
case unix.NFTA_OBJ_TABLE:
table = &Table{Name: ad.String()}
table = &Table{Name: ad.String(), Family: TableFamily(msg.Data[0])}
case unix.NFTA_OBJ_NAME:
name = ad.String()
case unix.NFTA_OBJ_TYPE:
Expand Down
11 changes: 9 additions & 2 deletions nftables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestConfigureNAT(t *testing.T) {
[]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 prerouting '{' type nat hook prerouting priority 0 \; '}'
[]byte("\x02\x00\x00\x00\x08\x00\x01\x00\x6e\x61\x74\x00\x0f\x00\x03\x00\x70\x72\x65\x72\x6f\x75\x74\x69\x6e\x67\x00\x00\x14\x00\x04\x80\x08\x00\x01\x00\x00\x00\x00\x00\x08\x00\x02\x00\x00\x00\x00\x00\x08\x00\x07\x00\x6e\x61\x74\x00"),
// nft add chain nat postrouting { type nat hook postrouting priority 100 \; }
// 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 oifname uplink0 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\x74\x00\x04\x80\x24\x00\x01\x80\x09\x00\x01\x00\x6d\x65\x74\x61\x00\x00\x00\x00\x14\x00\x02\x80\x08\x00\x02\x00\x00\x00\x00\x07\x08\x00\x01\x00\x00\x00\x00\x01\x38\x00\x01\x80\x08\x00\x01\x00\x63\x6d\x70\x00\x2c\x00\x02\x80\x08\x00\x01\x00\x00\x00\x00\x01\x08\x00\x02\x00\x00\x00\x00\x00\x18\x00\x03\x80\x14\x00\x01\x00\x75\x70\x6c\x69\x6e\x6b\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14\x00\x01\x80\x09\x00\x01\x00\x6d\x61\x73\x71\x00\x00\x00\x00\x04\x00\x02\x80"),
Expand Down Expand Up @@ -436,8 +436,9 @@ func TestGetObjReset(t *testing.T) {
},
}

filter := &nftables.Table{Name: "filter", Family: nftables.TableFamilyIPv4}
objs, err := c.GetObjReset(&nftables.CounterObj{
Table: &nftables.Table{Name: "filter", Family: nftables.TableFamilyIPv4},
Table: filter,
Name: "fwded",
})

Expand All @@ -454,6 +455,12 @@ func TestGetObjReset(t *testing.T) {
if !ok {
t.Fatalf("unexpected type: got %T, want *nftables.CounterObj", obj)
}
if got, want := co.Table.Name, filter.Name; got != want {
t.Errorf("unexpected table name: got %q, want %q", got, want)
}
if got, want := co.Table.Family, filter.Family; got != want {
t.Errorf("unexpected table family: got %d, want %d", got, want)
}
if got, want := co.Packets, uint64(9); got != want {
t.Errorf("unexpected number of packets: got %d, want %d", got, want)
}
Expand Down

0 comments on commit 695079e

Please sign in to comment.