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
89fn update_transport_checksum_pseudo_header<I: Ip>(
90    checksum: &mut Checksum,
91    src_ip: I::Addr,
92    dst_ip: I::Addr,
93    proto: u8,
94    transport_len: usize,
95) -> Result<(), TryFromIntError> {
96    I::map_ip_in(
97        (IpInv(checksum), src_ip, dst_ip, IpInv(proto), IpInv(transport_len)),
98        |(IpInv(checksum), src_ip, dst_ip, IpInv(proto), IpInv(transport_len))| {
99            let pseudo_header = {
100                // 4 bytes for src_ip + 4 bytes for dst_ip + 1 byte of zeros + 1
101                // byte for protocol + 2 bytes for total_len
102                let mut pseudo_header = [0u8; 12];
103                (&mut pseudo_header[..4]).copy_from_slice(src_ip.bytes());
104                (&mut pseudo_header[4..8]).copy_from_slice(dst_ip.bytes());
105                pseudo_header[9] = proto;
106                NetworkEndian::write_u16(&mut pseudo_header[10..12], transport_len.try_into()?);
107                pseudo_header
108            };
109            // add_bytes contains some branching logic at the beginning which is
110            // a bit more expensive than the main loop of the algorithm. In
111            // order to make sure we go through that logic as few times as
112            // possible, we construct the entire pseudo-header first, and then
113            // add it to the checksum all at once.
114            checksum.add_bytes(&pseudo_header[..]);
115            Ok(())
116        },
117        |(IpInv(checksum), src_ip, dst_ip, IpInv(proto), IpInv(transport_len))| {
118            let pseudo_header = {
119                // 16 bytes for src_ip + 16 bytes for dst_ip + 4 bytes for
120                // total_len + 3 bytes of zeroes + 1 byte for next header
121                let mut pseudo_header = [0u8; 40];
122                (&mut pseudo_header[..16]).copy_from_slice(src_ip.bytes());
123                (&mut pseudo_header[16..32]).copy_from_slice(dst_ip.bytes());
124                NetworkEndian::write_u32(&mut pseudo_header[32..36], transport_len.try_into()?);
125                pseudo_header[39] = proto;
126                pseudo_header
127            };
128            // add_bytes contains some branching logic at the beginning which is
129            // a bit more expensive than the main loop of the algorithm. In
130            // order to make sure we go through that logic as few times as
131            // possible, we construct the entire pseudo-header first, and then
132            // add it to the checksum all at once.
133            checksum.add_bytes(&pseudo_header[..]);
134            Ok(())
135        },
136    )
137}
138
139/// Compute the checksum used by TCP and UDP.
140///
141/// `compute_transport_checksum` computes the checksum used by TCP and UDP. For
142/// IPv4, the total packet length `transport_len` must fit in a `u16`, and for
143/// IPv6, a `u32`. If the provided packet is too big,
144/// `compute_transport_checksum` returns `None`.
145fn compute_transport_checksum_parts<'a, A: IpAddress, P>(
146    src_ip: A,
147    dst_ip: A,
148    proto: u8,
149    parts: P,
150) -> Option<[u8; 2]>
151where
152    P: Iterator<Item = &'a &'a [u8]> + Clone,
153{
154    // See for details:
155    // https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Checksum_computation
156    let mut checksum = Checksum::new();
157    let transport_len = parts.clone().map(|b| b.len()).sum();
158    update_transport_checksum_pseudo_header::<A::Version>(
159        &mut checksum,
160        src_ip,
161        dst_ip,
162        proto,
163        transport_len,
164    )
165    .ok()?;
166    for p in parts {
167        checksum.add_bytes(p);
168    }
169    Some(checksum.checksum())
170}
171
172/// Compute the checksum used by TCP and UDP.
173///
174/// Same as [`compute_transport_checksum_parts`] but gets the parts from a
175/// `SerializeTarget`.
176fn compute_transport_checksum_serialize<A: IpAddress>(
177    src_ip: A,
178    dst_ip: A,
179    proto: u8,
180    target: &mut SerializeTarget<'_>,
181    body: FragmentedBytesMut<'_, '_>,
182) -> Option<[u8; 2]> {
183    // See for details:
184    // https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Checksum_computation
185    let mut checksum = Checksum::new();
186    let transport_len = target.header.len() + body.len() + target.footer.len();
187    update_transport_checksum_pseudo_header::<A::Version>(
188        &mut checksum,
189        src_ip,
190        dst_ip,
191        proto,
192        transport_len,
193    )
194    .ok()?;
195
196    checksum.add_bytes(target.header);
197    for p in body.iter_fragments() {
198        checksum.add_bytes(p);
199    }
200    checksum.add_bytes(target.footer);
201    Some(checksum.checksum())
202}
203
204/// Compute the checksum used by TCP and UDP.
205///
206/// Same as [`compute_transport_checksum_parts`] but with a single part.
207#[cfg(test)]
208fn compute_transport_checksum<A: IpAddress>(
209    src_ip: A,
210    dst_ip: A,
211    proto: u8,
212    packet: &[u8],
213) -> Option<[u8; 2]> {
214    let mut checksum = Checksum::new();
215    update_transport_checksum_pseudo_header::<A::Version>(
216        &mut checksum,
217        src_ip,
218        dst_ip,
219        proto,
220        packet.len(),
221    )
222    .ok()?;
223    checksum.add_bytes(packet);
224    Some(checksum.checksum())
225}