domain/base/opt/
subnet.rs

1//! EDNS option for carrying client subnet information.
2//!
3//! The option in this module – [`ClientSubnet`] – can be used by a resolver
4//! to include information about the network a query originated from in its
5//! own query to an authoritative server so it can tailor its response for
6//! that network.
7//!
8//! The option is defined in [RFC 7871](https://tools.ietf.org/html/rfc7871)
9//! which also includes some guidance on its use.
10
11use core::fmt;
12use super::super::iana::OptionCode;
13use super::super::message_builder::OptBuilder;
14use super::super::net::IpAddr;
15use super::super::wire::{Compose, Composer, FormError, ParseError};
16use super::{Opt, OptData, ComposeOptData, ParseOptData};
17use octseq::builder::OctetsBuilder;
18use octseq::octets::Octets;
19use octseq::parse::Parser;
20
21//------------ ClientSubnet --------------------------------------------------
22
23/// Option data for the client subnet option.
24///
25/// This option allows a resolver to include information about the network a
26/// query originated from. This information can then be used by an
27/// authoritative server to provide the best response for this network.
28///
29/// The option identifies the network through an address prefix, i.e., an
30/// IP address of which only a certain number of left-side bits is
31/// interpreted. The option uses two such numbers: The _source prefix length_
32/// is the number of bits provided by the client when describing its network
33/// and the _scope prefix length_ is the number of bits that the server
34/// considered when providing the answer. The scope prefix length is zero
35/// in a query. It can be used by a caching resolver to cache multiple
36/// responses for different client subnets.
37///
38/// The option is defined in [RFC 7871](https://tools.ietf.org/html/rfc7871)
39/// which also includes some guidance on its use.
40#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
41pub struct ClientSubnet {
42    /// The source prefix length.
43    source_prefix_len: u8,
44
45    /// The scope prefix length.
46    scope_prefix_len: u8,
47
48    /// The address.
49    addr: IpAddr,
50}
51
52impl ClientSubnet {
53    /// Creates a new client subnet value.
54    ///
55    /// The function is very forgiving regarding the arguments and corrects
56    /// illegal values. That is, it limit the prefix lengths given to a number
57    /// meaningful for the address family. It will also set all bits not
58    /// covered by the source prefix length in the address to zero.
59    #[must_use]
60    pub fn new(
61        source_prefix_len: u8,
62        scope_prefix_len: u8,
63        addr: IpAddr,
64    ) -> ClientSubnet {
65        let source_prefix_len = normalize_prefix_len(addr, source_prefix_len);
66        let scope_prefix_len = normalize_prefix_len(addr, scope_prefix_len);
67        let (addr, _) = addr_apply_mask(addr, source_prefix_len);
68
69        ClientSubnet {
70            source_prefix_len,
71            scope_prefix_len,
72            addr,
73        }
74    }
75
76    /// Returns the source prefix length.
77    ///
78    /// The source prefix length is the prefix length as specified by the
79    /// client in a query.
80    #[must_use]
81    pub fn source_prefix_len(&self) -> u8 {
82        self.source_prefix_len
83    }
84
85    /// Returns the scope prefix length.
86    ///
87    /// The scope prefix length is the prefix length used by the server for
88    /// its answer.
89    #[must_use]
90    pub fn scope_prefix_len(&self) -> u8 {
91        self.scope_prefix_len
92    }
93
94    /// Returns the address.
95    #[must_use]
96    pub fn addr(&self) -> IpAddr {
97        self.addr
98    }
99
100    /// Parses a value from its wire format.
101    pub fn parse<Octs: AsRef<[u8]>>(
102        parser: &mut Parser<Octs>
103    ) -> Result<Self, ParseError> {
104        const ERR_ADDR_LEN: &str = "invalid address length in client \
105                                    subnet option";
106
107        let family = parser.parse_u16_be()?;
108        let source_prefix_len = parser.parse_u8()?;
109        let scope_prefix_len = parser.parse_u8()?;
110
111        // https://tools.ietf.org/html/rfc7871#section-6
112        //
113        // | ADDRESS, variable number of octets, contains either an IPv4 or
114        // | IPv6 address, depending on FAMILY, which MUST be truncated to
115        // | the number of bits indicated by the SOURCE PREFIX-LENGTH field,
116        // | padding with 0 bits to pad to the end of the last octet needed.
117        let prefix_bytes = prefix_bytes(source_prefix_len);
118
119        let addr = match family {
120            1 => {
121                let mut buf = [0; 4];
122                if prefix_bytes > buf.len() {
123                    return Err(ParseError::form_error(ERR_ADDR_LEN));
124                }
125                parser
126                    .parse_buf(&mut buf[..prefix_bytes])
127                    .map_err(|_| ParseError::form_error(ERR_ADDR_LEN))?;
128
129                if parser.remaining() != 0 {
130                    return Err(ParseError::form_error(ERR_ADDR_LEN));
131                }
132
133                IpAddr::from(buf)
134            }
135            2 => {
136                let mut buf = [0; 16];
137                if prefix_bytes > buf.len() {
138                    return Err(ParseError::form_error(ERR_ADDR_LEN));
139                }
140                parser
141                    .parse_buf(&mut buf[..prefix_bytes])
142                    .map_err(|_| ParseError::form_error(ERR_ADDR_LEN))?;
143
144                if parser.remaining() != 0 {
145                    return Err(ParseError::form_error(ERR_ADDR_LEN));
146                }
147
148                IpAddr::from(buf)
149            }
150            _ => {
151                return Err(FormError::new(
152                    "invalid client subnet address family",
153                )
154                .into())
155            }
156        };
157
158        // If the trailing bits beyond prefix length are not zero,
159        // return form error.
160        let (addr, modified) = addr_apply_mask(addr, source_prefix_len);
161        if modified {
162            return Err(ParseError::form_error(ERR_ADDR_LEN));
163        }
164
165        // no need to pass the normalizer in constructor again
166        Ok(ClientSubnet {
167            source_prefix_len,
168            scope_prefix_len,
169            addr,
170        })
171    }
172}
173
174//--- OptData
175
176impl OptData for ClientSubnet {
177    fn code(&self) -> OptionCode {
178        OptionCode::ClientSubnet
179    }
180}
181
182impl<'a, Octs: AsRef<[u8]>> ParseOptData<'a, Octs> for ClientSubnet {
183    fn parse_option(
184        code: OptionCode,
185        parser: &mut Parser<'a, Octs>,
186    ) -> Result<Option<Self>, ParseError> {
187        if code == OptionCode::ClientSubnet {
188            Self::parse(parser).map(Some)
189        }
190        else {
191            Ok(None)
192        }
193    }
194}
195
196impl ComposeOptData for ClientSubnet {
197    fn compose_len(&self) -> u16 {
198        u16::try_from(prefix_bytes(self.source_prefix_len)).unwrap() + 4
199    }
200
201    fn compose_option<Target: OctetsBuilder + ?Sized>(
202        &self, target: &mut Target
203    ) -> Result<(), Target::AppendError> {
204        let prefix_bytes = prefix_bytes(self.source_prefix_len);
205        match self.addr {
206            IpAddr::V4(addr) => {
207                1u16.compose(target)?;
208                self.source_prefix_len.compose(target)?;
209                self.scope_prefix_len.compose(target)?;
210                let array = addr.octets();
211                assert!(prefix_bytes <= array.len());
212                target.append_slice(&array[..prefix_bytes])
213            }
214            IpAddr::V6(addr) => {
215                2u16.compose(target)?;
216                self.source_prefix_len.compose(target)?;
217                self.scope_prefix_len.compose(target)?;
218                let array = addr.octets();
219                assert!(prefix_bytes <= array.len());
220                target.append_slice(&array[..prefix_bytes])
221            }
222        }
223    }
224}
225
226//--- Display
227
228impl fmt::Display for ClientSubnet {
229    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
230        match self.addr {
231            IpAddr::V4(a) => {
232                if self.scope_prefix_len != 0 {
233                    write!(f, "{}/{}/{}", a, self.source_prefix_len,
234                        self.scope_prefix_len)?;
235                } else {
236                    write!(f, "{}/{}", a, self.source_prefix_len)?;
237                }
238            }
239            IpAddr::V6(a) => {
240                if self.scope_prefix_len != 0 {
241                    write!(f, "{}/{}/{}", a, self.source_prefix_len,
242                        self.scope_prefix_len)?;
243                } else {
244                    write!(f, "{}/{}", a, self.source_prefix_len)?;
245                }
246            }
247        }
248
249        Ok(())
250    }
251}
252
253//--- Extended Opt and OptBuilder
254
255impl<Octs: Octets> Opt<Octs> {
256    /// Returns the first client subnet option if present.
257    ///
258    /// This option allows a resolver to include information about the
259    /// network a query originated from. This information can then be
260    /// used by an authoritative server to provide the best response for
261    /// this network.
262    pub fn client_subnet(&self) -> Option<ClientSubnet> {
263        self.first()
264    }
265}
266
267impl<'a, Target: Composer> OptBuilder<'a, Target> {
268    pub fn client_subnet(
269        &mut self,
270        source_prefix_len: u8,
271        scope_prefix_len: u8,
272        addr: IpAddr,
273    ) -> Result<(), Target::AppendError> {
274        self.push(
275            &ClientSubnet::new(source_prefix_len, scope_prefix_len, addr)
276        )
277    }
278}
279
280//------------ Helper Functions ----------------------------------------------
281
282/// Returns the number of bytes needed for a prefix of a given length
283fn prefix_bytes(bits: u8) -> usize {
284    (usize::from(bits) + 7) / 8
285}
286
287/// Only keeps the left-most `mask` bits and zeros out the rest.
288///
289/// Returns whether the buffer has been modified.
290fn apply_bit_mask(buf: &mut [u8], mask: usize) -> bool {
291    let mut modified = false;
292
293    // skip full bytes covered by prefix length
294    let mut p = mask / 8;
295    if p >= buf.len() {
296        return modified;
297    }
298
299    // clear extra bits in a byte
300    let bits = mask % 8;
301    if bits != 0 {
302        if buf[p].trailing_zeros() < (8 - bits) as u32 {
303            buf[p] &= 0xff << (8 - bits);
304            modified = true;
305        }
306        p += 1;
307    }
308
309    // clear the rest bytes
310    while p < buf.len() {
311        if buf[p] != 0 {
312            buf[p] = 0;
313            modified = true;
314        }
315        p += 1;
316    }
317
318    modified
319}
320
321/// Zeros out unused bits in a address prefix of the given length
322///
323/// Returns the new address and whether it was changed.
324fn addr_apply_mask(addr: IpAddr, len: u8) -> (IpAddr, bool) {
325    match addr {
326        IpAddr::V4(a) => {
327            let mut array = a.octets();
328            let m = apply_bit_mask(&mut array, len as usize);
329            (array.into(), m)
330        }
331        IpAddr::V6(a) => {
332            let mut array = a.octets();
333            let m = apply_bit_mask(&mut array, len as usize);
334            (array.into(), m)
335        }
336    }
337}
338
339/// Limits a prefix length for the given address.
340fn normalize_prefix_len(addr: IpAddr, len: u8) -> u8 {
341    let max = match addr {
342        IpAddr::V4(_) => 32,
343        IpAddr::V6(_) => 128,
344    };
345
346    core::cmp::min(len, max)
347}
348
349//============ Testing =======================================================
350
351#[cfg(all(test, feature="std", feature = "bytes"))]
352mod tests {
353    use super::*;
354    use super::super::test::test_option_compose_parse;
355    use octseq::builder::infallible;
356    use std::vec::Vec;
357    use core::str::FromStr;
358
359    macro_rules! check {
360        ($name:ident, $addr:expr, $prefix:expr, $exp:expr, $ok:expr) => {
361            #[test]
362            fn $name() {
363                let addr = $addr.parse().unwrap();
364                let opt = ClientSubnet::new($prefix, 0, addr);
365                assert_eq!(opt.addr(), $exp.parse::<IpAddr>().unwrap());
366
367                // Check parse by mangling the addr in option to
368                // generate maybe invalid buffer.
369                let mut opt_ = opt.clone();
370                opt_.addr = addr;
371                let mut buf = Vec::new();
372
373                infallible(opt_.compose_option(&mut buf));
374                match ClientSubnet::parse(&mut Parser::from_ref(&buf)) {
375                    Ok(v) => assert_eq!(opt, v),
376                    Err(_) => assert!(!$ok),
377                }
378            }
379        };
380    }
381
382    check!(prefix_at_boundary_v4, "192.0.2.0", 24, "192.0.2.0", true);
383    check!(prefix_at_boundary_v6, "2001:db8::", 32, "2001:db8::", true);
384    check!(prefix_no_truncation, "192.0.2.0", 23, "192.0.2.0", true);
385    check!(prefix_need_truncation, "192.0.2.0", 22, "192.0.0.0", false);
386    check!(prefix_min, "192.0.2.0", 0, "0.0.0.0", true);
387    check!(prefix_max, "192.0.2.0", 32, "192.0.2.0", true);
388    check!(prefix_too_long, "192.0.2.0", 100, "192.0.2.0", false);
389    
390    #[test]
391    #[allow(clippy::redundant_closure)] // lifetimes ...
392    fn client_subnet_compose_parse() {
393        test_option_compose_parse(
394            &ClientSubnet::new(4, 6, IpAddr::from_str("127.0.0.1").unwrap()),
395            |parser| ClientSubnet::parse(parser)
396        );
397    }
398}