mirror of
https://github.com/aljazceru/kata-containers.git
synced 2025-12-31 21:14:25 +01:00
The netlink dep needs to be updated to get logic for the tuntap
link. It is fixing a bug that uses a generic link instead.
This also requires the golang/x/sys package to be updated
for the IFLA_* constraints.
Commits for github.com/vishvananda/netlink
c8c507c fix: fix ip rule goto bug
db99c04 tuntap: Return TunTapLink instead of GenericLink
e993616 Fix unit test failure: TestNeighAddDelLLIPAddr
fb5fbae Mirred and connmark clobber their ActionAttrs
1187dc9 Fix tests
00009fb Add support for TC_ACT_CONNMARK
fafc1e7 support vlan protocol
fd97bf4 Add command to set devlink device switchdev mode
bcb80b2 Add devlink command by to get specific device name
f504738 Fix function comments based on best practices from Effective Go
e281812 Fix typos
adb577d Add support for IFLA_GSO_*
aa950f2 travis: run tests with Go 1.12.x
b64d7bc travis: specify go_import_path
b9cafe4 remove redundant type assertions in type switch
1e2e7ab Add Support for Virtual XFRM Interfaces
48a75e0 Fix Race Condition in TestXfrmMonitorExpire
e37f4b4 Avoid 64K allocation on the heap with each Receive
332a698 Add devlink commands for devlink device information
cb78b18 neigh_linux: Fix failure on deleted link neighs updates
2bc5004 Replace redundant copied u32 types with type aliases
093e80f Pass Ndmsg to NeighListExecute
78a3099 Make test suite more deterministic
2529893 genetlink: Add missing error check
91b013f code simplification
023a6da Make go vet happier
aa5b058 Simplify code
e137ed6 Replace nl.NewRtAttrChild with method on struct
3b1c596 Run TravisCI with Go 1.10 and 1.11
d741264 Reduce allocations
b48eed5 Add an API to rename rdma device name
02a3831 Adjust conntrack filters
d3a23fd Make AddChild more generic
1404979 Add support for hoplimit metric in routes
6d53654 Add support for neighbor subscription
531df7a Avoid serializing empty TCA_OPTIONS in qdisc messages
56b1bd2 fix: BRIDGE_FLAGS_* constants off-by-one
8aa85bf Add support for action and ifindex in XFRM policy
9eab419 Netlink: Fix Darwin build
2cbcf73 Add a test for Vlan filtering support for bridges.
0bbc55b Initial support for vlan aware bridges.
3ac69fd Add network namespace ID management.
d68dce4 Ingress qdisc add/del Test case
1006cf4 Implementation of HFSC
d85e18e Allow Tuntap non-persist, allow empty tuntap name
d77c86a protinfo: Check if object is nil
a06dabf Increase size of receive buffer
3e48e44 Revert "RTEXT_FILTER_VF doesn't always work with dump request, fixes #354"
028453c RTEXT_FILTER_VF doesn't always work with dump request, fixes #354
ee06b1d add vti6 support
b1cc70d fix prefixlen/local IP, incl. PtP addresses
7c0b594 Implemented String() for netem, fq and fq_codel in qdisc
769bb84 Adjust flags values
5f662e0 Add info about VFs on link
985ab95 Add support for link flag allmulticast
16769db Support LWTUNNEL_ENCAP_SEG6_LOCAL (including tests)
b7f0669 Add test to Add/Del IPv6 route.
55d3a80 Added tests for Gretap/Gretun devices
f07d9d5 Run both Inline/Encap mode in TestSEG6RouteAddDel
1970aef Add RDMA netlink socket for RDMA device information
dc00cf9 Add Hash to U32
23a36f2 Add Divisor to U32
85aa3b7 Add statistics to class attributes
aa0edbe Add support for setting InfininBand Node and Port GUID of a VF
41009d5 Read conntrack flow statistics
a2ad57a Add changelog file, initial release tagging
5236321 Use IFLA_* constants from x/sys/unix
25d2c79 Use IFF_MULTI_QUEUE from x/sys/unix to define TUNTAP_MULTI_QUEUE
d35d6b5 Clarify ESN bitmap length construction logic
a2af46a Add FQ Codel
465b5fe Add Fq Qdisc support
c27b7f7 Run gofmt -s -w on the project
5f5d5cd Add a 'ListExisting' option to get the existing entries in the route/addr/link tables as part of RouteSubscribeWithOptions, AddrSubscribeWithOptions, and LinkSubscribeWithOptions.
5a988e8 Support IPv6 GRE Tun and Tap
7291c36 addr_linux: Implement CacheInfo installation
422ffe6 addr_linux: Skip BROADCAST and LABEL for non-ipv4
1882fa9 Add Matchall filter
7b4c063 Update bpf_linux.go
ad19ca1 netlink: allow non linux builds to pass.
3ff4c21 Don't overwrite the XDP file descriptor with flags
d4235bf Eliminate cgo from netlink.
54ad9e3 Two new functions: LinkSetBondSlave and VethPeerIndex
f67b75e Properly tear down netns at the end of test
016ba6f Add support for managing source MACVLANs
6e7bb56 Run TestSocketGet in dedicated netns
a5d066d Fix LinkAdd for sit tunnel on 3.10 kernel
8bead6f Add requirements to conntrack tests
9ce265f Retrieve VLAN and VNI when listing neighbour
fad79cb Fix go build issue for fou code
Commits for golang/x/sys
88d2dcc unix: add IFLA_* constants for Linux 4.15
c1138c8 unix: update to Linux 4.15, glibc 2.27 and Go 1.10
37707fd unix: move gccgo redeclared *SyscallNoError functions to a separate file
8f27ce8 unix: fix cpuset size argument in sched_affinity syscall
3dbebcf unix: use SyscallNoError and RawSyscallNoError on Linux only
ff2a66f unix: fix godoc comment for clen
0346725 unix: add godoc for Sockaddr* types
90f0fdc plan9: add arm support
ef80224 unix: add sockaddr_l2 definitions
af9a212 unix: don't export padding fields on all platforms
af50095 unix: use ParseDirent from syscall
2c42eef unix: adjust replacement regex for removed struct fields for linux/s390x
fff93fa unix: add Statx on Linux
52ba35d unix: check error return of os.Symlink in tests on Linux
810d700 unix: match seek argument size to signature on linux/arm
b9cf5f9 unix: add cgroupstats type and constants
d38bf78 unix: restore gccgo support
2493af8 plan9: move Unsetenv into env_plan9.go
3ca7571 windows: move Unsetenv into env_windows.go
1792d66 unix: move Unsetenv into env_unix.go
dd9ec17 unix: fix build on Go 1.8
12d9d5b unix: add SchedGetaffinity and SchedSetaffinity on Linux
a3f2cbd unix: fix typo in unix/asm_linux_arm64.s made in 28a7276
28a7276 unix: add SyscallNoError and RawSyscallNoError on Linux
8380141 unix: simplify error handling in *listxattr on FreeBSD
df29b91 unix: add TestSelect for *BSD
801364e unix: add Select on Solaris
d818ba1 unix: remove syscall constants on Solaris
236baca unix: add timeout tests for Select and Pselect on Linux
571f7bb unix: simplify TestGetwd
d5840ad unix: add GetsockoptString for Darwin, *BSD and Solaris
Signed-off-by: Gabi Beyer <gabrielle.n.beyer@intel.com>
399 lines
14 KiB
Go
399 lines
14 KiB
Go
package netlink
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
|
|
"github.com/vishvananda/netlink/nl"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// ConntrackTableType Conntrack table for the netlink operation
|
|
type ConntrackTableType uint8
|
|
|
|
const (
|
|
// ConntrackTable Conntrack table
|
|
// https://github.com/torvalds/linux/blob/master/include/uapi/linux/netfilter/nfnetlink.h -> #define NFNL_SUBSYS_CTNETLINK 1
|
|
ConntrackTable = 1
|
|
// ConntrackExpectTable Conntrack expect table
|
|
// https://github.com/torvalds/linux/blob/master/include/uapi/linux/netfilter/nfnetlink.h -> #define NFNL_SUBSYS_CTNETLINK_EXP 2
|
|
ConntrackExpectTable = 2
|
|
)
|
|
const (
|
|
// For Parsing Mark
|
|
TCP_PROTO = 6
|
|
UDP_PROTO = 17
|
|
)
|
|
const (
|
|
// backward compatibility with golang 1.6 which does not have io.SeekCurrent
|
|
seekCurrent = 1
|
|
)
|
|
|
|
// InetFamily Family type
|
|
type InetFamily uint8
|
|
|
|
// -L [table] [options] List conntrack or expectation table
|
|
// -G [table] parameters Get conntrack or expectation
|
|
|
|
// -I [table] parameters Create a conntrack or expectation
|
|
// -U [table] parameters Update a conntrack
|
|
// -E [table] [options] Show events
|
|
|
|
// -C [table] Show counter
|
|
// -S Show statistics
|
|
|
|
// ConntrackTableList returns the flow list of a table of a specific family
|
|
// conntrack -L [table] [options] List conntrack or expectation table
|
|
func ConntrackTableList(table ConntrackTableType, family InetFamily) ([]*ConntrackFlow, error) {
|
|
return pkgHandle.ConntrackTableList(table, family)
|
|
}
|
|
|
|
// ConntrackTableFlush flushes all the flows of a specified table
|
|
// conntrack -F [table] Flush table
|
|
// The flush operation applies to all the family types
|
|
func ConntrackTableFlush(table ConntrackTableType) error {
|
|
return pkgHandle.ConntrackTableFlush(table)
|
|
}
|
|
|
|
// ConntrackDeleteFilter deletes entries on the specified table on the base of the filter
|
|
// conntrack -D [table] parameters Delete conntrack or expectation
|
|
func ConntrackDeleteFilter(table ConntrackTableType, family InetFamily, filter CustomConntrackFilter) (uint, error) {
|
|
return pkgHandle.ConntrackDeleteFilter(table, family, filter)
|
|
}
|
|
|
|
// ConntrackTableList returns the flow list of a table of a specific family using the netlink handle passed
|
|
// conntrack -L [table] [options] List conntrack or expectation table
|
|
func (h *Handle) ConntrackTableList(table ConntrackTableType, family InetFamily) ([]*ConntrackFlow, error) {
|
|
res, err := h.dumpConntrackTable(table, family)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Deserialize all the flows
|
|
var result []*ConntrackFlow
|
|
for _, dataRaw := range res {
|
|
result = append(result, parseRawData(dataRaw))
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// ConntrackTableFlush flushes all the flows of a specified table using the netlink handle passed
|
|
// conntrack -F [table] Flush table
|
|
// The flush operation applies to all the family types
|
|
func (h *Handle) ConntrackTableFlush(table ConntrackTableType) error {
|
|
req := h.newConntrackRequest(table, unix.AF_INET, nl.IPCTNL_MSG_CT_DELETE, unix.NLM_F_ACK)
|
|
_, err := req.Execute(unix.NETLINK_NETFILTER, 0)
|
|
return err
|
|
}
|
|
|
|
// ConntrackDeleteFilter deletes entries on the specified table on the base of the filter using the netlink handle passed
|
|
// conntrack -D [table] parameters Delete conntrack or expectation
|
|
func (h *Handle) ConntrackDeleteFilter(table ConntrackTableType, family InetFamily, filter CustomConntrackFilter) (uint, error) {
|
|
res, err := h.dumpConntrackTable(table, family)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
var matched uint
|
|
for _, dataRaw := range res {
|
|
flow := parseRawData(dataRaw)
|
|
if match := filter.MatchConntrackFlow(flow); match {
|
|
req2 := h.newConntrackRequest(table, family, nl.IPCTNL_MSG_CT_DELETE, unix.NLM_F_ACK)
|
|
// skip the first 4 byte that are the netfilter header, the newConntrackRequest is adding it already
|
|
req2.AddRawData(dataRaw[4:])
|
|
req2.Execute(unix.NETLINK_NETFILTER, 0)
|
|
matched++
|
|
}
|
|
}
|
|
|
|
return matched, nil
|
|
}
|
|
|
|
func (h *Handle) newConntrackRequest(table ConntrackTableType, family InetFamily, operation, flags int) *nl.NetlinkRequest {
|
|
// Create the Netlink request object
|
|
req := h.newNetlinkRequest((int(table)<<8)|operation, flags)
|
|
// Add the netfilter header
|
|
msg := &nl.Nfgenmsg{
|
|
NfgenFamily: uint8(family),
|
|
Version: nl.NFNETLINK_V0,
|
|
ResId: 0,
|
|
}
|
|
req.AddData(msg)
|
|
return req
|
|
}
|
|
|
|
func (h *Handle) dumpConntrackTable(table ConntrackTableType, family InetFamily) ([][]byte, error) {
|
|
req := h.newConntrackRequest(table, family, nl.IPCTNL_MSG_CT_GET, unix.NLM_F_DUMP)
|
|
return req.Execute(unix.NETLINK_NETFILTER, 0)
|
|
}
|
|
|
|
// The full conntrack flow structure is very complicated and can be found in the file:
|
|
// http://git.netfilter.org/libnetfilter_conntrack/tree/include/internal/object.h
|
|
// For the time being, the structure below allows to parse and extract the base information of a flow
|
|
type ipTuple struct {
|
|
Bytes uint64
|
|
DstIP net.IP
|
|
DstPort uint16
|
|
Packets uint64
|
|
Protocol uint8
|
|
SrcIP net.IP
|
|
SrcPort uint16
|
|
}
|
|
|
|
type ConntrackFlow struct {
|
|
FamilyType uint8
|
|
Forward ipTuple
|
|
Reverse ipTuple
|
|
Mark uint32
|
|
}
|
|
|
|
func (s *ConntrackFlow) String() string {
|
|
// conntrack cmd output:
|
|
// udp 17 src=127.0.0.1 dst=127.0.0.1 sport=4001 dport=1234 packets=5 bytes=532 [UNREPLIED] src=127.0.0.1 dst=127.0.0.1 sport=1234 dport=4001 packets=10 bytes=1078 mark=0
|
|
return fmt.Sprintf("%s\t%d src=%s dst=%s sport=%d dport=%d packets=%d bytes=%d\tsrc=%s dst=%s sport=%d dport=%d packets=%d bytes=%d mark=%d",
|
|
nl.L4ProtoMap[s.Forward.Protocol], s.Forward.Protocol,
|
|
s.Forward.SrcIP.String(), s.Forward.DstIP.String(), s.Forward.SrcPort, s.Forward.DstPort, s.Forward.Packets, s.Forward.Bytes,
|
|
s.Reverse.SrcIP.String(), s.Reverse.DstIP.String(), s.Reverse.SrcPort, s.Reverse.DstPort, s.Reverse.Packets, s.Reverse.Bytes,
|
|
s.Mark)
|
|
}
|
|
|
|
// This method parse the ip tuple structure
|
|
// The message structure is the following:
|
|
// <len, [CTA_IP_V4_SRC|CTA_IP_V6_SRC], 16 bytes for the IP>
|
|
// <len, [CTA_IP_V4_DST|CTA_IP_V6_DST], 16 bytes for the IP>
|
|
// <len, NLA_F_NESTED|nl.CTA_TUPLE_PROTO, 1 byte for the protocol, 3 bytes of padding>
|
|
// <len, CTA_PROTO_SRC_PORT, 2 bytes for the source port, 2 bytes of padding>
|
|
// <len, CTA_PROTO_DST_PORT, 2 bytes for the source port, 2 bytes of padding>
|
|
func parseIpTuple(reader *bytes.Reader, tpl *ipTuple) uint8 {
|
|
for i := 0; i < 2; i++ {
|
|
_, t, _, v := parseNfAttrTLV(reader)
|
|
switch t {
|
|
case nl.CTA_IP_V4_SRC, nl.CTA_IP_V6_SRC:
|
|
tpl.SrcIP = v
|
|
case nl.CTA_IP_V4_DST, nl.CTA_IP_V6_DST:
|
|
tpl.DstIP = v
|
|
}
|
|
}
|
|
// Skip the next 4 bytes nl.NLA_F_NESTED|nl.CTA_TUPLE_PROTO
|
|
reader.Seek(4, seekCurrent)
|
|
_, t, _, v := parseNfAttrTLV(reader)
|
|
if t == nl.CTA_PROTO_NUM {
|
|
tpl.Protocol = uint8(v[0])
|
|
}
|
|
// Skip some padding 3 bytes
|
|
reader.Seek(3, seekCurrent)
|
|
for i := 0; i < 2; i++ {
|
|
_, t, _ := parseNfAttrTL(reader)
|
|
switch t {
|
|
case nl.CTA_PROTO_SRC_PORT:
|
|
parseBERaw16(reader, &tpl.SrcPort)
|
|
case nl.CTA_PROTO_DST_PORT:
|
|
parseBERaw16(reader, &tpl.DstPort)
|
|
}
|
|
// Skip some padding 2 byte
|
|
reader.Seek(2, seekCurrent)
|
|
}
|
|
return tpl.Protocol
|
|
}
|
|
|
|
func parseNfAttrTLV(r *bytes.Reader) (isNested bool, attrType, len uint16, value []byte) {
|
|
isNested, attrType, len = parseNfAttrTL(r)
|
|
|
|
value = make([]byte, len)
|
|
binary.Read(r, binary.BigEndian, &value)
|
|
return isNested, attrType, len, value
|
|
}
|
|
|
|
func parseNfAttrTL(r *bytes.Reader) (isNested bool, attrType, len uint16) {
|
|
binary.Read(r, nl.NativeEndian(), &len)
|
|
len -= nl.SizeofNfattr
|
|
|
|
binary.Read(r, nl.NativeEndian(), &attrType)
|
|
isNested = (attrType & nl.NLA_F_NESTED) == nl.NLA_F_NESTED
|
|
attrType = attrType & (nl.NLA_F_NESTED - 1)
|
|
|
|
return isNested, attrType, len
|
|
}
|
|
|
|
func parseBERaw16(r *bytes.Reader, v *uint16) {
|
|
binary.Read(r, binary.BigEndian, v)
|
|
}
|
|
|
|
func parseBERaw64(r *bytes.Reader, v *uint64) {
|
|
binary.Read(r, binary.BigEndian, v)
|
|
}
|
|
|
|
func parseByteAndPacketCounters(r *bytes.Reader) (bytes, packets uint64) {
|
|
for i := 0; i < 2; i++ {
|
|
switch _, t, _ := parseNfAttrTL(r); t {
|
|
case nl.CTA_COUNTERS_BYTES:
|
|
parseBERaw64(r, &bytes)
|
|
case nl.CTA_COUNTERS_PACKETS:
|
|
parseBERaw64(r, &packets)
|
|
default:
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func parseRawData(data []byte) *ConntrackFlow {
|
|
s := &ConntrackFlow{}
|
|
var proto uint8
|
|
// First there is the Nfgenmsg header
|
|
// consume only the family field
|
|
reader := bytes.NewReader(data)
|
|
binary.Read(reader, nl.NativeEndian(), &s.FamilyType)
|
|
|
|
// skip rest of the Netfilter header
|
|
reader.Seek(3, seekCurrent)
|
|
// The message structure is the following:
|
|
// <len, NLA_F_NESTED|CTA_TUPLE_ORIG> 4 bytes
|
|
// <len, NLA_F_NESTED|CTA_TUPLE_IP> 4 bytes
|
|
// flow information of the forward flow
|
|
// <len, NLA_F_NESTED|CTA_TUPLE_REPLY> 4 bytes
|
|
// <len, NLA_F_NESTED|CTA_TUPLE_IP> 4 bytes
|
|
// flow information of the reverse flow
|
|
for reader.Len() > 0 {
|
|
if nested, t, l := parseNfAttrTL(reader); nested {
|
|
switch t {
|
|
case nl.CTA_TUPLE_ORIG:
|
|
if nested, t, _ = parseNfAttrTL(reader); nested && t == nl.CTA_TUPLE_IP {
|
|
proto = parseIpTuple(reader, &s.Forward)
|
|
}
|
|
case nl.CTA_TUPLE_REPLY:
|
|
if nested, t, _ = parseNfAttrTL(reader); nested && t == nl.CTA_TUPLE_IP {
|
|
parseIpTuple(reader, &s.Reverse)
|
|
} else {
|
|
// Header not recognized skip it
|
|
reader.Seek(int64(l), seekCurrent)
|
|
}
|
|
case nl.CTA_COUNTERS_ORIG:
|
|
s.Forward.Bytes, s.Forward.Packets = parseByteAndPacketCounters(reader)
|
|
case nl.CTA_COUNTERS_REPLY:
|
|
s.Reverse.Bytes, s.Reverse.Packets = parseByteAndPacketCounters(reader)
|
|
}
|
|
}
|
|
}
|
|
if proto == TCP_PROTO {
|
|
reader.Seek(64, seekCurrent)
|
|
_, t, _, v := parseNfAttrTLV(reader)
|
|
if t == nl.CTA_MARK {
|
|
s.Mark = uint32(v[3])
|
|
}
|
|
} else if proto == UDP_PROTO {
|
|
reader.Seek(16, seekCurrent)
|
|
_, t, _, v := parseNfAttrTLV(reader)
|
|
if t == nl.CTA_MARK {
|
|
s.Mark = uint32(v[3])
|
|
}
|
|
}
|
|
return s
|
|
}
|
|
|
|
// Conntrack parameters and options:
|
|
// -n, --src-nat ip source NAT ip
|
|
// -g, --dst-nat ip destination NAT ip
|
|
// -j, --any-nat ip source or destination NAT ip
|
|
// -m, --mark mark Set mark
|
|
// -c, --secmark secmark Set selinux secmark
|
|
// -e, --event-mask eventmask Event mask, eg. NEW,DESTROY
|
|
// -z, --zero Zero counters while listing
|
|
// -o, --output type[,...] Output format, eg. xml
|
|
// -l, --label label[,...] conntrack labels
|
|
|
|
// Common parameters and options:
|
|
// -s, --src, --orig-src ip Source address from original direction
|
|
// -d, --dst, --orig-dst ip Destination address from original direction
|
|
// -r, --reply-src ip Source address from reply direction
|
|
// -q, --reply-dst ip Destination address from reply direction
|
|
// -p, --protonum proto Layer 4 Protocol, eg. 'tcp'
|
|
// -f, --family proto Layer 3 Protocol, eg. 'ipv6'
|
|
// -t, --timeout timeout Set timeout
|
|
// -u, --status status Set status, eg. ASSURED
|
|
// -w, --zone value Set conntrack zone
|
|
// --orig-zone value Set zone for original direction
|
|
// --reply-zone value Set zone for reply direction
|
|
// -b, --buffer-size Netlink socket buffer size
|
|
// --mask-src ip Source mask address
|
|
// --mask-dst ip Destination mask address
|
|
|
|
// Filter types
|
|
type ConntrackFilterType uint8
|
|
|
|
const (
|
|
ConntrackOrigSrcIP = iota // -orig-src ip Source address from original direction
|
|
ConntrackOrigDstIP // -orig-dst ip Destination address from original direction
|
|
ConntrackReplySrcIP // --reply-src ip Reply Source IP
|
|
ConntrackReplyDstIP // --reply-dst ip Reply Destination IP
|
|
ConntrackReplyAnyIP // Match source or destination reply IP
|
|
ConntrackNatSrcIP = ConntrackReplySrcIP // deprecated use instead ConntrackReplySrcIP
|
|
ConntrackNatDstIP = ConntrackReplyDstIP // deprecated use instead ConntrackReplyDstIP
|
|
ConntrackNatAnyIP = ConntrackReplyAnyIP // deprecated use instaed ConntrackReplyAnyIP
|
|
)
|
|
|
|
type CustomConntrackFilter interface {
|
|
// MatchConntrackFlow applies the filter to the flow and returns true if the flow matches
|
|
// the filter or false otherwise
|
|
MatchConntrackFlow(flow *ConntrackFlow) bool
|
|
}
|
|
|
|
type ConntrackFilter struct {
|
|
ipFilter map[ConntrackFilterType]net.IP
|
|
}
|
|
|
|
// AddIP adds an IP to the conntrack filter
|
|
func (f *ConntrackFilter) AddIP(tp ConntrackFilterType, ip net.IP) error {
|
|
if f.ipFilter == nil {
|
|
f.ipFilter = make(map[ConntrackFilterType]net.IP)
|
|
}
|
|
if _, ok := f.ipFilter[tp]; ok {
|
|
return errors.New("Filter attribute already present")
|
|
}
|
|
f.ipFilter[tp] = ip
|
|
return nil
|
|
}
|
|
|
|
// MatchConntrackFlow applies the filter to the flow and returns true if the flow matches the filter
|
|
// false otherwise
|
|
func (f *ConntrackFilter) MatchConntrackFlow(flow *ConntrackFlow) bool {
|
|
if len(f.ipFilter) == 0 {
|
|
// empty filter always not match
|
|
return false
|
|
}
|
|
|
|
match := true
|
|
// -orig-src ip Source address from original direction
|
|
if elem, found := f.ipFilter[ConntrackOrigSrcIP]; found {
|
|
match = match && elem.Equal(flow.Forward.SrcIP)
|
|
}
|
|
|
|
// -orig-dst ip Destination address from original direction
|
|
if elem, found := f.ipFilter[ConntrackOrigDstIP]; match && found {
|
|
match = match && elem.Equal(flow.Forward.DstIP)
|
|
}
|
|
|
|
// -src-nat ip Source NAT ip
|
|
if elem, found := f.ipFilter[ConntrackReplySrcIP]; match && found {
|
|
match = match && elem.Equal(flow.Reverse.SrcIP)
|
|
}
|
|
|
|
// -dst-nat ip Destination NAT ip
|
|
if elem, found := f.ipFilter[ConntrackReplyDstIP]; match && found {
|
|
match = match && elem.Equal(flow.Reverse.DstIP)
|
|
}
|
|
|
|
// Match source or destination reply IP
|
|
if elem, found := f.ipFilter[ConntrackReplyAnyIP]; match && found {
|
|
match = match && (elem.Equal(flow.Reverse.SrcIP) || elem.Equal(flow.Reverse.DstIP))
|
|
}
|
|
|
|
return match
|
|
}
|
|
|
|
var _ CustomConntrackFilter = (*ConntrackFilter)(nil)
|