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 (
|
2020-12-19 16:43:25 -08:00
|
|
|
"tailscale.com/net/packet"
|
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
|
|
|
|
|
|
|
func (ms matches) match(q *packet.Parsed) bool {
|
|
|
|
for _, m := range ms {
|
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-15 18:20:17 -07:00
|
|
|
if !m.SrcsContains(q.Src.Addr()) {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms matches) matchIPsOnly(q *packet.Parsed) bool {
|
|
|
|
for _, m := range ms {
|
2024-06-15 18:20:17 -07:00
|
|
|
if !m.SrcsContains(q.Src.Addr()) {
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|