match.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package filter
  4. import (
  5. "net/netip"
  6. "tailscale.com/net/packet"
  7. "tailscale.com/tailcfg"
  8. "tailscale.com/types/views"
  9. "tailscale.com/wgengine/filter/filtertype"
  10. )
  11. type matches []filtertype.Match
  12. func (ms matches) match(q *packet.Parsed, hasCap CapTestFunc) bool {
  13. for i := range ms {
  14. m := &ms[i]
  15. if !views.SliceContains(m.IPProto, q.IPProto) {
  16. continue
  17. }
  18. if !srcMatches(m, q.Src.Addr(), hasCap) {
  19. continue
  20. }
  21. for _, dst := range m.Dsts {
  22. if !dst.Net.Contains(q.Dst.Addr()) {
  23. continue
  24. }
  25. if !dst.Ports.Contains(q.Dst.Port()) {
  26. continue
  27. }
  28. return true
  29. }
  30. }
  31. return false
  32. }
  33. // srcMatches reports whether srcAddr matche the src requirements in m, either
  34. // by Srcs (using SrcsContains), or by the node having a capability listed
  35. // in SrcCaps using the provided hasCap function.
  36. func srcMatches(m *filtertype.Match, srcAddr netip.Addr, hasCap CapTestFunc) bool {
  37. if m.SrcsContains(srcAddr) {
  38. return true
  39. }
  40. if hasCap != nil {
  41. for _, c := range m.SrcCaps {
  42. if hasCap(srcAddr, c) {
  43. return true
  44. }
  45. }
  46. }
  47. return false
  48. }
  49. // CapTestFunc is the function signature of a function that tests whether srcIP
  50. // has a given capability.
  51. //
  52. // It it used in the fast path of evaluating filter rules so should be fast.
  53. type CapTestFunc = func(srcIP netip.Addr, cap tailcfg.NodeCapability) bool
  54. func (ms matches) matchIPsOnly(q *packet.Parsed, hasCap CapTestFunc) bool {
  55. srcAddr := q.Src.Addr()
  56. for _, m := range ms {
  57. if !m.SrcsContains(srcAddr) {
  58. continue
  59. }
  60. for _, dst := range m.Dsts {
  61. if dst.Net.Contains(q.Dst.Addr()) {
  62. return true
  63. }
  64. }
  65. }
  66. if hasCap != nil {
  67. for _, m := range ms {
  68. for _, c := range m.SrcCaps {
  69. if hasCap(srcAddr, c) {
  70. return true
  71. }
  72. }
  73. }
  74. }
  75. return false
  76. }
  77. // matchProtoAndIPsOnlyIfAllPorts reports q matches any Match in ms where the
  78. // Match if for the right IP Protocol and IP address, but ports are
  79. // ignored, as long as the match is for the entire uint16 port range.
  80. func (ms matches) matchProtoAndIPsOnlyIfAllPorts(q *packet.Parsed) bool {
  81. for _, m := range ms {
  82. if !views.SliceContains(m.IPProto, q.IPProto) {
  83. continue
  84. }
  85. if !m.SrcsContains(q.Src.Addr()) {
  86. continue
  87. }
  88. for _, dst := range m.Dsts {
  89. if dst.Ports != filtertype.AllPorts {
  90. continue
  91. }
  92. if dst.Net.Contains(q.Dst.Addr()) {
  93. return true
  94. }
  95. }
  96. }
  97. return false
  98. }