2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
package filter
|
|
|
|
|
|
|
|
import (
|
2024-06-18 20:44:12 +00:00
|
|
|
"net/netip"
|
|
|
|
|
2020-12-20 00:43:25 +00:00
|
|
|
"tailscale.com/net/packet"
|
2024-06-18 20:44:12 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2024-06-18 19:05:34 +00:00
|
|
|
"tailscale.com/types/views"
|
2024-06-16 18:34:11 +00:00
|
|
|
"tailscale.com/wgengine/filter/filtertype"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
2024-06-16 18:34:11 +00:00
|
|
|
type matches []filtertype.Match
|
2020-12-20 00:43:25 +00:00
|
|
|
|
2024-06-18 20:44:12 +00:00
|
|
|
func (ms matches) match(q *packet.Parsed, hasCap CapTestFunc) bool {
|
|
|
|
for i := range ms {
|
|
|
|
m := &ms[i]
|
2024-06-18 19:05:34 +00:00
|
|
|
if !views.SliceContains(m.IPProto, q.IPProto) {
|
2021-03-17 21:24:32 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-06-18 20:44:12 +00:00
|
|
|
if !srcMatches(m, q.Src.Addr(), hasCap) {
|
2020-12-20 00:43:25 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, dst := range m.Dsts {
|
2022-07-25 03:08:42 +00:00
|
|
|
if !dst.Net.Contains(q.Dst.Addr()) {
|
2020-12-20 00:43:25 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-06-16 18:34:11 +00:00
|
|
|
if !dst.Ports.Contains(q.Dst.Port()) {
|
2020-12-20 00:43:25 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-06-18 20:44:12 +00:00
|
|
|
// srcMatches reports whether srcAddr matche the src requirements in m, either
|
|
|
|
// by Srcs (using SrcsContains), or by the node having a capability listed
|
|
|
|
// in SrcCaps using the provided hasCap function.
|
|
|
|
func srcMatches(m *filtertype.Match, srcAddr netip.Addr, hasCap CapTestFunc) bool {
|
|
|
|
if m.SrcsContains(srcAddr) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if hasCap != nil {
|
|
|
|
for _, c := range m.SrcCaps {
|
|
|
|
if hasCap(srcAddr, c) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// CapTestFunc is the function signature of a function that tests whether srcIP
|
|
|
|
// has a given capability.
|
|
|
|
//
|
|
|
|
// It it used in the fast path of evaluating filter rules so should be fast.
|
|
|
|
type CapTestFunc = func(srcIP netip.Addr, cap tailcfg.NodeCapability) bool
|
|
|
|
|
|
|
|
func (ms matches) matchIPsOnly(q *packet.Parsed, hasCap CapTestFunc) bool {
|
|
|
|
srcAddr := q.Src.Addr()
|
2020-12-20 00:43:25 +00:00
|
|
|
for _, m := range ms {
|
2024-06-18 20:44:12 +00:00
|
|
|
if !m.SrcsContains(srcAddr) {
|
2020-12-20 00:43:25 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, dst := range m.Dsts {
|
2022-07-25 03:08:42 +00:00
|
|
|
if dst.Net.Contains(q.Dst.Addr()) {
|
2020-12-20 00:43:25 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-18 20:44:12 +00:00
|
|
|
if hasCap != nil {
|
|
|
|
for _, m := range ms {
|
|
|
|
for _, c := range m.SrcCaps {
|
|
|
|
if hasCap(srcAddr, c) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-20 00:43:25 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-12-04 19:52:39 +00:00
|
|
|
// matchProtoAndIPsOnlyIfAllPorts reports q matches any Match in ms where the
|
|
|
|
// Match if for the right IP Protocol and IP address, but ports are
|
|
|
|
// ignored, as long as the match is for the entire uint16 port range.
|
|
|
|
func (ms matches) matchProtoAndIPsOnlyIfAllPorts(q *packet.Parsed) bool {
|
|
|
|
for _, m := range ms {
|
2024-06-18 19:05:34 +00:00
|
|
|
if !views.SliceContains(m.IPProto, q.IPProto) {
|
2021-12-04 19:52:39 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-06-16 01:20:17 +00:00
|
|
|
if !m.SrcsContains(q.Src.Addr()) {
|
2021-12-04 19:52:39 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, dst := range m.Dsts {
|
2024-06-16 18:34:11 +00:00
|
|
|
if dst.Ports != filtertype.AllPorts {
|
2021-12-04 19:52:39 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
if dst.Net.Contains(q.Dst.Addr()) {
|
2021-12-04 19:52:39 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|