Skip to content

Commit

Permalink
rule: add support for ipproto
Browse files Browse the repository at this point in the history
This is similar to vishvananda#511,
but this time for the ipproto option:
```
ip rule add ipproto xxx table main
```
  • Loading branch information
MrHohn committed Aug 26, 2021
1 parent e1a867c commit 544c894
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Rule struct {
Invert bool
Dport *RulePortRange
Sport *RulePortRange
IPProto int
}

func (r Rule) String() string {
Expand Down
6 changes: 6 additions & 0 deletions rule_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ func ruleHandle(rule *Rule, req *nl.NetlinkRequest) error {
req.AddData(nl.NewRtAttr(nl.FRA_SPORT_RANGE, b))
}

if rule.IPProto > 0 {
b := make([]byte, 4)
native.PutUint32(b, uint32(rule.IPProto))
req.AddData(nl.NewRtAttr(nl.FRA_IP_PROTO, b))
}

_, err := req.Execute(unix.NETLINK_ROUTE, 0)
return err
}
Expand Down
1 change: 1 addition & 0 deletions rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestRuleAddDel(t *testing.T) {
rule.Tos = 0x10
rule.Dport = NewRulePortRange(80, 80)
rule.Sport = NewRulePortRange(1000, 1024)
rule.IPProto = unix.IPPROTO_UDP
if err := RuleAdd(rule); err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 544c894

Please sign in to comment.