diff options
author | gVisor bot <gvisor-bot@google.com> | 2020-03-16 18:17:16 +0000 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2020-03-16 18:17:16 +0000 |
commit | f364c384715dba6d78df15b7d5229d7d6772ab34 (patch) | |
tree | c1e1a6c473455e58eb3be7af60d799b4ee53b75e /pkg/tcpip/iptables/iptables.go | |
parent | 92305e29a3911a29e94a1bc70d488a6f67011660 (diff) | |
parent | 159a230b9b8483317bf7ac6cf3113d73a1dcf702 (diff) |
Merge release-20200219.0-181-g159a230 (automated)
Diffstat (limited to 'pkg/tcpip/iptables/iptables.go')
-rw-r--r-- | pkg/tcpip/iptables/iptables.go | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/pkg/tcpip/iptables/iptables.go b/pkg/tcpip/iptables/iptables.go index 2ab9e0675..d30571c74 100644 --- a/pkg/tcpip/iptables/iptables.go +++ b/pkg/tcpip/iptables/iptables.go @@ -267,9 +267,8 @@ func (it *IPTables) checkRule(hook Hook, pkt tcpip.PacketBuffer, table Table, ru pkt.NetworkHeader = pkt.Data.First() } - // First check whether the packet matches the IP header filter. - // TODO(gvisor.dev/issue/170): Support other fields of the filter. - if rule.Filter.Protocol != 0 && rule.Filter.Protocol != header.IPv4(pkt.NetworkHeader).TransportProtocol() { + // Check whether the packet matches the IP header filter. + if !filterMatch(rule.Filter, header.IPv4(pkt.NetworkHeader)) { // Continue on to the next rule. return RuleJump, ruleIdx + 1 } @@ -290,3 +289,26 @@ func (it *IPTables) checkRule(hook Hook, pkt tcpip.PacketBuffer, table Table, ru // All the matchers matched, so run the target. return rule.Target.Action(pkt) } + +func filterMatch(filter IPHeaderFilter, hdr header.IPv4) bool { + // TODO(gvisor.dev/issue/170): Support other fields of the filter. + // Check the transport protocol. + if filter.Protocol != 0 && filter.Protocol != hdr.TransportProtocol() { + return false + } + + // Check the destination IP. + dest := hdr.DestinationAddress() + matches := true + for i := range filter.Dst { + if dest[i]&filter.DstMask[i] != filter.Dst[i] { + matches = false + break + } + } + if matches == filter.DstInvert { + return false + } + + return true +} |