netconfig: use oifname instead of oif

This commit is contained in:
Michael Stapelberg 2018-06-05 08:48:30 +02:00
parent 21ca224281
commit 320ca04a2f
2 changed files with 12 additions and 11 deletions

View File

@ -176,7 +176,7 @@ func TestNetconfig(t *testing.T) {
for n, rule := range rules {
t.Logf("rule %d: %s", n, rule)
}
if len(rules) < 3 {
if len(rules) < 2 {
t.Fatalf("nftables rules not found")
}
wantRules := []string{
@ -187,7 +187,7 @@ func TestNetconfig(t *testing.T) {
``,
` chain postrouting {`,
` type nat hook postrouting priority 100; policy accept;`,
` oif "uplink0" masquerade`,
` oifname "uplink0" masquerade`,
` }`,
`}`,
}

View File

@ -262,6 +262,12 @@ func applyInterfaces(dir, root string) error {
}
func applyFirewall() error {
func ifname(n string) []byte {
b := make([]byte, 16)
copy(b, []byte(n+"\x00"))
return b
}
c := &nftables.Conn{}
// TODO: currently, each iteration adds a nftables.Rule — clear before?
@ -287,22 +293,17 @@ func applyFirewall() error {
Type: nftables.ChainTypeNAT,
})
iface, err := net.InterfaceByName("uplink0")
if err != nil {
return err
}
c.AddRule(&nftables.Rule{
Table: nat,
Chain: postrouting,
Exprs: []expr.Any{
// meta load oif => reg 1
&expr.Meta{Key: expr.MetaKeyOIF, Register: 1},
// cmp eq reg 1 0x00000003
// meta load oifname => reg 1
&expr.Meta{Key: expr.MetaKeyOIFNAME, Register: 1},
// cmp eq reg 1 0x696c7075 0x00306b6e 0x00000000 0x00000000
&expr.Cmp{
Op: expr.CmpOpEq,
Register: 1,
Data: uint32(iface.Index), // TODO: try using oifname instead of oif
Data: ifname("uplink0"),
},
// masq
&expr.Masq{},