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 (
|
2023-10-11 21:00:50 +00:00
|
|
|
"slices"
|
2020-02-29 03:27:17 +00:00
|
|
|
|
2020-12-20 00:43:25 +00:00
|
|
|
"tailscale.com/net/packet"
|
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
|
|
|
|
|
|
|
func (ms matches) match(q *packet.Parsed) bool {
|
|
|
|
for _, m := range ms {
|
2023-10-11 21:00:50 +00:00
|
|
|
if !slices.Contains(m.IPProto, q.IPProto) {
|
2021-03-17 21:24:32 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-06-16 01:20:17 +00:00
|
|
|
if !m.SrcsContains(q.Src.Addr()) {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms matches) matchIPsOnly(q *packet.Parsed) bool {
|
|
|
|
for _, m := range ms {
|
2024-06-16 01:20:17 +00:00
|
|
|
if !m.SrcsContains(q.Src.Addr()) {
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2023-10-11 21:00:50 +00:00
|
|
|
if !slices.Contains(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
|
|
|
|
}
|