2023-01-27 13:37:20 -08:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-02-05 14:16:58 -08:00
|
|
|
|
|
|
|
package filter
|
|
|
|
|
|
|
|
import (
|
2024-06-18 13:44:12 -07:00
|
|
|
"net/netip"
|
|
|
|
|
2020-12-19 16:43:25 -08:00
|
|
|
"tailscale.com/net/packet"
|
2024-06-18 13:44:12 -07:00
|
|
|
"tailscale.com/tailcfg"
|
2024-06-18 12:05:34 -07:00
|
|
|
"tailscale.com/types/views"
|
2024-06-16 11:34:11 -07:00
|
|
|
"tailscale.com/wgengine/filter/filtertype"
|
2020-02-05 14:16:58 -08:00
|
|
|
)
|
|
|
|
|
2024-06-16 11:34:11 -07:00
|
|
|
type matches []filtertype.Match
|
2020-12-19 16:43:25 -08:00
|
|
|
|
2024-06-18 13:44:12 -07:00
|
|
|
func (ms matches) match(q *packet.Parsed, hasCap CapTestFunc) bool {
|
|
|
|
for i := range ms {
|
|
|
|
m := &ms[i]
|
2024-06-18 12:05:34 -07:00
|
|
|
if !views.SliceContains(m.IPProto, q.IPProto) {
|
2021-03-17 14:24:32 -07:00
|
|
|
continue
|
|
|
|
}
|
2024-06-18 13:44:12 -07:00
|
|
|
if !srcMatches(m, q.Src.Addr(), hasCap) {
|
2020-12-19 16:43:25 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, dst := range m.Dsts {
|
2022-07-24 20:08:42 -07:00
|
|
|
if !dst.Net.Contains(q.Dst.Addr()) {
|
2020-12-19 16:43:25 -08:00
|
|
|
continue
|
|
|
|
}
|
2024-06-16 11:34:11 -07:00
|
|
|
if !dst.Ports.Contains(q.Dst.Port()) {
|
2020-12-19 16:43:25 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-06-18 13:44:12 -07: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-19 16:43:25 -08:00
|
|
|
for _, m := range ms {
|
2024-06-18 13:44:12 -07:00
|
|
|
if !m.SrcsContains(srcAddr) {
|
2020-12-19 16:43:25 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, dst := range m.Dsts {
|
2022-07-24 20:08:42 -07:00
|
|
|
if dst.Net.Contains(q.Dst.Addr()) {
|
2020-12-19 16:43:25 -08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-18 13:44:12 -07:00
|
|
|
if hasCap != nil {
|
|
|
|
for _, m := range ms {
|
|
|
|
for _, c := range m.SrcCaps {
|
|
|
|
if hasCap(srcAddr, c) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-19 16:43:25 -08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-12-04 11:52:39 -08: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 12:05:34 -07:00
|
|
|
if !views.SliceContains(m.IPProto, q.IPProto) {
|
2021-12-04 11:52:39 -08:00
|
|
|
continue
|
|
|
|
}
|
2024-06-15 18:20:17 -07:00
|
|
|
if !m.SrcsContains(q.Src.Addr()) {
|
2021-12-04 11:52:39 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, dst := range m.Dsts {
|
2024-06-16 11:34:11 -07:00
|
|
|
if dst.Ports != filtertype.AllPorts {
|
2021-12-04 11:52:39 -08:00
|
|
|
continue
|
|
|
|
}
|
2022-07-24 20:08:42 -07:00
|
|
|
if dst.Net.Contains(q.Dst.Addr()) {
|
2021-12-04 11:52:39 -08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|