Skip to content

Commit 6a1556a

Browse files
committed
Fix reading of rule type and add l3mdev flag.
When reading rules, makes sure to populate the type field of Rule. Also adds a l3mdev flag to Rule, reads it when listing rules, sets it when updating rules, and makes sure it is taken into account when comparing rules using Equal as introduced in vishvananda#1095.
1 parent 6a8c071 commit 6a1556a

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

rule.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package netlink
33
import (
44
"fmt"
55
"net"
6+
7+
"golang.org/x/sys/unix"
68
)
79

810
// Rule represents a netlink rule.
@@ -29,6 +31,7 @@ type Rule struct {
2931
UIDRange *RuleUIDRange
3032
Protocol uint8
3133
Type uint8
34+
L3mdev uint8
3235
}
3336

3437
func (r Rule) Equal(x Rule) bool {
@@ -43,7 +46,8 @@ func (r Rule) Equal(x Rule) bool {
4346
r.IifName == x.IifName &&
4447
r.Invert == x.Invert &&
4548
r.Tos == x.Tos &&
46-
r.Type == x.Type &&
49+
(r.Type == x.Type ||
50+
(r.Type == 0 && x.Type == unix.RTN_UNICAST || r.Type == unix.RTN_UNICAST && x.Type == 0)) &&
4751
r.IPProto == x.IPProto &&
4852
r.Protocol == x.Protocol &&
4953
r.Mark == x.Mark &&
@@ -59,7 +63,8 @@ func (r Rule) Equal(x Rule) bool {
5963
r.SuppressPrefixlen == x.SuppressPrefixlen &&
6064
(r.Dport == x.Dport || (r.Dport != nil && x.Dport != nil && r.Dport.Equal(*x.Dport))) &&
6165
(r.Sport == x.Sport || (r.Sport != nil && x.Sport != nil && r.Sport.Equal(*x.Sport))) &&
62-
(r.UIDRange == x.UIDRange || (r.UIDRange != nil && x.UIDRange != nil && r.UIDRange.Equal(*x.UIDRange)))
66+
(r.UIDRange == x.UIDRange || (r.UIDRange != nil && x.UIDRange != nil && r.UIDRange.Equal(*x.UIDRange))) &&
67+
r.L3mdev == x.L3mdev
6368
}
6469

6570
func ptrEqual(a, b *uint32) bool {

rule_linux.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ func ruleHandle(rule *Rule, req *nl.NetlinkRequest) error {
178178
req.AddData(nl.NewRtAttr(nl.FRA_PROTOCOL, nl.Uint8Attr(rule.Protocol)))
179179
}
180180

181+
if rule.L3mdev > 0 {
182+
req.AddData(nl.NewRtAttr(nl.FRA_L3MDEV, nl.Uint8Attr(rule.L3mdev)))
183+
}
184+
181185
_, err := req.Execute(unix.NETLINK_ROUTE, 0)
182186
return err
183187
}
@@ -239,6 +243,7 @@ func (h *Handle) RuleListFiltered(family int, filter *Rule, filterMask uint64) (
239243
rule.Invert = msg.Flags&FibRuleInvert > 0
240244
rule.Family = int(msg.Family)
241245
rule.Tos = uint(msg.Tos)
246+
rule.Type = msg.Type
242247

243248
for j := range attrs {
244249
switch attrs[j].Attr.Type {
@@ -291,6 +296,8 @@ func (h *Handle) RuleListFiltered(family int, filter *Rule, filterMask uint64) (
291296
rule.UIDRange = NewRuleUIDRange(native.Uint32(attrs[j].Value[0:4]), native.Uint32(attrs[j].Value[4:8]))
292297
case nl.FRA_PROTOCOL:
293298
rule.Protocol = uint8(attrs[j].Value[0])
299+
case nl.FRA_L3MDEV:
300+
rule.L3mdev = uint8(attrs[j].Value[0])
294301
}
295302
}
296303

rule_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestRuleAddDel(t *testing.T) {
5555
// find this rule
5656
found := ruleExists(rules, *rule)
5757
if !found {
58-
t.Fatal("Rule has diffrent options than one added")
58+
t.Fatal("Rule has different options than one added")
5959
}
6060

6161
if err := RuleDel(rule); err != nil {
@@ -699,6 +699,7 @@ func TestRuleEqual(t *testing.T) {
699699
{UIDRange: &RuleUIDRange{Start: 3, End: 5}},
700700
{Protocol: FAMILY_V6},
701701
{Type: unix.RTN_UNREACHABLE},
702+
{L3mdev: 1},
702703
}
703704
for i1 := range cases {
704705
for i2 := range cases {

0 commit comments

Comments
 (0)