Skip to main content

packet_formats/
ip.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! IP protocol types.
6
7// TODO(https://fxbug.dev/326330182): this import seems actually necessary. Is this a bug on the
8// lint?
9#[allow(unused_imports)]
10use alloc::vec::Vec;
11use core::cmp::PartialEq;
12use core::convert::Infallible as Never;
13use core::fmt::{Debug, Display};
14use core::hash::Hash;
15use core::marker::PhantomData;
16
17use net_types::ip::{GenericOverIp, Ip, IpAddr, Ipv4, Ipv4Addr, Ipv6, Ipv6Addr};
18use packet::{
19    BufferViewMut, NoOpSerializationContext, PacketBuilder, ParsablePacket, ParseMetadata,
20    PartialPacketBuilder, SerializationContext,
21};
22use zerocopy::{
23    FromBytes, Immutable, IntoBytes, KnownLayout, SplitByteSlice, SplitByteSliceMut, Unaligned,
24};
25
26use crate::error::{IpParseResult, Ipv6ParseError, ParseError};
27use crate::ethernet::EthernetIpExt;
28use crate::icmp::IcmpIpExt;
29use crate::ipv4::{IPV4_MIN_HDR_LEN, Ipv4Header, Ipv4Packet, Ipv4PacketBuilder, Ipv4PacketRaw};
30use crate::ipv6::{IPV6_FIXED_HDR_LEN, Ipv6Header, Ipv6Packet, Ipv6PacketBuilder, Ipv6PacketRaw};
31use crate::private::Sealed;
32
33/// An [`Ip`] extension trait adding an associated type for the IP protocol
34/// number.
35pub trait IpProtoExt: Ip {
36    /// The type representing an IPv4 or IPv6 protocol number.
37    ///
38    /// For IPv4, this is [`Ipv4Proto`], and for IPv6, this is [`Ipv6Proto`].
39    type Proto: IpProtocol
40        + GenericOverIp<Self, Type = Self::Proto>
41        + GenericOverIp<Ipv4, Type = Ipv4Proto>
42        + GenericOverIp<Ipv6, Type = Ipv6Proto>
43        + Copy
44        + Clone
45        + Hash
46        + Debug
47        + Display
48        + PartialEq
49        + Eq
50        + PartialOrd
51        + Ord;
52}
53
54impl IpProtoExt for Ipv4 {
55    type Proto = Ipv4Proto;
56}
57
58impl IpProtoExt for Ipv6 {
59    type Proto = Ipv6Proto;
60}
61
62/// IP packet context relevant to serialization.
63pub struct IpEnvelope<I: IpExt> {
64    /// True if the IP packet has options (IPv4) or extension headers (IPv6).
65    pub has_options: bool,
66    _marker: PhantomData<I>,
67}
68
69impl<I: IpExt> IpEnvelope<I> {
70    /// Creates a new `IpEnvelope`.
71    pub fn new(has_options: bool) -> Self {
72        Self { has_options, _marker: PhantomData }
73    }
74}
75
76/// A trait for IP serialization contexts.
77pub trait IpSerializationContext<I: IpExt>: SerializationContext {
78    /// Converts an `IpEnvelope` into the serialization context's state.
79    fn envelope_to_state(envelope: IpEnvelope<I>) -> Self::ContextState;
80}
81
82impl<I: IpExt> IpSerializationContext<I> for NoOpSerializationContext {
83    fn envelope_to_state(_envelope: IpEnvelope<I>) -> Self::ContextState {
84        ()
85    }
86}
87
88/// An extension trait to the `Ip` trait adding associated types relevant for
89/// packet parsing and serialization.
90pub trait IpExt: EthernetIpExt + IcmpIpExt {
91    /// The error type returned when parsing a packet of this IP version fails.
92    type PacketParseError: From<ParseError> + Debug + PartialEq + Send + Sync;
93
94    /// An IP packet type for this IP version.
95    type Packet<B: SplitByteSlice>: IpPacket<B, Self>
96        + GenericOverIp<Self, Type = Self::Packet<B>>
97        + GenericOverIp<Ipv4, Type = Ipv4Packet<B>>
98        + GenericOverIp<Ipv6, Type = Ipv6Packet<B>>;
99    /// A raw IP packet type for this IP version.
100    type PacketRaw<B: SplitByteSlice>: IpPacketRaw<B, Self>
101        + GenericOverIp<Self, Type = Self::PacketRaw<B>>
102        + GenericOverIp<Ipv4, Type = Ipv4PacketRaw<B>>
103        + GenericOverIp<Ipv6, Type = Ipv6PacketRaw<B>>;
104    /// An IP packet builder type for the IP version.
105    type PacketBuilder<C: IpSerializationContext<Self>>: IpPacketBuilder<C, Self> + Eq;
106    /// Minimal IP header size.
107    const MIN_HEADER_LENGTH: usize;
108}
109
110impl IpExt for Ipv4 {
111    type PacketParseError = ParseError;
112    type Packet<B: SplitByteSlice> = Ipv4Packet<B>;
113    type PacketRaw<B: SplitByteSlice> = Ipv4PacketRaw<B>;
114    type PacketBuilder<C: IpSerializationContext<Self>> = Ipv4PacketBuilder;
115
116    const MIN_HEADER_LENGTH: usize = IPV4_MIN_HDR_LEN;
117}
118
119impl IpExt for Ipv6 {
120    type PacketParseError = Ipv6ParseError;
121    type Packet<B: SplitByteSlice> = Ipv6Packet<B>;
122    type PacketRaw<B: SplitByteSlice> = Ipv6PacketRaw<B>;
123    type PacketBuilder<C: IpSerializationContext<Self>> = Ipv6PacketBuilder;
124
125    const MIN_HEADER_LENGTH: usize = IPV6_FIXED_HDR_LEN;
126}
127
128/// An error encountered during NAT64 translation.
129#[derive(Debug)]
130pub enum Nat64Error {
131    /// Support not yet implemented in the library.
132    NotImplemented,
133}
134
135/// The result of NAT64 translation.
136#[derive(Debug)]
137pub enum Nat64TranslationResult<S, E> {
138    /// Forward the packet encoded in `S`.
139    Forward(S),
140    /// Silently drop the packet.
141    Drop,
142    /// An error was encountered.
143    Err(E),
144}
145
146/// Combines Differentiated Services Code Point (DSCP) and Explicit Congestion
147/// Notification (ECN) values into one. Internally the 2 fields are stored
148/// using the same layout as the Traffic Class field in IPv6 and the Type Of
149/// Service field in IPv4: 6 higher bits for DSCP and 2 lower bits for ECN.
150#[derive(
151    Default,
152    Debug,
153    Clone,
154    Copy,
155    PartialEq,
156    Eq,
157    KnownLayout,
158    FromBytes,
159    IntoBytes,
160    Immutable,
161    Unaligned,
162)]
163#[repr(C)]
164pub struct DscpAndEcn(u8);
165
166const DSCP_OFFSET: u8 = 2;
167const DSCP_MAX: u8 = (1 << (8 - DSCP_OFFSET)) - 1;
168const ECN_MAX: u8 = (1 << DSCP_OFFSET) - 1;
169
170impl DscpAndEcn {
171    /// Returns the default value. Implemented separately from the `Default`
172    /// trait to make it `const`.
173    pub const fn default() -> Self {
174        Self(0)
175    }
176
177    /// Creates a new `DscpAndEcn` instance with the specified DSCP and ECN
178    /// values.
179    pub const fn new(dscp: u8, ecn: u8) -> Self {
180        debug_assert!(dscp <= DSCP_MAX);
181        debug_assert!(ecn <= ECN_MAX);
182        Self((dscp << DSCP_OFFSET) + ecn)
183    }
184
185    /// Constructs a new `DspAndEcn` from a raw value, i.e., both fields packet
186    /// into one byte.
187    pub const fn new_with_raw(value: u8) -> Self {
188        Self(value)
189    }
190
191    /// Returns the Differentiated Services Code Point value.
192    pub fn dscp(self) -> u8 {
193        let Self(v) = self;
194        v >> 2
195    }
196
197    /// Returns the Explicit Congestion Notification value.
198    pub fn ecn(self) -> u8 {
199        let Self(v) = self;
200        v & 0x3
201    }
202
203    /// Returns the raw value, i.e. both fields packed into one byte.
204    pub fn raw(self) -> u8 {
205        let Self(value) = self;
206        value
207    }
208}
209
210impl From<u8> for DscpAndEcn {
211    fn from(value: u8) -> Self {
212        Self::new_with_raw(value)
213    }
214}
215
216/// An IPv4 or IPv6 packet.
217///
218/// `IpPacket` is implemented by `Ipv4Packet` and `Ipv6Packet`.
219pub trait IpPacket<B: SplitByteSlice, I: IpExt>:
220    Sized + Debug + ParsablePacket<B, (), Error = I::PacketParseError>
221{
222    /// A builder for this packet type.
223    type Builder<C: IpSerializationContext<I>>: IpPacketBuilder<C, I>;
224
225    /// The source IP address.
226    fn src_ip(&self) -> I::Addr;
227
228    /// The destination IP address.
229    fn dst_ip(&self) -> I::Addr;
230
231    /// The protocol number.
232    fn proto(&self) -> I::Proto;
233
234    /// The Time to Live (TTL) (IPv4) or Hop Limit (IPv6) field.
235    fn ttl(&self) -> u8;
236
237    /// The Differentiated Services Code Point (DSCP) and the Explicit
238    /// Congestion Notification (ECN).
239    fn dscp_and_ecn(&self) -> DscpAndEcn;
240
241    /// Set the Time to Live (TTL) (IPv4) or Hop Limit (IPv6) field.
242    ///
243    /// `set_ttl` updates the packet's TTL/Hop Limit in place.
244    fn set_ttl(&mut self, ttl: u8)
245    where
246        B: SplitByteSliceMut;
247
248    /// Get the body.
249    fn body(&self) -> &[u8];
250
251    /// Consume the packet and return some metadata.
252    ///
253    /// Consume the packet and return the source address, destination address,
254    /// protocol, and `ParseMetadata`.
255    fn into_metadata(self) -> (I::Addr, I::Addr, I::Proto, ParseMetadata) {
256        let src_ip = self.src_ip();
257        let dst_ip = self.dst_ip();
258        let proto = self.proto();
259        let meta = self.parse_metadata();
260        (src_ip, dst_ip, proto, meta)
261    }
262
263    /// Converts a packet reference into a dynamically-typed reference.
264    fn as_ip_addr_ref(&self) -> IpAddr<&'_ Ipv4Packet<B>, &'_ Ipv6Packet<B>>;
265
266    /// Reassembles a fragmented packet into a parsed IP packet.
267    ///
268    /// # Panics
269    ///
270    /// Panics if the provided header is too small to hold a valid header.
271    fn reassemble_fragmented_packet<BV: BufferViewMut<B>, IT: Iterator<Item = Vec<u8>>>(
272        buffer: BV,
273        header: Vec<u8>,
274        body_fragments: IT,
275    ) -> IpParseResult<I, ()>
276    where
277        B: SplitByteSliceMut;
278
279    /// Copies the full packet into a `Vec`.
280    fn to_vec(&self) -> Vec<u8>;
281
282    /// Constructs a builder with the same contents as this packet's header.
283    fn builder<C: IpSerializationContext<I>>(&self) -> Self::Builder<C>;
284}
285
286impl<B: SplitByteSlice> IpPacket<B, Ipv4> for Ipv4Packet<B> {
287    type Builder<C: IpSerializationContext<Ipv4>> = Ipv4PacketBuilder;
288
289    fn src_ip(&self) -> Ipv4Addr {
290        Ipv4Header::src_ip(self)
291    }
292    fn dst_ip(&self) -> Ipv4Addr {
293        Ipv4Header::dst_ip(self)
294    }
295    fn proto(&self) -> Ipv4Proto {
296        Ipv4Header::proto(self)
297    }
298    fn dscp_and_ecn(&self) -> DscpAndEcn {
299        Ipv4Header::dscp_and_ecn(self)
300    }
301    fn ttl(&self) -> u8 {
302        Ipv4Header::ttl(self)
303    }
304    fn set_ttl(&mut self, ttl: u8)
305    where
306        B: SplitByteSliceMut,
307    {
308        Ipv4Packet::set_ttl(self, ttl)
309    }
310    fn body(&self) -> &[u8] {
311        Ipv4Packet::body(self)
312    }
313
314    fn as_ip_addr_ref(&self) -> IpAddr<&'_ Self, &'_ Ipv6Packet<B>> {
315        IpAddr::V4(self)
316    }
317
318    fn reassemble_fragmented_packet<BV: BufferViewMut<B>, IT: Iterator<Item = Vec<u8>>>(
319        buffer: BV,
320        header: Vec<u8>,
321        body_fragments: IT,
322    ) -> IpParseResult<Ipv4, ()>
323    where
324        B: SplitByteSliceMut,
325    {
326        crate::ipv4::reassemble_fragmented_packet(buffer, header, body_fragments)
327    }
328
329    fn to_vec(&self) -> Vec<u8> {
330        self.to_vec()
331    }
332
333    fn builder<C: IpSerializationContext<Ipv4>>(&self) -> Self::Builder<C> {
334        Ipv4Header::builder(self)
335    }
336}
337
338impl<B: SplitByteSlice> IpPacket<B, Ipv6> for Ipv6Packet<B> {
339    type Builder<C: IpSerializationContext<Ipv6>> = Ipv6PacketBuilder;
340
341    fn src_ip(&self) -> Ipv6Addr {
342        Ipv6Header::src_ip(self)
343    }
344    fn dst_ip(&self) -> Ipv6Addr {
345        Ipv6Header::dst_ip(self)
346    }
347    fn proto(&self) -> Ipv6Proto {
348        Ipv6Packet::proto(self)
349    }
350    fn dscp_and_ecn(&self) -> DscpAndEcn {
351        Ipv6Header::dscp_and_ecn(self)
352    }
353    fn ttl(&self) -> u8 {
354        Ipv6Header::hop_limit(self)
355    }
356    fn set_ttl(&mut self, ttl: u8)
357    where
358        B: SplitByteSliceMut,
359    {
360        Ipv6Packet::set_hop_limit(self, ttl)
361    }
362    fn body(&self) -> &[u8] {
363        Ipv6Packet::body(self)
364    }
365
366    fn as_ip_addr_ref(&self) -> IpAddr<&'_ Ipv4Packet<B>, &'_ Self> {
367        IpAddr::V6(self)
368    }
369    fn reassemble_fragmented_packet<BV: BufferViewMut<B>, IT: Iterator<Item = Vec<u8>>>(
370        buffer: BV,
371        header: Vec<u8>,
372        body_fragments: IT,
373    ) -> IpParseResult<Ipv6, ()>
374    where
375        B: SplitByteSliceMut,
376    {
377        crate::ipv6::reassemble_fragmented_packet(buffer, header, body_fragments)
378    }
379
380    fn to_vec(&self) -> Vec<u8> {
381        self.to_vec()
382    }
383
384    fn builder<C: IpSerializationContext<Ipv6>>(&self) -> Self::Builder<C> {
385        self.builder()
386    }
387}
388
389/// A raw IPv4 or IPv6 packet.
390///
391/// `IpPacketRaw` is implemented by `Ipv4PacketRaw` and `Ipv6PacketRaw`.
392pub trait IpPacketRaw<B: SplitByteSlice, I: IpExt>:
393    Sized + ParsablePacket<B, (), Error = I::PacketParseError>
394{
395}
396
397impl<B: SplitByteSlice> IpPacketRaw<B, Ipv4> for Ipv4PacketRaw<B> {}
398impl<B: SplitByteSlice, I: IpExt> GenericOverIp<I> for Ipv4PacketRaw<B> {
399    type Type = <I as IpExt>::PacketRaw<B>;
400}
401
402impl<B: SplitByteSlice> IpPacketRaw<B, Ipv6> for Ipv6PacketRaw<B> {}
403impl<B: SplitByteSlice, I: IpExt> GenericOverIp<I> for Ipv6PacketRaw<B> {
404    type Type = <I as IpExt>::PacketRaw<B>;
405}
406
407/// A builder for IP packets.
408pub trait IpPacketBuilder<C: SerializationContext, I: IpExt>:
409    PacketBuilder<C> + PartialPacketBuilder<C> + Clone + Debug
410{
411    /// Returns a new packet builder for an associated IP version with the given
412    /// given source and destination IP addresses, TTL (IPv4)/Hop Limit (IPv4)
413    /// and Protocol Number.
414    fn new(src_ip: I::Addr, dst_ip: I::Addr, ttl: u8, proto: I::Proto) -> Self;
415
416    /// Returns the source IP address for the builder.
417    fn src_ip(&self) -> I::Addr;
418
419    /// Sets the source IP address for the builder.
420    fn set_src_ip(&mut self, addr: I::Addr);
421
422    /// Returns the destination IP address for the builder.
423    fn dst_ip(&self) -> I::Addr;
424
425    /// Sets the destination IP address for the builder.
426    fn set_dst_ip(&mut self, addr: I::Addr);
427
428    /// Returns the IP protocol number for the builder.
429    fn proto(&self) -> I::Proto;
430
431    /// Set DSCP & ECN fields.
432    fn set_dscp_and_ecn(&mut self, dscp_and_ecn: DscpAndEcn);
433}
434
435/// An IPv4 or IPv6 protocol number.
436pub trait IpProtocol: From<IpProto> + From<u8> + Sealed + Send + Sync + 'static {}
437
438impl Sealed for Never {}
439
440create_protocol_enum!(
441    /// An IPv4 or IPv6 protocol number.
442    ///
443    /// `IpProto` encodes the protocol numbers whose values are the same for
444    /// both IPv4 and IPv6.
445    ///
446    /// The protocol numbers are maintained [by IANA][protocol-numbers].
447    ///
448    /// [protocol-numbers]: https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
449    #[allow(missing_docs)]
450    #[derive(Copy, Clone, Hash, Eq, Ord, PartialEq, PartialOrd)]
451    pub enum IpProto: u8 {
452        Tcp, 6, "TCP";
453        Udp, 17, "UDP";
454        Reserved, 255, "IANA-RESERVED";
455    }
456);
457
458create_protocol_enum!(
459    /// An IPv4 protocol number.
460    ///
461    /// The protocol numbers are maintained [by IANA][protocol-numbers].
462    ///
463    /// [protocol-numbers]: https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
464    #[allow(missing_docs)]
465    #[derive(Copy, Clone, Hash, Eq, Ord, PartialEq, PartialOrd)]
466    pub enum Ipv4Proto: u8 {
467        Icmp, 1, "ICMP";
468        Igmp, 2, "IGMP";
469        + Proto(IpProto);
470        _, "IPv4 protocol {}";
471    }
472);
473
474impl IpProtocol for Ipv4Proto {}
475impl<I: Ip + IpProtoExt> GenericOverIp<I> for Ipv4Proto {
476    type Type = I::Proto;
477}
478impl Sealed for Ipv4Proto {}
479
480create_protocol_enum!(
481    /// An IPv6 protocol number.
482    ///
483    /// The protocol numbers are maintained [by IANA][protocol-numbers].
484    ///
485    /// [protocol-numbers]: https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
486    #[allow(missing_docs)]
487    #[derive(Copy, Clone, Hash, Eq, Ord, PartialEq, PartialOrd)]
488    pub enum Ipv6Proto: u8 {
489        Icmpv6, 58, "ICMPv6";
490        NoNextHeader, 59, "NO NEXT HEADER";
491        + Proto(IpProto);
492        _, "IPv6 protocol {}";
493    }
494);
495
496impl IpProtocol for Ipv6Proto {}
497impl<I: Ip + IpProtoExt> GenericOverIp<I> for Ipv6Proto {
498    type Type = I::Proto;
499}
500impl Sealed for Ipv6Proto {}
501
502create_protocol_enum!(
503    /// An IPv6 extension header.
504    ///
505    /// These are next header values that encode for extension header types.
506    /// This enum does not include upper layer protocol numbers even though they
507    /// may be valid next header values.
508    #[allow(missing_docs)]
509    #[derive(Copy, Clone, Hash, Eq, PartialEq)]
510    pub enum Ipv6ExtHdrType: u8 {
511        HopByHopOptions, 0, "IPv6 HOP-BY-HOP OPTIONS HEADER";
512        Routing, 43, "IPv6 ROUTING HEADER";
513        Fragment, 44, "IPv6 FRAGMENT HEADER";
514        EncapsulatingSecurityPayload, 50, "ENCAPSULATING SECURITY PAYLOAD";
515        Authentication, 51, "AUTHENTICATION HEADER";
516        DestinationOptions, 60, "IPv6 DESTINATION OPTIONS HEADER";
517        _,  "IPv6 EXTENSION HEADER {}";
518    }
519);
520
521/// An IP fragment offset.
522///
523/// Represents a fragment offset found in an IP header. The offset is expressed
524/// in units of 8 octets and must be smaller than `1 << 13`.
525///
526/// This is valid for both IPv4 ([RFC 791 Section 3.1]) and IPv6 ([RFC 8200
527/// Section 4.5]) headers.
528///
529/// [RFC 791 Section 3.1]: https://datatracker.ietf.org/doc/html/rfc791#section-3.1
530/// [RFC 8200 Section 4.5]: https://datatracker.ietf.org/doc/html/rfc8200#section-4.5
531#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Copy, Clone)]
532pub struct FragmentOffset(u16);
533
534impl FragmentOffset {
535    /// The zero fragment offset.
536    pub const ZERO: FragmentOffset = FragmentOffset(0);
537
538    /// Creates a new offset from a raw u16 value.
539    ///
540    /// Returns `None` if `offset` is not smaller than `1 << 13`.
541    pub const fn new(offset: u16) -> Option<Self> {
542        if offset < 1 << 13 { Some(Self(offset)) } else { None }
543    }
544
545    /// Creates a new offset from a raw u16 value masking to only the lowest 13
546    /// bits.
547    pub(crate) fn new_with_lsb(offset: u16) -> Self {
548        Self(offset & 0x1FFF)
549    }
550
551    /// Creates a new offset from a raw u16 value masking to only the highest 13
552    /// bits.
553    pub(crate) fn new_with_msb(offset: u16) -> Self {
554        Self(offset >> 3)
555    }
556
557    /// Creates a new offset from a raw bytes value.
558    ///
559    /// Returns `None` if `offset_bytes` is not a multiple of `8`.
560    pub const fn new_with_bytes(offset_bytes: u16) -> Option<Self> {
561        if offset_bytes & 0x7 == 0 {
562            // NOTE: check for length above ensures this fits in a u16.
563            Some(Self(offset_bytes >> 3))
564        } else {
565            None
566        }
567    }
568
569    /// Consumes `self` returning the raw offset value in 8-octets multiples.
570    pub const fn into_raw(self) -> u16 {
571        self.0
572    }
573
574    /// Consumes `self` returning the total number of bytes represented by this
575    /// offset.
576    ///
577    /// Equal to 8 times the raw offset value.
578    pub fn into_bytes(self) -> u16 {
579        // NB: Shift can't overflow because `FragmentOffset` is guaranteed to
580        // fit in 13 bits.
581        self.0 << 3
582    }
583}
584
585#[cfg(test)]
586mod tests {
587    use super::*;
588
589    #[test]
590    fn fragment_offset_raw() {
591        assert_eq!(FragmentOffset::new(1), Some(FragmentOffset(1)));
592        assert_eq!(FragmentOffset::new(1 << 13), None);
593    }
594
595    #[test]
596    fn fragment_offset_bytes() {
597        assert_eq!(FragmentOffset::new_with_bytes(0), Some(FragmentOffset(0)));
598        for i in 1..=7 {
599            assert_eq!(FragmentOffset::new_with_bytes(i), None);
600        }
601        assert_eq!(FragmentOffset::new_with_bytes(8), Some(FragmentOffset(1)));
602        assert_eq!(FragmentOffset::new_with_bytes(core::u16::MAX), None);
603        assert_eq!(
604            FragmentOffset::new_with_bytes(core::u16::MAX & !0x7),
605            Some(FragmentOffset((1 << 13) - 1)),
606        );
607    }
608}