1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
//! Implements address policies, based on a series of accept/reject
//! rules.
use std::fmt::Display;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::str::FromStr;
use super::{PolicyError, PortRange};
/// A sequence of rules that are applied to an address:port until one
/// matches.
///
/// Each rule is of the form "accept PATTERN" or "reject PATTERN",
/// where every pattern describes a set of addresses and ports.
/// Address sets are given as a prefix of 0-128 bits that the address
/// must have; port sets are given as a low-bound and high-bound that
/// the target port might lie between.
///
/// Relays use this type for defining their own policies, and for
/// publishing their IPv4 policies. Clients instead use
/// [super::portpolicy::PortPolicy] objects to view a summary of the
/// relays' declared policies.
///
/// An example IPv4 policy might be:
///
/// ```ignore
/// reject *:25
/// reject 127.0.0.0/8:*
/// reject 192.168.0.0/16:*
/// accept *:80
/// accept *:443
/// accept *:9000-65535
/// reject *:*
/// ```
#[derive(Clone, Debug, Default)]
pub struct AddrPolicy {
/// A list of rules to apply to find out whether an address is
/// contained by this policy.
///
/// The rules apply in order; the first one to match determines
/// whether the address is accepted or rejected.
rules: Vec<AddrPolicyRule>,
}
/// A kind of policy rule: either accepts or rejects addresses
/// matching a pattern.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[allow(clippy::exhaustive_enums)]
pub enum RuleKind {
/// A rule that accepts matching address:port combinations.
Accept,
/// A rule that rejects matching address:port combinations.
Reject,
}
impl AddrPolicy {
/// Apply this policy to an address:port combination
///
/// We do this by applying each rule in sequence, until one
/// matches.
///
/// Returns None if no rule matches.
pub fn allows(&self, addr: &IpAddr, port: u16) -> Option<RuleKind> {
self.rules
.iter()
.find(|rule| rule.pattern.matches(addr, port))
.map(|AddrPolicyRule { kind, .. }| *kind)
}
/// As allows, but accept a SocketAddr.
pub fn allows_sockaddr(&self, addr: &SocketAddr) -> Option<RuleKind> {
self.allows(&addr.ip(), addr.port())
}
/// Create a new AddrPolicy that matches nothing.
pub fn new() -> Self {
AddrPolicy::default()
}
/// Add a new rule to this policy.
///
/// The newly added rule is applied _after_ all previous rules.
/// It matches all addresses and ports covered by AddrPortPattern.
///
/// If accept is true, the rule is to accept addresses that match;
/// if accept is false, the rule rejects such addresses.
pub fn push(&mut self, kind: RuleKind, pattern: AddrPortPattern) {
self.rules.push(AddrPolicyRule { kind, pattern });
}
}
/// A single rule in an address policy.
///
/// Contains a pattern and what to do with things that match it.
#[derive(Clone, Debug)]
struct AddrPolicyRule {
/// What do we do with items that match the pattern?
kind: RuleKind,
/// What pattern are we trying to match?
pattern: AddrPortPattern,
}
/*
impl Display for AddrPolicyRule {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let cmd = match self.kind {
RuleKind::Accept => "accept",
RuleKind::Reject => "reject",
};
write!(f, "{} {}", cmd, self.pattern)
}
}
*/
/// A pattern that may or may not match an address and port.
///
/// Each AddrPortPattern has an IP pattern, which matches a set of
/// addresses by prefix, and a port pattern, which matches a range of
/// ports.
///
/// # Example
///
/// ```
/// use tor_netdoc::types::policy::AddrPortPattern;
/// use std::net::{IpAddr,Ipv4Addr};
/// let localhost = IpAddr::V4(Ipv4Addr::new(127,3,4,5));
/// let not_localhost = IpAddr::V4(Ipv4Addr::new(192,0,2,16));
/// let pat: AddrPortPattern = "127.0.0.0/8:*".parse().unwrap();
///
/// assert!(pat.matches(&localhost, 22));
/// assert!(! pat.matches(¬_localhost, 22));
/// ```
#[derive(
Clone, Debug, Eq, PartialEq, serde_with::SerializeDisplay, serde_with::DeserializeFromStr,
)]
pub struct AddrPortPattern {
/// A pattern to match somewhere between zero and all IP addresses.
pattern: IpPattern,
/// A pattern to match a range of ports.
ports: PortRange,
}
impl AddrPortPattern {
/// Return an AddrPortPattern matching all targets.
pub fn new_all() -> Self {
Self {
pattern: IpPattern::Star,
ports: PortRange::new_all(),
}
}
/// Return true iff this pattern matches a given address and port.
pub fn matches(&self, addr: &IpAddr, port: u16) -> bool {
self.pattern.matches(addr) && self.ports.contains(port)
}
/// As matches, but accept a SocketAddr.
pub fn matches_sockaddr(&self, addr: &SocketAddr) -> bool {
self.matches(&addr.ip(), addr.port())
}
}
impl Display for AddrPortPattern {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.ports.is_all() {
write!(f, "{}:*", self.pattern)
} else {
write!(f, "{}:{}", self.pattern, self.ports)
}
}
}
impl FromStr for AddrPortPattern {
type Err = PolicyError;
fn from_str(s: &str) -> Result<Self, PolicyError> {
let last_colon = s.rfind(':').ok_or(PolicyError::InvalidPolicy)?;
let pattern: IpPattern = s[..last_colon].parse()?;
let ports_s = &s[last_colon + 1..];
let ports: PortRange = if ports_s == "*" {
PortRange::new_all()
} else {
ports_s.parse()?
};
Ok(AddrPortPattern { pattern, ports })
}
}
/// A pattern that matches one or more IP addresses.
//
// TODO(nickm): At present there is no way for Display or FromStr to distinguish
// V4Star, V6Star, and Star. If we decide it's important to have a syntax for
// "all IPv4 addresses" that isn't "0.0.0.0/0", we'll need to revisit that.
// At present, C tor allows '*', '*4', and '*6'.
#[derive(Clone, Debug, Eq, PartialEq)]
enum IpPattern {
/// Match all addresses.
Star,
/// Match all IPv4 addresses.
V4Star,
/// Match all IPv6 addresses.
V6Star,
/// Match all IPv4 addresses beginning with a given prefix.
V4(Ipv4Addr, u8),
/// Match all IPv6 addresses beginning with a given prefix.
V6(Ipv6Addr, u8),
}
impl IpPattern {
/// Construct an IpPattern that matches the first `mask` bits of `addr`.
fn from_addr_and_mask(addr: IpAddr, mask: u8) -> Result<Self, PolicyError> {
match (addr, mask) {
(IpAddr::V4(_), 0) => Ok(IpPattern::V4Star),
(IpAddr::V6(_), 0) => Ok(IpPattern::V6Star),
(IpAddr::V4(a), m) if m <= 32 => Ok(IpPattern::V4(a, m)),
(IpAddr::V6(a), m) if m <= 128 => Ok(IpPattern::V6(a, m)),
(_, _) => Err(PolicyError::InvalidMask),
}
}
/// Return true iff `addr` is matched by this pattern.
fn matches(&self, addr: &IpAddr) -> bool {
match (self, addr) {
(IpPattern::Star, _) => true,
(IpPattern::V4Star, IpAddr::V4(_)) => true,
(IpPattern::V6Star, IpAddr::V6(_)) => true,
(IpPattern::V4(pat, mask), IpAddr::V4(addr)) => {
let p1 = u32::from_be_bytes(pat.octets());
let p2 = u32::from_be_bytes(addr.octets());
let shift = 32 - mask;
(p1 >> shift) == (p2 >> shift)
}
(IpPattern::V6(pat, mask), IpAddr::V6(addr)) => {
let p1 = u128::from_be_bytes(pat.octets());
let p2 = u128::from_be_bytes(addr.octets());
let shift = 128 - mask;
(p1 >> shift) == (p2 >> shift)
}
(_, _) => false,
}
}
}
impl Display for IpPattern {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use IpPattern::*;
match self {
Star | V4Star | V6Star => write!(f, "*"),
V4(a, 32) => write!(f, "{}", a),
V4(a, m) => write!(f, "{}/{}", a, m),
V6(a, 128) => write!(f, "[{}]", a),
V6(a, m) => write!(f, "[{}]/{}", a, m),
}
}
}
/// Helper: try to parse a plain ipv4 address, or an IPv6 address
/// wrapped in brackets.
fn parse_addr(mut s: &str) -> Result<IpAddr, PolicyError> {
let bracketed = s.starts_with('[') && s.ends_with(']');
if bracketed {
s = &s[1..s.len() - 1];
}
let addr: IpAddr = s.parse().map_err(|_| PolicyError::InvalidAddress)?;
if addr.is_ipv6() != bracketed {
return Err(PolicyError::InvalidAddress);
}
Ok(addr)
}
impl FromStr for IpPattern {
type Err = PolicyError;
fn from_str(s: &str) -> Result<Self, PolicyError> {
let (ip_s, mask_s) = match s.find('/') {
Some(slash_idx) => (&s[..slash_idx], Some(&s[slash_idx + 1..])),
None => (s, None),
};
match (ip_s, mask_s) {
("*", Some(_)) => Err(PolicyError::MaskWithStar),
("*", None) => Ok(IpPattern::Star),
(s, Some(m)) => {
let a: IpAddr = parse_addr(s)?;
let m: u8 = m.parse().map_err(|_| PolicyError::InvalidMask)?;
IpPattern::from_addr_and_mask(a, m)
}
(s, None) => {
let a: IpAddr = parse_addr(s)?;
let m = if a.is_ipv4() { 32 } else { 128 };
IpPattern::from_addr_and_mask(a, m)
}
}
}
}
#[cfg(test)]
mod test {
// @@ begin test lint list maintained by maint/add_warning @@
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::mixed_attributes_style)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::unchecked_duration_subtraction)]
#![allow(clippy::useless_vec)]
#![allow(clippy::needless_pass_by_value)]
//! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
use super::*;
#[test]
fn test_roundtrip_rules() {
fn check(inp: &str, outp: &str) {
let policy = inp.parse::<AddrPortPattern>().unwrap();
assert_eq!(format!("{}", policy), outp);
}
check("127.0.0.2/32:77-10000", "127.0.0.2:77-10000");
check("127.0.0.2/32:*", "127.0.0.2:*");
check("127.0.0.0/16:9-100", "127.0.0.0/16:9-100");
check("127.0.0.0/0:443", "*:443");
check("*:443", "*:443");
check("[::1]:443", "[::1]:443");
check("[ffaa::]/16:80", "[ffaa::]/16:80");
check("[ffaa::77]/128:80", "[ffaa::77]:80");
}
#[test]
fn test_bad_rules() {
fn check(s: &str) {
assert!(s.parse::<AddrPortPattern>().is_err());
}
check("marzipan:80");
check("1.2.3.4:90-80");
check("1.2.3.4/100:8888");
check("[1.2.3.4]/16:80");
check("[::1]/130:8888");
}
#[test]
fn test_rule_matches() {
fn check(addr: &str, yes: &[&str], no: &[&str]) {
use std::net::SocketAddr;
let policy = addr.parse::<AddrPortPattern>().unwrap();
for s in yes {
let sa = s.parse::<SocketAddr>().unwrap();
assert!(policy.matches_sockaddr(&sa));
}
for s in no {
let sa = s.parse::<SocketAddr>().unwrap();
assert!(!policy.matches_sockaddr(&sa));
}
}
check(
"1.2.3.4/16:80",
&["1.2.3.4:80", "1.2.44.55:80"],
&["9.9.9.9:80", "1.3.3.4:80", "1.2.3.4:81"],
);
check(
"*:443-8000",
&["1.2.3.4:443", "[::1]:500"],
&["9.0.0.0:80", "[::1]:80"],
);
check(
"[face::]/8:80",
&["[fab0::7]:80"],
&["[dd00::]:80", "[face::7]:443"],
);
check("0.0.0.0/0:*", &["127.0.0.1:80"], &["[f00b::]:80"]);
check("[::]/0:*", &["[f00b::]:80"], &["127.0.0.1:80"]);
}
#[test]
fn test_policy_matches() -> Result<(), PolicyError> {
let mut policy = AddrPolicy::default();
policy.push(RuleKind::Accept, "*:443".parse()?);
policy.push(RuleKind::Accept, "[::1]:80".parse()?);
policy.push(RuleKind::Reject, "*:80".parse()?);
let policy = policy; // drop mut
assert_eq!(
policy.allows_sockaddr(&"[::6]:443".parse().unwrap()),
Some(RuleKind::Accept)
);
assert_eq!(
policy.allows_sockaddr(&"127.0.0.1:443".parse().unwrap()),
Some(RuleKind::Accept)
);
assert_eq!(
policy.allows_sockaddr(&"[::1]:80".parse().unwrap()),
Some(RuleKind::Accept)
);
assert_eq!(
policy.allows_sockaddr(&"[::2]:80".parse().unwrap()),
Some(RuleKind::Reject)
);
assert_eq!(
policy.allows_sockaddr(&"127.0.0.1:80".parse().unwrap()),
Some(RuleKind::Reject)
);
assert_eq!(
policy.allows_sockaddr(&"127.0.0.1:66".parse().unwrap()),
None
);
Ok(())
}
#[test]
fn serde() {
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
struct X {
p1: AddrPortPattern,
p2: AddrPortPattern,
}
let x = X {
p1: "127.0.0.1/8:9-10".parse().unwrap(),
p2: "*:80".parse().unwrap(),
};
let encoded = serde_json::to_string(&x).unwrap();
let expected = r#"{"p1":"127.0.0.1/8:9-10","p2":"*:80"}"#;
let x2: X = serde_json::from_str(&encoded).unwrap();
let x3: X = serde_json::from_str(expected).unwrap();
assert_eq!(&x2, &x3);
assert_eq!(&x2, &x);
}
}