Skip to content

Commit 5b0b9d8

Browse files
oxtoacartbradfitz
authored andcommitted
rule: add Rule.Type to allow adding/listing unreachable (RTN_UNREACHABLE) rules
Updates #710 Co-authored-by: Brad Fitzpatrick <[email protected]> Signed-off-by: Percy Wegmann <[email protected]>
1 parent 65a253d commit 5b0b9d8

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

rule.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Rule struct {
2828
IPProto int
2929
UIDRange *RuleUIDRange
3030
Protocol uint8
31+
Type uint8
3132
}
3233

3334
func (r Rule) String() string {
@@ -41,8 +42,8 @@ func (r Rule) String() string {
4142
to = r.Dst.String()
4243
}
4344

44-
return fmt.Sprintf("ip rule %d: from %s to %s table %d",
45-
r.Priority, from, to, r.Table)
45+
return fmt.Sprintf("ip rule %d: from %s to %s table %d %s",
46+
r.Priority, from, to, r.Table, r.typeString())
4647
}
4748

4849
// NewRule return empty rules.

rule_linux.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ func ruleHandle(rule *Rule, req *nl.NetlinkRequest) error {
4343
msg.Protocol = unix.RTPROT_BOOT
4444
msg.Scope = unix.RT_SCOPE_UNIVERSE
4545
msg.Table = unix.RT_TABLE_UNSPEC
46-
msg.Type = unix.RTN_UNSPEC
47-
if req.NlMsghdr.Flags&unix.NLM_F_CREATE > 0 {
46+
msg.Type = rule.Type // usually 0, same as unix.RTN_UNSPEC
47+
if msg.Type == 0 && req.NlMsghdr.Flags&unix.NLM_F_CREATE > 0 {
4848
msg.Type = unix.RTN_UNICAST
4949
}
5050
if rule.Invert {
@@ -332,3 +332,34 @@ func ptrEqual(a, b *uint32) bool {
332332
}
333333
return *a == *b
334334
}
335+
336+
func (r Rule) typeString() string {
337+
switch r.Type {
338+
case unix.RTN_UNSPEC: // zero
339+
return ""
340+
case unix.RTN_UNICAST:
341+
return ""
342+
case unix.RTN_LOCAL:
343+
return "local"
344+
case unix.RTN_BROADCAST:
345+
return "broadcast"
346+
case unix.RTN_ANYCAST:
347+
return "anycast"
348+
case unix.RTN_MULTICAST:
349+
return "multicast"
350+
case unix.RTN_BLACKHOLE:
351+
return "blackhole"
352+
case unix.RTN_UNREACHABLE:
353+
return "unreachable"
354+
case unix.RTN_PROHIBIT:
355+
return "prohibit"
356+
case unix.RTN_THROW:
357+
return "throw"
358+
case unix.RTN_NAT:
359+
return "nat"
360+
case unix.RTN_XRESOLVE:
361+
return "xresolve"
362+
default:
363+
return fmt.Sprintf("type(0x%x)", r.Type)
364+
}
365+
}

rule_nonlinux.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build !linux
2+
// +build !linux
3+
4+
package netlink
5+
6+
func (r Rule) typeString() string {
7+
return ""
8+
}

rule_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ func TestRuleString(t *testing.T) {
613613
s string
614614
}{
615615
"empty rule": {
616-
s: "ip rule 0: from all to all table 0",
616+
s: "ip rule 0: from all to all table 0 ",
617617
},
618618
"rule with src and dst equivalent to <nil>": {
619619
r: Rule{
@@ -622,7 +622,7 @@ func TestRuleString(t *testing.T) {
622622
Dst: &net.IPNet{IP: net.IPv4(20, 0, 0, 0)},
623623
Table: 99,
624624
},
625-
s: "ip rule 100: from all to all table 99",
625+
s: "ip rule 100: from all to all table 99 ",
626626
},
627627
"rule with src and dst": {
628628
r: Rule{
@@ -631,7 +631,14 @@ func TestRuleString(t *testing.T) {
631631
Dst: &net.IPNet{IP: net.IPv4(20, 0, 0, 0), Mask: net.IPv4Mask(255, 255, 255, 0)},
632632
Table: 99,
633633
},
634-
s: "ip rule 100: from 10.0.0.0/24 to 20.0.0.0/24 table 99",
634+
s: "ip rule 100: from 10.0.0.0/24 to 20.0.0.0/24 table 99 ",
635+
},
636+
"rule with type": {
637+
r: Rule{
638+
Priority: 101,
639+
Type: unix.RTN_UNREACHABLE,
640+
},
641+
s: "ip rule 101: from all to all table 0 unreachable",
635642
},
636643
}
637644

@@ -671,6 +678,7 @@ func ruleEquals(a, b Rule) bool {
671678
a.IifName == b.IifName &&
672679
a.Invert == b.Invert &&
673680
a.Tos == b.Tos &&
681+
a.Type == b.Type &&
674682
a.IPProto == b.IPProto &&
675683
a.Protocol == b.Protocol &&
676684
a.Mark == b.Mark &&

0 commit comments

Comments
 (0)