Skip to main content

packet_formats/
lib.rs

1// Copyright 2018 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//! Serialization and deserialization of wire formats.
6//!
7//! This module provides efficient serialization and deserialization of the
8//! various wire formats used by this program. Where possible, it uses lifetimes
9//! and immutability to allow for safe zero-copy parsing.
10//!
11//! # Endianness
12//!
13//! All values exposed or consumed by this crate are in host byte order, so the
14//! caller does not need to worry about it. Any necessary conversions are
15//! performed under the hood.
16
17#![cfg_attr(not(test), no_std)]
18// TODO(joshlf): Move into debug_err! and debug_err_fn! definitions once
19// attributes are allowed on expressions
20// (https://github.com/rust-lang/rust/issues/15701).
21#![allow(clippy::blocks_in_conditions)]
22#![deny(missing_docs, unreachable_patterns)]
23
24extern crate alloc;
25
26/// Emit a debug message and return an error.
27///
28/// Invoke the `debug!` macro on all but the first argument. A call to
29/// `debug_err!(err, ...)` is an expression whose value is the expression `err`.
30macro_rules! debug_err {
31    ($err:expr, $($arg:tt)*) => (
32        // TODO(joshlf): Uncomment once attributes are allowed on expressions
33        // #[cfg_attr(feature = "cargo-clippy", allow(block_in_if_condition_stmt))]
34        {
35            use ::log::debug;
36            debug!($($arg)*);
37            $err
38        }
39    )
40}
41
42/// Create a closure which emits a debug message and returns an error.
43///
44/// Create a closure which, when called, invokes the `debug!` macro on all but
45/// the first argument, and returns the first argument.
46macro_rules! debug_err_fn {
47    ($err:expr, $($arg:tt)*) => (
48        // TODO(joshlf): Uncomment once attributes are allowed on expressions
49        // #[cfg_attr(feature = "cargo-clippy", allow(block_in_if_condition_stmt))]
50        || {
51            use ::log::debug;
52            debug!($($arg)*);
53            $err
54        }
55    )
56}
57
58#[macro_use]
59mod macros;
60pub mod arp;
61pub mod error;
62pub mod ethernet;
63pub mod gmp;
64pub mod icmp;
65pub mod igmp;
66pub mod ip;
67pub mod ipv4;
68pub mod ipv6;
69pub mod tcp;
70pub mod testdata;
71pub mod testutil;
72pub mod udp;
73pub mod utils;
74
75use core::num::TryFromIntError;
76
77use byteorder::{ByteOrder, NetworkEndian};
78use internet_checksum::Checksum;
79use net_types::ip::{Ip, IpAddress, IpInvariant as IpInv, Ipv6Addr};
80use packet::{FragmentedBytesMut, SerializeTarget};
81
82// The "sealed trait" pattern.
83//
84// https://rust-lang.github.io/api-guidelines/future-proofing.html
85mod private {
86    pub trait Sealed {}
87}
88
89/// The checksumming action that should be performed during serialization based
90/// on available checksum offloading capabilities.
91#[derive(Debug, Copy, Clone, PartialEq)]
92pub enum TransportChecksumAction {
93    /// A full checksum should be computed.
94    ComputeFull,
95    /// A partial checksum over the IP pseudo-header should be computed.
96    ComputePartial,
97}
98
99fn update_transport_checksum_pseudo_header<I: Ip>(
100    checksum: &mut Checksum,
101    src_ip: I::Addr,
102    dst_ip: I::Addr,
103    proto: u8,
104    transport_len: usize,
105) -> Result<(), TryFromIntError> {
106    I::map_ip_in(
107        (IpInv(checksum), src_ip, dst_ip, IpInv(proto), IpInv(transport_len)),
108        |(IpInv(checksum), src_ip, dst_ip, IpInv(proto), IpInv(transport_len))| {
109            let pseudo_header = {
110                // 4 bytes for src_ip + 4 bytes for dst_ip + 1 byte of zeros + 1
111                // byte for protocol + 2 bytes for total_len
112                let mut pseudo_header = [0u8; 12];
113                (&mut pseudo_header[..4]).copy_from_slice(src_ip.bytes());
114                (&mut pseudo_header[4..8]).copy_from_slice(dst_ip.bytes());
115                pseudo_header[9] = proto;
116                NetworkEndian::write_u16(&mut pseudo_header[10..12], transport_len.try_into()?);
117                pseudo_header
118            };
119            // add_bytes contains some branching logic at the beginning which is
120            // a bit more expensive than the main loop of the algorithm. In
121            // order to make sure we go through that logic as few times as
122            // possible, we construct the entire pseudo-header first, and then
123            // add it to the checksum all at once.
124            checksum.add_bytes(&pseudo_header[..]);
125            Ok(())
126        },
127        |(IpInv(checksum), src_ip, dst_ip, IpInv(proto), IpInv(transport_len))| {
128            let pseudo_header = {
129                // 16 bytes for src_ip + 16 bytes for dst_ip + 4 bytes for
130                // total_len + 3 bytes of zeroes + 1 byte for next header
131                let mut pseudo_header = [0u8; 40];
132                (&mut pseudo_header[..16]).copy_from_slice(src_ip.bytes());
133                (&mut pseudo_header[16..32]).copy_from_slice(dst_ip.bytes());
134                NetworkEndian::write_u32(&mut pseudo_header[32..36], transport_len.try_into()?);
135                pseudo_header[39] = proto;
136                pseudo_header
137            };
138            // add_bytes contains some branching logic at the beginning which is
139            // a bit more expensive than the main loop of the algorithm. In
140            // order to make sure we go through that logic as few times as
141            // possible, we construct the entire pseudo-header first, and then
142            // add it to the checksum all at once.
143            checksum.add_bytes(&pseudo_header[..]);
144            Ok(())
145        },
146    )
147}
148
149/// Compute the checksum used by TCP and UDP.
150///
151/// `compute_transport_checksum` computes the checksum used by TCP and UDP. For
152/// IPv4, the total packet length `transport_len` must fit in a `u16`, and for
153/// IPv6, a `u32`. If the provided packet is too big,
154/// `compute_transport_checksum` returns `None`.
155fn compute_transport_checksum_parts<'a, A: IpAddress, P>(
156    src_ip: A,
157    dst_ip: A,
158    proto: u8,
159    parts: P,
160) -> Option<[u8; 2]>
161where
162    P: Iterator<Item = &'a &'a [u8]> + Clone,
163{
164    // See for details:
165    // https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Checksum_computation
166    let mut checksum = Checksum::new();
167    let transport_len = parts.clone().map(|b| b.len()).sum();
168    update_transport_checksum_pseudo_header::<A::Version>(
169        &mut checksum,
170        src_ip,
171        dst_ip,
172        proto,
173        transport_len,
174    )
175    .ok()?;
176    for p in parts {
177        checksum.add_bytes(p);
178    }
179    Some(checksum.checksum())
180}
181
182/// Compute the checksum used by TCP and UDP.
183///
184/// Same as [`compute_transport_checksum_parts`] but gets the parts from a
185/// `SerializeTarget`.
186fn compute_transport_checksum_serialize<A: IpAddress>(
187    src_ip: A,
188    dst_ip: A,
189    proto: u8,
190    target: &SerializeTarget<'_>,
191    body: FragmentedBytesMut<'_, '_>,
192) -> Option<[u8; 2]> {
193    // See for details:
194    // https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Checksum_computation
195    let mut checksum = Checksum::new();
196    let transport_len = target.header.len() + body.len() + target.footer.len();
197    update_transport_checksum_pseudo_header::<A::Version>(
198        &mut checksum,
199        src_ip,
200        dst_ip,
201        proto,
202        transport_len,
203    )
204    .ok()?;
205
206    checksum.add_bytes(target.header);
207    for p in body.iter_fragments() {
208        checksum.add_bytes(p);
209    }
210    checksum.add_bytes(target.footer);
211    Some(checksum.checksum())
212}
213
214/// Computes just the pseudo-header portion of a TCP or UDP checksum.
215///
216/// Returns the one's complement sum, as expected by hardware offloading engines.
217fn compute_transport_pseudo_header_partial_checksum<A: IpAddress>(
218    src_ip: A,
219    dst_ip: A,
220    proto: u8,
221    target: &SerializeTarget<'_>,
222    body: FragmentedBytesMut<'_, '_>,
223) -> Option<[u8; 2]> {
224    // See for details:
225    // https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Checksum_computation
226    let mut checksum = Checksum::new();
227    let transport_len = target.header.len() + body.len() + target.footer.len();
228    update_transport_checksum_pseudo_header::<A::Version>(
229        &mut checksum,
230        src_ip,
231        dst_ip,
232        proto,
233        transport_len,
234    )
235    .ok()?;
236    checksum.partial_checksum()
237}
238
239/// Compute the checksum used by TCP and UDP.
240///
241/// Same as [`compute_transport_checksum_parts`] but with a single part.
242#[cfg(test)]
243fn compute_transport_checksum<A: IpAddress>(
244    src_ip: A,
245    dst_ip: A,
246    proto: u8,
247    packet: &[u8],
248) -> Option<[u8; 2]> {
249    let mut checksum = Checksum::new();
250    update_transport_checksum_pseudo_header::<A::Version>(
251        &mut checksum,
252        src_ip,
253        dst_ip,
254        proto,
255        packet.len(),
256    )
257    .ok()?;
258    checksum.add_bytes(packet);
259    Some(checksum.checksum())
260}