2015-11-26 10:50:07 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/vishvananda/netlink/nl"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RuleAdd adds a rule to the system.
|
|
|
|
// Equivalent to: ip rule add
|
|
|
|
func RuleAdd(rule *Rule) error {
|
2016-05-09 23:55:00 +00:00
|
|
|
return pkgHandle.RuleAdd(rule)
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RuleAdd adds a rule to the system.
|
|
|
|
// Equivalent to: ip rule add
|
|
|
|
func (h *Handle) RuleAdd(rule *Rule) error {
|
|
|
|
req := h.newNetlinkRequest(syscall.RTM_NEWRULE, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
|
2015-11-26 10:50:07 +00:00
|
|
|
return ruleHandle(rule, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RuleDel deletes a rule from the system.
|
|
|
|
// Equivalent to: ip rule del
|
|
|
|
func RuleDel(rule *Rule) error {
|
2016-05-09 23:55:00 +00:00
|
|
|
return pkgHandle.RuleDel(rule)
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RuleDel deletes a rule from the system.
|
|
|
|
// Equivalent to: ip rule del
|
|
|
|
func (h *Handle) RuleDel(rule *Rule) error {
|
|
|
|
req := h.newNetlinkRequest(syscall.RTM_DELRULE, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
|
2015-11-26 10:50:07 +00:00
|
|
|
return ruleHandle(rule, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ruleHandle(rule *Rule, req *nl.NetlinkRequest) error {
|
|
|
|
msg := nl.NewRtMsg()
|
2015-12-09 13:07:11 +00:00
|
|
|
msg.Family = syscall.AF_INET
|
|
|
|
var dstFamily uint8
|
2015-11-26 10:50:07 +00:00
|
|
|
|
|
|
|
var rtAttrs []*nl.RtAttr
|
|
|
|
if rule.Dst != nil && rule.Dst.IP != nil {
|
|
|
|
dstLen, _ := rule.Dst.Mask.Size()
|
|
|
|
msg.Dst_len = uint8(dstLen)
|
2015-12-09 13:07:11 +00:00
|
|
|
msg.Family = uint8(nl.GetIPFamily(rule.Dst.IP))
|
|
|
|
dstFamily = msg.Family
|
2015-11-26 10:50:07 +00:00
|
|
|
var dstData []byte
|
2015-12-09 13:07:11 +00:00
|
|
|
if msg.Family == syscall.AF_INET {
|
2015-11-26 10:50:07 +00:00
|
|
|
dstData = rule.Dst.IP.To4()
|
|
|
|
} else {
|
|
|
|
dstData = rule.Dst.IP.To16()
|
|
|
|
}
|
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_DST, dstData))
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule.Src != nil && rule.Src.IP != nil {
|
2015-12-09 13:07:11 +00:00
|
|
|
msg.Family = uint8(nl.GetIPFamily(rule.Src.IP))
|
|
|
|
if dstFamily != 0 && dstFamily != msg.Family {
|
2015-11-26 10:50:07 +00:00
|
|
|
return fmt.Errorf("source and destination ip are not the same IP family")
|
|
|
|
}
|
|
|
|
srcLen, _ := rule.Src.Mask.Size()
|
|
|
|
msg.Src_len = uint8(srcLen)
|
|
|
|
var srcData []byte
|
2015-12-09 13:07:11 +00:00
|
|
|
if msg.Family == syscall.AF_INET {
|
2015-11-26 10:50:07 +00:00
|
|
|
srcData = rule.Src.IP.To4()
|
|
|
|
} else {
|
|
|
|
srcData = rule.Src.IP.To16()
|
|
|
|
}
|
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_SRC, srcData))
|
|
|
|
}
|
|
|
|
|
2015-12-09 13:07:11 +00:00
|
|
|
if rule.Table >= 0 {
|
|
|
|
msg.Table = uint8(rule.Table)
|
|
|
|
if rule.Table >= 256 {
|
2015-11-26 10:50:07 +00:00
|
|
|
msg.Table = syscall.RT_TABLE_UNSPEC
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req.AddData(msg)
|
|
|
|
for i := range rtAttrs {
|
|
|
|
req.AddData(rtAttrs[i])
|
|
|
|
}
|
|
|
|
|
2016-11-02 08:09:16 +00:00
|
|
|
native := nl.NativeEndian()
|
2015-11-26 10:50:07 +00:00
|
|
|
|
2015-12-09 13:07:11 +00:00
|
|
|
if rule.Priority >= 0 {
|
2016-11-02 08:09:16 +00:00
|
|
|
b := make([]byte, 4)
|
2015-11-26 10:50:07 +00:00
|
|
|
native.PutUint32(b, uint32(rule.Priority))
|
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_PRIORITY, b))
|
|
|
|
}
|
2015-12-09 13:07:11 +00:00
|
|
|
if rule.Mark >= 0 {
|
2016-11-02 08:09:16 +00:00
|
|
|
b := make([]byte, 4)
|
2015-11-26 10:50:07 +00:00
|
|
|
native.PutUint32(b, uint32(rule.Mark))
|
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_FWMARK, b))
|
|
|
|
}
|
2015-12-09 13:07:11 +00:00
|
|
|
if rule.Mask >= 0 {
|
2016-11-02 08:09:16 +00:00
|
|
|
b := make([]byte, 4)
|
2015-11-26 10:50:07 +00:00
|
|
|
native.PutUint32(b, uint32(rule.Mask))
|
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_FWMASK, b))
|
|
|
|
}
|
2015-12-09 13:07:11 +00:00
|
|
|
if rule.Flow >= 0 {
|
2016-11-02 08:09:16 +00:00
|
|
|
b := make([]byte, 4)
|
2015-11-26 10:50:07 +00:00
|
|
|
native.PutUint32(b, uint32(rule.Flow))
|
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_FLOW, b))
|
|
|
|
}
|
2015-12-09 13:14:24 +00:00
|
|
|
if rule.TunID > 0 {
|
2016-11-02 08:09:16 +00:00
|
|
|
b := make([]byte, 4)
|
2015-12-09 13:14:24 +00:00
|
|
|
native.PutUint32(b, uint32(rule.TunID))
|
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_TUN_ID, b))
|
|
|
|
}
|
2015-12-09 13:07:11 +00:00
|
|
|
if rule.Table >= 256 {
|
2016-11-02 08:09:16 +00:00
|
|
|
b := make([]byte, 4)
|
2015-11-26 10:50:07 +00:00
|
|
|
native.PutUint32(b, uint32(rule.Table))
|
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_TABLE, b))
|
|
|
|
}
|
2015-12-09 13:07:11 +00:00
|
|
|
if msg.Table > 0 {
|
|
|
|
if rule.SuppressPrefixlen >= 0 {
|
2016-11-02 08:09:16 +00:00
|
|
|
b := make([]byte, 4)
|
2015-11-26 10:50:07 +00:00
|
|
|
native.PutUint32(b, uint32(rule.SuppressPrefixlen))
|
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_SUPPRESS_PREFIXLEN, b))
|
|
|
|
}
|
2015-12-09 13:07:11 +00:00
|
|
|
if rule.SuppressIfgroup >= 0 {
|
2016-11-02 08:09:16 +00:00
|
|
|
b := make([]byte, 4)
|
2015-11-26 10:50:07 +00:00
|
|
|
native.PutUint32(b, uint32(rule.SuppressIfgroup))
|
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_SUPPRESS_IFGROUP, b))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if rule.IifName != "" {
|
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_IIFNAME, []byte(rule.IifName)))
|
|
|
|
}
|
|
|
|
if rule.OifName != "" {
|
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_OIFNAME, []byte(rule.OifName)))
|
|
|
|
}
|
2015-12-09 13:07:11 +00:00
|
|
|
if rule.Goto >= 0 {
|
|
|
|
msg.Type = nl.FR_ACT_NOP
|
2016-11-02 08:09:16 +00:00
|
|
|
b := make([]byte, 4)
|
2015-11-26 10:50:07 +00:00
|
|
|
native.PutUint32(b, uint32(rule.Goto))
|
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_GOTO, b))
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RuleList lists rules in the system.
|
|
|
|
// Equivalent to: ip rule list
|
|
|
|
func RuleList(family int) ([]Rule, error) {
|
2016-05-09 23:55:00 +00:00
|
|
|
return pkgHandle.RuleList(family)
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RuleList lists rules in the system.
|
|
|
|
// Equivalent to: ip rule list
|
|
|
|
func (h *Handle) RuleList(family int) ([]Rule, error) {
|
|
|
|
req := h.newNetlinkRequest(syscall.RTM_GETRULE, syscall.NLM_F_DUMP|syscall.NLM_F_REQUEST)
|
2015-11-26 10:50:07 +00:00
|
|
|
msg := nl.NewIfInfomsg(family)
|
|
|
|
req.AddData(msg)
|
|
|
|
|
|
|
|
msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWRULE)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
native := nl.NativeEndian()
|
|
|
|
var res = make([]Rule, 0)
|
|
|
|
for i := range msgs {
|
|
|
|
msg := nl.DeserializeRtMsg(msgs[i])
|
|
|
|
attrs, err := nl.ParseRouteAttr(msgs[i][msg.Len():])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-09 13:07:11 +00:00
|
|
|
rule := NewRule()
|
2015-11-26 10:50:07 +00:00
|
|
|
|
|
|
|
for j := range attrs {
|
|
|
|
switch attrs[j].Attr.Type {
|
|
|
|
case syscall.RTA_TABLE:
|
|
|
|
rule.Table = int(native.Uint32(attrs[j].Value[0:4]))
|
|
|
|
case nl.FRA_SRC:
|
|
|
|
rule.Src = &net.IPNet{
|
|
|
|
IP: attrs[j].Value,
|
|
|
|
Mask: net.CIDRMask(int(msg.Src_len), 8*len(attrs[j].Value)),
|
|
|
|
}
|
|
|
|
case nl.FRA_DST:
|
|
|
|
rule.Dst = &net.IPNet{
|
|
|
|
IP: attrs[j].Value,
|
|
|
|
Mask: net.CIDRMask(int(msg.Dst_len), 8*len(attrs[j].Value)),
|
|
|
|
}
|
|
|
|
case nl.FRA_FWMARK:
|
|
|
|
rule.Mark = int(native.Uint32(attrs[j].Value[0:4]))
|
|
|
|
case nl.FRA_FWMASK:
|
|
|
|
rule.Mask = int(native.Uint32(attrs[j].Value[0:4]))
|
2015-12-09 13:14:24 +00:00
|
|
|
case nl.FRA_TUN_ID:
|
|
|
|
rule.TunID = uint(native.Uint64(attrs[j].Value[0:4]))
|
2015-11-26 10:50:07 +00:00
|
|
|
case nl.FRA_IIFNAME:
|
|
|
|
rule.IifName = string(attrs[j].Value[:len(attrs[j].Value)-1])
|
|
|
|
case nl.FRA_OIFNAME:
|
|
|
|
rule.OifName = string(attrs[j].Value[:len(attrs[j].Value)-1])
|
|
|
|
case nl.FRA_SUPPRESS_PREFIXLEN:
|
|
|
|
i := native.Uint32(attrs[j].Value[0:4])
|
|
|
|
if i != 0xffffffff {
|
|
|
|
rule.SuppressPrefixlen = int(i)
|
|
|
|
}
|
|
|
|
case nl.FRA_SUPPRESS_IFGROUP:
|
|
|
|
i := native.Uint32(attrs[j].Value[0:4])
|
|
|
|
if i != 0xffffffff {
|
|
|
|
rule.SuppressIfgroup = int(i)
|
|
|
|
}
|
|
|
|
case nl.FRA_FLOW:
|
|
|
|
rule.Flow = int(native.Uint32(attrs[j].Value[0:4]))
|
|
|
|
case nl.FRA_GOTO:
|
|
|
|
rule.Goto = int(native.Uint32(attrs[j].Value[0:4]))
|
|
|
|
case nl.FRA_PRIORITY:
|
|
|
|
rule.Priority = int(native.Uint32(attrs[j].Value[0:4]))
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 13:07:11 +00:00
|
|
|
res = append(res, *rule)
|
2015-11-26 10:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|