github.com/sagernet/netlink@v0.0.0-20240612041022-b9a21c07ac6a/rule.go (about) 1 package netlink 2 3 import ( 4 "fmt" 5 "net/netip" 6 ) 7 8 // Rule represents a netlink rule. 9 type Rule struct { 10 Priority int 11 Family int 12 Table int 13 Type uint8 14 Mark uint32 15 MarkSet bool 16 Mask int 17 Tos uint 18 TunID uint 19 Goto int 20 Src netip.Prefix 21 Dst netip.Prefix 22 Flow int 23 IifName string 24 OifName string 25 SuppressIfgroup int 26 SuppressPrefixlen int 27 Invert bool 28 Dport *RulePortRange 29 Sport *RulePortRange 30 IPProto int 31 UIDRange *RuleUIDRange 32 } 33 34 func (r Rule) String() string { 35 from := "all" 36 if r.Src.IsValid() && r.Src.String() != "<nil>" { 37 from = r.Src.String() 38 } 39 40 to := "all" 41 if r.Dst.IsValid() && r.Dst.String() != "<nil>" { 42 to = r.Dst.String() 43 } 44 45 return fmt.Sprintf("ip rule %d: from %s to %s table %d", 46 r.Priority, from, to, r.Table) 47 } 48 49 // NewRule return empty rules. 50 func NewRule() *Rule { 51 return &Rule{ 52 Table: -1, 53 SuppressIfgroup: -1, 54 SuppressPrefixlen: -1, 55 Priority: -1, 56 Mask: -1, 57 Goto: -1, 58 Flow: -1, 59 } 60 } 61 62 // NewRulePortRange creates rule sport/dport range. 63 func NewRulePortRange(start, end uint16) *RulePortRange { 64 return &RulePortRange{Start: start, End: end} 65 } 66 67 // RulePortRange represents rule sport/dport range. 68 type RulePortRange struct { 69 Start uint16 70 End uint16 71 } 72 73 // NewRuleUIDRange creates rule uid range. 74 func NewRuleUIDRange(start, end uint32) *RuleUIDRange { 75 return &RuleUIDRange{Start: start, End: end} 76 } 77 78 // RuleUIDRange represents rule uid range. 79 type RuleUIDRange struct { 80 Start uint32 81 End uint32 82 }