2015-11-26 10:50:07 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
2020-01-16 16:27:48 +00:00
|
|
|
"bytes"
|
Add ErrDumpInterrupted
Add a specific error to report that a netlink response had
NLM_F_DUMP_INTR set, indicating that the set of results may be
incomplete or inconsistent.
unix.EINTR was previously returned (with no results) when the
NLM_F_DUMP_INTR flag was set. Now, errors.Is(err, unix.EINTR) will
still work. But, this will be a breaking change for any code that's
checking for equality with unix.EINTR.
Return results with ErrDumpInterrupted. Results may be incomplete
or inconsistent, but give the caller the option of using them.
Look for NLM_F_DUMP_INTR in more places:
- linkSubscribeAt, neighSubscribeAt, routeSubscribeAt
- can do an initial dump, which may report inconsistent results
-> if there's an error callback, call it with ErrDumpInterrupted
- socketDiagXDPExecutor
- makes an NLM_F_DUMP request, without using Execute()
-> give it the same behaviour as functions that do use Execute()
Signed-off-by: Rob Murray <rob.murray@docker.com>
2024-09-05 08:25:13 +00:00
|
|
|
"errors"
|
2015-11-26 10:50:07 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/vishvananda/netlink/nl"
|
2017-10-20 20:38:07 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2015-11-26 10:50:07 +00:00
|
|
|
)
|
|
|
|
|
2017-11-07 17:34:56 +00:00
|
|
|
const FibRuleInvert = 0x2
|
|
|
|
|
2015-11-26 10:50:07 +00:00
|
|
|
// 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 {
|
2017-10-20 20:38:07 +00:00
|
|
|
req := h.newNetlinkRequest(unix.RTM_NEWRULE, unix.NLM_F_CREATE|unix.NLM_F_EXCL|unix.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 {
|
2017-11-07 17:34:56 +00:00
|
|
|
req := h.newNetlinkRequest(unix.RTM_DELRULE, unix.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()
|
2017-10-20 20:38:07 +00:00
|
|
|
msg.Family = unix.AF_INET
|
2017-11-07 17:34:56 +00:00
|
|
|
msg.Protocol = unix.RTPROT_BOOT
|
|
|
|
msg.Scope = unix.RT_SCOPE_UNIVERSE
|
|
|
|
msg.Table = unix.RT_TABLE_UNSPEC
|
2024-08-22 19:13:23 +00:00
|
|
|
msg.Type = rule.Type // usually 0, same as unix.RTN_UNSPEC
|
|
|
|
if msg.Type == 0 && req.NlMsghdr.Flags&unix.NLM_F_CREATE > 0 {
|
2017-11-07 17:34:56 +00:00
|
|
|
msg.Type = unix.RTN_UNICAST
|
|
|
|
}
|
|
|
|
if rule.Invert {
|
|
|
|
msg.Flags |= FibRuleInvert
|
|
|
|
}
|
2017-08-16 06:28:20 +00:00
|
|
|
if rule.Family != 0 {
|
|
|
|
msg.Family = uint8(rule.Family)
|
|
|
|
}
|
2017-11-07 17:34:56 +00:00
|
|
|
if rule.Table >= 0 && rule.Table < 256 {
|
|
|
|
msg.Table = uint8(rule.Table)
|
|
|
|
}
|
2020-02-08 20:55:55 +00:00
|
|
|
if rule.Tos != 0 {
|
|
|
|
msg.Tos = uint8(rule.Tos)
|
|
|
|
}
|
2015-11-26 10:50:07 +00:00
|
|
|
|
2017-11-07 17:34:56 +00:00
|
|
|
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
|
2017-10-20 20:38:07 +00:00
|
|
|
if msg.Family == unix.AF_INET {
|
2015-11-26 10:50:07 +00:00
|
|
|
dstData = rule.Dst.IP.To4()
|
|
|
|
} else {
|
|
|
|
dstData = rule.Dst.IP.To16()
|
|
|
|
}
|
2017-10-20 20:38:07 +00:00
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_DST, dstData))
|
2015-11-26 10:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-10-20 20:38:07 +00:00
|
|
|
if msg.Family == unix.AF_INET {
|
2015-11-26 10:50:07 +00:00
|
|
|
srcData = rule.Src.IP.To4()
|
|
|
|
} else {
|
|
|
|
srcData = rule.Src.IP.To16()
|
|
|
|
}
|
2017-10-20 20:38:07 +00:00
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_SRC, srcData))
|
2015-11-26 10:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req.AddData(msg)
|
|
|
|
for i := range rtAttrs {
|
|
|
|
req.AddData(rtAttrs[i])
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
2024-07-29 09:12:06 +00:00
|
|
|
if rule.Mark != 0 || rule.Mask != nil {
|
2016-11-02 08:09:16 +00:00
|
|
|
b := make([]byte, 4)
|
2024-07-29 09:12:06 +00:00
|
|
|
native.PutUint32(b, rule.Mark)
|
2015-11-26 10:50:07 +00:00
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_FWMARK, b))
|
|
|
|
}
|
2024-07-29 09:12:06 +00:00
|
|
|
if rule.Mask != nil {
|
2016-11-02 08:09:16 +00:00
|
|
|
b := make([]byte, 4)
|
2024-07-29 09:12:06 +00:00
|
|
|
native.PutUint32(b, *rule.Mask)
|
2015-11-26 10:50:07 +00:00
|
|
|
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 != "" {
|
2020-08-18 08:30:53 +00:00
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_IIFNAME, []byte(rule.IifName+"\x00")))
|
2015-11-26 10:50:07 +00:00
|
|
|
}
|
|
|
|
if rule.OifName != "" {
|
2020-08-18 08:30:53 +00:00
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_OIFNAME, []byte(rule.OifName+"\x00")))
|
2015-11-26 10:50:07 +00:00
|
|
|
}
|
2015-12-09 13:07:11 +00:00
|
|
|
if rule.Goto >= 0 {
|
2019-06-04 01:59:58 +00:00
|
|
|
msg.Type = nl.FR_ACT_GOTO
|
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))
|
|
|
|
}
|
|
|
|
|
2022-01-18 13:20:09 +00:00
|
|
|
if rule.IPProto > 0 {
|
|
|
|
b := make([]byte, 4)
|
|
|
|
native.PutUint32(b, uint32(rule.IPProto))
|
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_IP_PROTO, b))
|
|
|
|
}
|
|
|
|
|
2020-01-16 16:27:48 +00:00
|
|
|
if rule.Dport != nil {
|
|
|
|
b := rule.Dport.toRtAttrData()
|
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_DPORT_RANGE, b))
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule.Sport != nil {
|
|
|
|
b := rule.Sport.toRtAttrData()
|
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_SPORT_RANGE, b))
|
|
|
|
}
|
|
|
|
|
2022-04-27 23:49:27 +00:00
|
|
|
if rule.UIDRange != nil {
|
|
|
|
b := rule.UIDRange.toRtAttrData()
|
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_UID_RANGE, b))
|
|
|
|
}
|
|
|
|
|
2023-03-10 11:50:29 +00:00
|
|
|
if rule.Protocol > 0 {
|
|
|
|
req.AddData(nl.NewRtAttr(nl.FRA_PROTOCOL, nl.Uint8Attr(rule.Protocol)))
|
|
|
|
}
|
|
|
|
|
2017-10-20 20:38:07 +00:00
|
|
|
_, err := req.Execute(unix.NETLINK_ROUTE, 0)
|
2015-11-26 10:50:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RuleList lists rules in the system.
|
|
|
|
// Equivalent to: ip rule list
|
Add ErrDumpInterrupted
Add a specific error to report that a netlink response had
NLM_F_DUMP_INTR set, indicating that the set of results may be
incomplete or inconsistent.
unix.EINTR was previously returned (with no results) when the
NLM_F_DUMP_INTR flag was set. Now, errors.Is(err, unix.EINTR) will
still work. But, this will be a breaking change for any code that's
checking for equality with unix.EINTR.
Return results with ErrDumpInterrupted. Results may be incomplete
or inconsistent, but give the caller the option of using them.
Look for NLM_F_DUMP_INTR in more places:
- linkSubscribeAt, neighSubscribeAt, routeSubscribeAt
- can do an initial dump, which may report inconsistent results
-> if there's an error callback, call it with ErrDumpInterrupted
- socketDiagXDPExecutor
- makes an NLM_F_DUMP request, without using Execute()
-> give it the same behaviour as functions that do use Execute()
Signed-off-by: Rob Murray <rob.murray@docker.com>
2024-09-05 08:25:13 +00:00
|
|
|
//
|
|
|
|
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
|
|
|
|
// or incomplete.
|
2015-11-26 10:50:07 +00:00
|
|
|
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
|
Add ErrDumpInterrupted
Add a specific error to report that a netlink response had
NLM_F_DUMP_INTR set, indicating that the set of results may be
incomplete or inconsistent.
unix.EINTR was previously returned (with no results) when the
NLM_F_DUMP_INTR flag was set. Now, errors.Is(err, unix.EINTR) will
still work. But, this will be a breaking change for any code that's
checking for equality with unix.EINTR.
Return results with ErrDumpInterrupted. Results may be incomplete
or inconsistent, but give the caller the option of using them.
Look for NLM_F_DUMP_INTR in more places:
- linkSubscribeAt, neighSubscribeAt, routeSubscribeAt
- can do an initial dump, which may report inconsistent results
-> if there's an error callback, call it with ErrDumpInterrupted
- socketDiagXDPExecutor
- makes an NLM_F_DUMP request, without using Execute()
-> give it the same behaviour as functions that do use Execute()
Signed-off-by: Rob Murray <rob.murray@docker.com>
2024-09-05 08:25:13 +00:00
|
|
|
//
|
|
|
|
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
|
|
|
|
// or incomplete.
|
2016-05-08 18:35:49 +00:00
|
|
|
func (h *Handle) RuleList(family int) ([]Rule, error) {
|
2020-04-30 02:49:27 +00:00
|
|
|
return h.RuleListFiltered(family, nil, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RuleListFiltered gets a list of rules in the system filtered by the
|
|
|
|
// specified rule template `filter`.
|
|
|
|
// Equivalent to: ip rule list
|
Add ErrDumpInterrupted
Add a specific error to report that a netlink response had
NLM_F_DUMP_INTR set, indicating that the set of results may be
incomplete or inconsistent.
unix.EINTR was previously returned (with no results) when the
NLM_F_DUMP_INTR flag was set. Now, errors.Is(err, unix.EINTR) will
still work. But, this will be a breaking change for any code that's
checking for equality with unix.EINTR.
Return results with ErrDumpInterrupted. Results may be incomplete
or inconsistent, but give the caller the option of using them.
Look for NLM_F_DUMP_INTR in more places:
- linkSubscribeAt, neighSubscribeAt, routeSubscribeAt
- can do an initial dump, which may report inconsistent results
-> if there's an error callback, call it with ErrDumpInterrupted
- socketDiagXDPExecutor
- makes an NLM_F_DUMP request, without using Execute()
-> give it the same behaviour as functions that do use Execute()
Signed-off-by: Rob Murray <rob.murray@docker.com>
2024-09-05 08:25:13 +00:00
|
|
|
//
|
|
|
|
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
|
|
|
|
// or incomplete.
|
2020-04-30 02:49:27 +00:00
|
|
|
func RuleListFiltered(family int, filter *Rule, filterMask uint64) ([]Rule, error) {
|
|
|
|
return pkgHandle.RuleListFiltered(family, filter, filterMask)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RuleListFiltered lists rules in the system.
|
|
|
|
// Equivalent to: ip rule list
|
Add ErrDumpInterrupted
Add a specific error to report that a netlink response had
NLM_F_DUMP_INTR set, indicating that the set of results may be
incomplete or inconsistent.
unix.EINTR was previously returned (with no results) when the
NLM_F_DUMP_INTR flag was set. Now, errors.Is(err, unix.EINTR) will
still work. But, this will be a breaking change for any code that's
checking for equality with unix.EINTR.
Return results with ErrDumpInterrupted. Results may be incomplete
or inconsistent, but give the caller the option of using them.
Look for NLM_F_DUMP_INTR in more places:
- linkSubscribeAt, neighSubscribeAt, routeSubscribeAt
- can do an initial dump, which may report inconsistent results
-> if there's an error callback, call it with ErrDumpInterrupted
- socketDiagXDPExecutor
- makes an NLM_F_DUMP request, without using Execute()
-> give it the same behaviour as functions that do use Execute()
Signed-off-by: Rob Murray <rob.murray@docker.com>
2024-09-05 08:25:13 +00:00
|
|
|
//
|
|
|
|
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
|
|
|
|
// or incomplete.
|
2020-04-30 02:49:27 +00:00
|
|
|
func (h *Handle) RuleListFiltered(family int, filter *Rule, filterMask uint64) ([]Rule, error) {
|
2017-10-20 20:38:07 +00:00
|
|
|
req := h.newNetlinkRequest(unix.RTM_GETRULE, unix.NLM_F_DUMP|unix.NLM_F_REQUEST)
|
2015-11-26 10:50:07 +00:00
|
|
|
msg := nl.NewIfInfomsg(family)
|
|
|
|
req.AddData(msg)
|
|
|
|
|
Add ErrDumpInterrupted
Add a specific error to report that a netlink response had
NLM_F_DUMP_INTR set, indicating that the set of results may be
incomplete or inconsistent.
unix.EINTR was previously returned (with no results) when the
NLM_F_DUMP_INTR flag was set. Now, errors.Is(err, unix.EINTR) will
still work. But, this will be a breaking change for any code that's
checking for equality with unix.EINTR.
Return results with ErrDumpInterrupted. Results may be incomplete
or inconsistent, but give the caller the option of using them.
Look for NLM_F_DUMP_INTR in more places:
- linkSubscribeAt, neighSubscribeAt, routeSubscribeAt
- can do an initial dump, which may report inconsistent results
-> if there's an error callback, call it with ErrDumpInterrupted
- socketDiagXDPExecutor
- makes an NLM_F_DUMP request, without using Execute()
-> give it the same behaviour as functions that do use Execute()
Signed-off-by: Rob Murray <rob.murray@docker.com>
2024-09-05 08:25:13 +00:00
|
|
|
msgs, executeErr := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWRULE)
|
|
|
|
if executeErr != nil && !errors.Is(executeErr, ErrDumpInterrupted) {
|
|
|
|
return nil, executeErr
|
2015-11-26 10:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2024-03-27 21:23:31 +00:00
|
|
|
rule.Priority = 0 // The default priority from kernel
|
2015-11-26 10:50:07 +00:00
|
|
|
|
2017-11-07 17:34:56 +00:00
|
|
|
rule.Invert = msg.Flags&FibRuleInvert > 0
|
2023-07-10 22:37:32 +00:00
|
|
|
rule.Family = int(msg.Family)
|
2020-02-08 20:55:55 +00:00
|
|
|
rule.Tos = uint(msg.Tos)
|
2017-11-07 17:34:56 +00:00
|
|
|
|
2015-11-26 10:50:07 +00:00
|
|
|
for j := range attrs {
|
|
|
|
switch attrs[j].Attr.Type {
|
2017-10-20 20:38:07 +00:00
|
|
|
case unix.RTA_TABLE:
|
2015-11-26 10:50:07 +00:00
|
|
|
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:
|
2024-07-29 09:12:06 +00:00
|
|
|
rule.Mark = native.Uint32(attrs[j].Value[0:4])
|
2015-11-26 10:50:07 +00:00
|
|
|
case nl.FRA_FWMASK:
|
2024-07-29 09:12:06 +00:00
|
|
|
mask := native.Uint32(attrs[j].Value[0:4])
|
|
|
|
rule.Mask = &mask
|
2015-12-09 13:14:24 +00:00
|
|
|
case nl.FRA_TUN_ID:
|
2021-08-10 22:41:03 +00:00
|
|
|
rule.TunID = uint(native.Uint64(attrs[j].Value[0:8]))
|
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]))
|
2022-01-18 13:20:09 +00:00
|
|
|
case nl.FRA_IP_PROTO:
|
|
|
|
rule.IPProto = int(native.Uint32(attrs[j].Value[0:4]))
|
2020-01-16 16:27:48 +00:00
|
|
|
case nl.FRA_DPORT_RANGE:
|
|
|
|
rule.Dport = NewRulePortRange(native.Uint16(attrs[j].Value[0:2]), native.Uint16(attrs[j].Value[2:4]))
|
|
|
|
case nl.FRA_SPORT_RANGE:
|
|
|
|
rule.Sport = NewRulePortRange(native.Uint16(attrs[j].Value[0:2]), native.Uint16(attrs[j].Value[2:4]))
|
2022-04-27 23:49:27 +00:00
|
|
|
case nl.FRA_UID_RANGE:
|
|
|
|
rule.UIDRange = NewRuleUIDRange(native.Uint32(attrs[j].Value[0:4]), native.Uint32(attrs[j].Value[4:8]))
|
2023-03-10 11:50:29 +00:00
|
|
|
case nl.FRA_PROTOCOL:
|
|
|
|
rule.Protocol = uint8(attrs[j].Value[0])
|
2015-11-26 10:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-30 02:49:27 +00:00
|
|
|
|
|
|
|
if filter != nil {
|
|
|
|
switch {
|
|
|
|
case filterMask&RT_FILTER_SRC != 0 &&
|
|
|
|
(rule.Src == nil || rule.Src.String() != filter.Src.String()):
|
|
|
|
continue
|
|
|
|
case filterMask&RT_FILTER_DST != 0 &&
|
|
|
|
(rule.Dst == nil || rule.Dst.String() != filter.Dst.String()):
|
|
|
|
continue
|
|
|
|
case filterMask&RT_FILTER_TABLE != 0 &&
|
|
|
|
filter.Table != unix.RT_TABLE_UNSPEC && rule.Table != filter.Table:
|
|
|
|
continue
|
|
|
|
case filterMask&RT_FILTER_TOS != 0 && rule.Tos != filter.Tos:
|
|
|
|
continue
|
|
|
|
case filterMask&RT_FILTER_PRIORITY != 0 && rule.Priority != filter.Priority:
|
|
|
|
continue
|
|
|
|
case filterMask&RT_FILTER_MARK != 0 && rule.Mark != filter.Mark:
|
|
|
|
continue
|
2024-07-29 09:12:06 +00:00
|
|
|
case filterMask&RT_FILTER_MASK != 0 && !ptrEqual(rule.Mask, filter.Mask):
|
2020-04-30 02:49:27 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 13:07:11 +00:00
|
|
|
res = append(res, *rule)
|
2015-11-26 10:50:07 +00:00
|
|
|
}
|
|
|
|
|
Add ErrDumpInterrupted
Add a specific error to report that a netlink response had
NLM_F_DUMP_INTR set, indicating that the set of results may be
incomplete or inconsistent.
unix.EINTR was previously returned (with no results) when the
NLM_F_DUMP_INTR flag was set. Now, errors.Is(err, unix.EINTR) will
still work. But, this will be a breaking change for any code that's
checking for equality with unix.EINTR.
Return results with ErrDumpInterrupted. Results may be incomplete
or inconsistent, but give the caller the option of using them.
Look for NLM_F_DUMP_INTR in more places:
- linkSubscribeAt, neighSubscribeAt, routeSubscribeAt
- can do an initial dump, which may report inconsistent results
-> if there's an error callback, call it with ErrDumpInterrupted
- socketDiagXDPExecutor
- makes an NLM_F_DUMP request, without using Execute()
-> give it the same behaviour as functions that do use Execute()
Signed-off-by: Rob Murray <rob.murray@docker.com>
2024-09-05 08:25:13 +00:00
|
|
|
return res, executeErr
|
2015-11-26 10:50:07 +00:00
|
|
|
}
|
2020-01-16 16:27:48 +00:00
|
|
|
|
|
|
|
func (pr *RulePortRange) toRtAttrData() []byte {
|
|
|
|
b := [][]byte{make([]byte, 2), make([]byte, 2)}
|
|
|
|
native.PutUint16(b[0], pr.Start)
|
|
|
|
native.PutUint16(b[1], pr.End)
|
|
|
|
return bytes.Join(b, []byte{})
|
|
|
|
}
|
2022-04-27 23:49:27 +00:00
|
|
|
|
|
|
|
func (pr *RuleUIDRange) toRtAttrData() []byte {
|
|
|
|
b := [][]byte{make([]byte, 4), make([]byte, 4)}
|
|
|
|
native.PutUint32(b[0], pr.Start)
|
|
|
|
native.PutUint32(b[1], pr.End)
|
|
|
|
return bytes.Join(b, []byte{})
|
|
|
|
}
|
2024-07-29 09:12:06 +00:00
|
|
|
|
|
|
|
func ptrEqual(a, b *uint32) bool {
|
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if (a == nil) || (b == nil) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return *a == *b
|
|
|
|
}
|
2024-08-22 19:13:23 +00:00
|
|
|
|
|
|
|
func (r Rule) typeString() string {
|
|
|
|
switch r.Type {
|
|
|
|
case unix.RTN_UNSPEC: // zero
|
|
|
|
return ""
|
|
|
|
case unix.RTN_UNICAST:
|
|
|
|
return ""
|
|
|
|
case unix.RTN_LOCAL:
|
|
|
|
return "local"
|
|
|
|
case unix.RTN_BROADCAST:
|
|
|
|
return "broadcast"
|
|
|
|
case unix.RTN_ANYCAST:
|
|
|
|
return "anycast"
|
|
|
|
case unix.RTN_MULTICAST:
|
|
|
|
return "multicast"
|
|
|
|
case unix.RTN_BLACKHOLE:
|
|
|
|
return "blackhole"
|
|
|
|
case unix.RTN_UNREACHABLE:
|
|
|
|
return "unreachable"
|
|
|
|
case unix.RTN_PROHIBIT:
|
|
|
|
return "prohibit"
|
|
|
|
case unix.RTN_THROW:
|
|
|
|
return "throw"
|
|
|
|
case unix.RTN_NAT:
|
|
|
|
return "nat"
|
|
|
|
case unix.RTN_XRESOLVE:
|
|
|
|
return "xresolve"
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("type(0x%x)", r.Type)
|
|
|
|
}
|
|
|
|
}
|