garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /net/src/wire.rs

raw

//! Byte order, checksums and packet framing.

use crate::{IpAddr, Ipv4, Ipv6};
use alloc::vec::Vec;

pub fn be16(b: &[u8], at: usize) -> Option<u16> {
    Some(u16::from_be_bytes(b.get(at..at + 2)?.try_into().ok()?))
}

pub fn be32(b: &[u8], at: usize) -> Option<u32> {
    Some(u32::from_be_bytes(b.get(at..at + 4)?.try_into().ok()?))
}

/// The Internet checksum: the ones' complement of the ones' complement sum
/// of 16-bit words. Over data that carries its own checksum, it is 0.
pub fn checksum(data: &[u8]) -> u16 {
    !fold(sum(0, data))
}

fn sum(mut acc: u32, data: &[u8]) -> u32 {
    let (pairs, rest) = data.as_chunks::<2>();
    for c in pairs {
        acc += u32::from(u16::from_be_bytes(*c));
    }
    if let [last] = rest {
        acc += u32::from(*last) << 8;
    }
    acc
}

fn fold(mut acc: u32) -> u16 {
    while acc > 0xffff {
        acc = (acc & 0xffff) + (acc >> 16);
    }
    acc as u16
}

fn pseudo(source: &IpAddr, destination: &IpAddr, protocol: u8, len: usize) -> u32 {
    match (source, destination) {
        (IpAddr::V6(s), IpAddr::V6(d)) => {
            let mut p = [0u8; 40];
            p[..16].copy_from_slice(s);
            p[16..32].copy_from_slice(d);
            p[32..36].copy_from_slice(&(len as u32).to_be_bytes());
            p[39] = protocol;
            sum(0, &p)
        }
        _ => {
            let (s, d) = (v4(source), v4(destination));
            let mut p = [0u8; 12];
            p[..4].copy_from_slice(&s);
            p[4..8].copy_from_slice(&d);
            p[9] = protocol;
            p[10..12].copy_from_slice(&(len as u16).to_be_bytes());
            sum(0, &p)
        }
    }
}

fn v4(ip: &IpAddr) -> Ipv4 {
    match ip {
        IpAddr::V4(ip) => *ip,
        IpAddr::V6(ip) => ip[12..].try_into().unwrap(),
    }
}

/// A transport's checksum with its pseudo-header (TCP, UDP, ICMPv6): 0
/// over data that is whole.
pub fn transport_checksum(source: &IpAddr, destination: &IpAddr, protocol: u8, data: &[u8]) -> u16 {
    !fold(sum(pseudo(source, destination, protocol, data.len()), data))
}

/// TCP's checksum with its pseudo-header: 0 over a segment that is whole.
pub fn tcp_checksum(source: &IpAddr, destination: &IpAddr, segment: &[u8]) -> u16 {
    transport_checksum(source, destination, 6, segment)
}

/// An ICMPv6 message with its checksum filled in.
pub fn icmpv6(source: &Ipv6, destination: &Ipv6, mut m: Vec<u8>) -> Vec<u8> {
    m[2..4].copy_from_slice(&[0, 0]);
    let c = transport_checksum(&IpAddr::V6(*source), &IpAddr::V6(*destination), 58, &m);
    m[2..4].copy_from_slice(&c.to_be_bytes());
    m
}

pub fn udp_packet(source: &IpAddr, destination: &IpAddr, from: u16, to: u16, data: &[u8]) -> Vec<u8> {
    let len = 8 + data.len();
    let mut u = Vec::with_capacity(len);
    u.extend_from_slice(&from.to_be_bytes());
    u.extend_from_slice(&to.to_be_bytes());
    u.extend_from_slice(&(len as u16).to_be_bytes());
    u.extend_from_slice(&[0, 0]);
    u.extend_from_slice(data);
    let mut c = transport_checksum(source, destination, 17, &u);
    if c == 0 {
        c = 0xffff;
    }
    u[6..8].copy_from_slice(&c.to_be_bytes());
    u
}

pub fn ipv4_packet(id: &mut u16, source: Ipv4, destination: Ipv4, protocol: u8, body: &[u8]) -> Vec<u8> {
    *id = id.wrapping_add(1);
    let total = 20 + body.len();
    let mut p = Vec::with_capacity(total);
    p.extend_from_slice(&[0x45, 0]);
    p.extend_from_slice(&(total as u16).to_be_bytes());
    p.extend_from_slice(&id.to_be_bytes());
    p.extend_from_slice(&[0x40, 0, 64, protocol, 0, 0]);
    p.extend_from_slice(&source);
    p.extend_from_slice(&destination);
    let c = checksum(&p);
    p[10..12].copy_from_slice(&c.to_be_bytes());
    p.extend_from_slice(body);
    p
}