rule: add support for ipproto

This is similar to https://github.com/vishvananda/netlink/pull/511,
but this time for the ipproto option:
```
ip rule add ipproto xxx table main
```
This commit is contained in:
Zihong Zheng 2021-08-25 21:23:33 -07:00 committed by Vish (Ishaya) Abrams
parent 796d4ea903
commit 5a3e901175
3 changed files with 8 additions and 0 deletions

View File

@ -25,6 +25,7 @@ type Rule struct {
Invert bool
Dport *RulePortRange
Sport *RulePortRange
IPProto int
}
func (r Rule) String() string {

View File

@ -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
}

View File

@ -33,6 +33,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)
}