rule_linux.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package netlink
  2. import (
  3. "fmt"
  4. "net"
  5. "syscall"
  6. "github.com/vishvananda/netlink/nl"
  7. )
  8. // RuleAdd adds a rule to the system.
  9. // Equivalent to: ip rule add
  10. func RuleAdd(rule *Rule) error {
  11. return pkgHandle.RuleAdd(rule)
  12. }
  13. // RuleAdd adds a rule to the system.
  14. // Equivalent to: ip rule add
  15. func (h *Handle) RuleAdd(rule *Rule) error {
  16. req := h.newNetlinkRequest(syscall.RTM_NEWRULE, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
  17. return ruleHandle(rule, req)
  18. }
  19. // RuleDel deletes a rule from the system.
  20. // Equivalent to: ip rule del
  21. func RuleDel(rule *Rule) error {
  22. return pkgHandle.RuleDel(rule)
  23. }
  24. // RuleDel deletes a rule from the system.
  25. // Equivalent to: ip rule del
  26. func (h *Handle) RuleDel(rule *Rule) error {
  27. req := h.newNetlinkRequest(syscall.RTM_DELRULE, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
  28. return ruleHandle(rule, req)
  29. }
  30. func ruleHandle(rule *Rule, req *nl.NetlinkRequest) error {
  31. msg := nl.NewRtMsg()
  32. msg.Family = syscall.AF_INET
  33. var dstFamily uint8
  34. var rtAttrs []*nl.RtAttr
  35. if rule.Dst != nil && rule.Dst.IP != nil {
  36. dstLen, _ := rule.Dst.Mask.Size()
  37. msg.Dst_len = uint8(dstLen)
  38. msg.Family = uint8(nl.GetIPFamily(rule.Dst.IP))
  39. dstFamily = msg.Family
  40. var dstData []byte
  41. if msg.Family == syscall.AF_INET {
  42. dstData = rule.Dst.IP.To4()
  43. } else {
  44. dstData = rule.Dst.IP.To16()
  45. }
  46. rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_DST, dstData))
  47. }
  48. if rule.Src != nil && rule.Src.IP != nil {
  49. msg.Family = uint8(nl.GetIPFamily(rule.Src.IP))
  50. if dstFamily != 0 && dstFamily != msg.Family {
  51. return fmt.Errorf("source and destination ip are not the same IP family")
  52. }
  53. srcLen, _ := rule.Src.Mask.Size()
  54. msg.Src_len = uint8(srcLen)
  55. var srcData []byte
  56. if msg.Family == syscall.AF_INET {
  57. srcData = rule.Src.IP.To4()
  58. } else {
  59. srcData = rule.Src.IP.To16()
  60. }
  61. rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_SRC, srcData))
  62. }
  63. if rule.Table >= 0 {
  64. msg.Table = uint8(rule.Table)
  65. if rule.Table >= 256 {
  66. msg.Table = syscall.RT_TABLE_UNSPEC
  67. }
  68. }
  69. req.AddData(msg)
  70. for i := range rtAttrs {
  71. req.AddData(rtAttrs[i])
  72. }
  73. native := nl.NativeEndian()
  74. if rule.Priority >= 0 {
  75. b := make([]byte, 4)
  76. native.PutUint32(b, uint32(rule.Priority))
  77. req.AddData(nl.NewRtAttr(nl.FRA_PRIORITY, b))
  78. }
  79. if rule.Mark >= 0 {
  80. b := make([]byte, 4)
  81. native.PutUint32(b, uint32(rule.Mark))
  82. req.AddData(nl.NewRtAttr(nl.FRA_FWMARK, b))
  83. }
  84. if rule.Mask >= 0 {
  85. b := make([]byte, 4)
  86. native.PutUint32(b, uint32(rule.Mask))
  87. req.AddData(nl.NewRtAttr(nl.FRA_FWMASK, b))
  88. }
  89. if rule.Flow >= 0 {
  90. b := make([]byte, 4)
  91. native.PutUint32(b, uint32(rule.Flow))
  92. req.AddData(nl.NewRtAttr(nl.FRA_FLOW, b))
  93. }
  94. if rule.TunID > 0 {
  95. b := make([]byte, 4)
  96. native.PutUint32(b, uint32(rule.TunID))
  97. req.AddData(nl.NewRtAttr(nl.FRA_TUN_ID, b))
  98. }
  99. if rule.Table >= 256 {
  100. b := make([]byte, 4)
  101. native.PutUint32(b, uint32(rule.Table))
  102. req.AddData(nl.NewRtAttr(nl.FRA_TABLE, b))
  103. }
  104. if msg.Table > 0 {
  105. if rule.SuppressPrefixlen >= 0 {
  106. b := make([]byte, 4)
  107. native.PutUint32(b, uint32(rule.SuppressPrefixlen))
  108. req.AddData(nl.NewRtAttr(nl.FRA_SUPPRESS_PREFIXLEN, b))
  109. }
  110. if rule.SuppressIfgroup >= 0 {
  111. b := make([]byte, 4)
  112. native.PutUint32(b, uint32(rule.SuppressIfgroup))
  113. req.AddData(nl.NewRtAttr(nl.FRA_SUPPRESS_IFGROUP, b))
  114. }
  115. }
  116. if rule.IifName != "" {
  117. req.AddData(nl.NewRtAttr(nl.FRA_IIFNAME, []byte(rule.IifName)))
  118. }
  119. if rule.OifName != "" {
  120. req.AddData(nl.NewRtAttr(nl.FRA_OIFNAME, []byte(rule.OifName)))
  121. }
  122. if rule.Goto >= 0 {
  123. msg.Type = nl.FR_ACT_NOP
  124. b := make([]byte, 4)
  125. native.PutUint32(b, uint32(rule.Goto))
  126. req.AddData(nl.NewRtAttr(nl.FRA_GOTO, b))
  127. }
  128. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  129. return err
  130. }
  131. // RuleList lists rules in the system.
  132. // Equivalent to: ip rule list
  133. func RuleList(family int) ([]Rule, error) {
  134. return pkgHandle.RuleList(family)
  135. }
  136. // RuleList lists rules in the system.
  137. // Equivalent to: ip rule list
  138. func (h *Handle) RuleList(family int) ([]Rule, error) {
  139. req := h.newNetlinkRequest(syscall.RTM_GETRULE, syscall.NLM_F_DUMP|syscall.NLM_F_REQUEST)
  140. msg := nl.NewIfInfomsg(family)
  141. req.AddData(msg)
  142. msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWRULE)
  143. if err != nil {
  144. return nil, err
  145. }
  146. native := nl.NativeEndian()
  147. var res = make([]Rule, 0)
  148. for i := range msgs {
  149. msg := nl.DeserializeRtMsg(msgs[i])
  150. attrs, err := nl.ParseRouteAttr(msgs[i][msg.Len():])
  151. if err != nil {
  152. return nil, err
  153. }
  154. rule := NewRule()
  155. for j := range attrs {
  156. switch attrs[j].Attr.Type {
  157. case syscall.RTA_TABLE:
  158. rule.Table = int(native.Uint32(attrs[j].Value[0:4]))
  159. case nl.FRA_SRC:
  160. rule.Src = &net.IPNet{
  161. IP: attrs[j].Value,
  162. Mask: net.CIDRMask(int(msg.Src_len), 8*len(attrs[j].Value)),
  163. }
  164. case nl.FRA_DST:
  165. rule.Dst = &net.IPNet{
  166. IP: attrs[j].Value,
  167. Mask: net.CIDRMask(int(msg.Dst_len), 8*len(attrs[j].Value)),
  168. }
  169. case nl.FRA_FWMARK:
  170. rule.Mark = int(native.Uint32(attrs[j].Value[0:4]))
  171. case nl.FRA_FWMASK:
  172. rule.Mask = int(native.Uint32(attrs[j].Value[0:4]))
  173. case nl.FRA_TUN_ID:
  174. rule.TunID = uint(native.Uint64(attrs[j].Value[0:4]))
  175. case nl.FRA_IIFNAME:
  176. rule.IifName = string(attrs[j].Value[:len(attrs[j].Value)-1])
  177. case nl.FRA_OIFNAME:
  178. rule.OifName = string(attrs[j].Value[:len(attrs[j].Value)-1])
  179. case nl.FRA_SUPPRESS_PREFIXLEN:
  180. i := native.Uint32(attrs[j].Value[0:4])
  181. if i != 0xffffffff {
  182. rule.SuppressPrefixlen = int(i)
  183. }
  184. case nl.FRA_SUPPRESS_IFGROUP:
  185. i := native.Uint32(attrs[j].Value[0:4])
  186. if i != 0xffffffff {
  187. rule.SuppressIfgroup = int(i)
  188. }
  189. case nl.FRA_FLOW:
  190. rule.Flow = int(native.Uint32(attrs[j].Value[0:4]))
  191. case nl.FRA_GOTO:
  192. rule.Goto = int(native.Uint32(attrs[j].Value[0:4]))
  193. case nl.FRA_PRIORITY:
  194. rule.Priority = int(native.Uint32(attrs[j].Value[0:4]))
  195. }
  196. }
  197. res = append(res, *rule)
  198. }
  199. return res, nil
  200. }