JFLib

netpkt/nat.rs#

Source: lib/netpkt/nat.rs

nat -- address and port translation of IPv4 packets in place (RFC 3022): addresses, TCP and
UDP ports and the identifiers of ICMP queries change, and every checksum that covers them
follows incrementally (RFC 1624); ICMP errors take the packet they quote along (RFC 5508)

  Flow::of(packet) -> Option<Flow>      protocol, addresses, ports (the identifier of an ICMP
                                        query in both), ICMP type and code, TCP flags; a
                                        fragment after the first has no ports (`ports`
                                        false)
  Flow::quoted(packet) -> Option<Flow>  the packet an ICMP error quotes (its header and at
                                        least 8 bytes of its payload)
  rewrite(pkt, side, addr, port)        the source or destination address of a packet and,
                                        with Some, its port (an ICMP query: its identifier);
                                        the IPv4 header checksum, the TCP or UDP checksum
                                        (pseudo header and port; a UDP checksum of 0 means
                                        none and stays 0) and the ICMP checksum follow; a
                                        later fragment changes its address only
  rewrite_quoted(pkt, side, addr, port) the same for the packet inside an ICMP error: its
                                        header checksum, its TCP or UDP checksum when the
                                        quote holds it, an ICMP query's checksum, and the
                                        error's own ICMP checksum over every changed byte
                                        (the outer header stays; rewrite() changes it)
  is_query(type) / reply_of(type)       ICMP queries that carry an identifier (echo,
                                        timestamp, information, address mask) and the type
                                        of their answers

Translation needs no full recomputation: the segment of a first fragment is not complete,
yet its checksum covers all of it, and an ICMP error quotes only the start of a packet.
Every field these functions change lies on a 16-bit boundary of the data its checksum
covers, so the old and new words are enough (checksum::update).
  • enum Side which address and port of a packet a translation changes

    • Src
    • Dst
  • struct Flow what translation and filters look at in a packet

    • proto: u8
    • src: Ipv4
    • dst: Ipv4
    • sport: u16 -- TCP and UDP ports; an ICMP query's identifier in both; 0 when unknown
    • dport: u16
    • ports: bool -- the ports (or the identifier) are known: not a later fragment, and the transport header is there
    • icmp: Option<(u8, u8)> -- ICMP type and code
    • flags: u8 -- TCP flags (netpkt::tcp::SYN ...)
    • later: bool -- a fragment after the first (no transport header)
    • mf: bool -- more fragments follow
    • id: u16 -- the identification field (fragments of one datagram share it)
  • fn is_query(typ: u8) -> bool ICMP queries with an identifier: echo, timestamp, information, address mask (requests and replies)

  • fn reply_of(typ: u8) -> Option<u8> the type of the answer to an ICMP query request (None: not a request)

impl Flow

  • fn of(packet: &[u8]) -> Option<Flow> the flow of a whole packet

  • fn quoted(packet: &[u8]) -> Option<Flow> the flow of the packet an ICMP error quotes (packet: the error's whole IPv4 packet)

  • fn rewrite(pkt: &mut [u8], side: Side, addr: Ipv4, port: Option<u16>) -> bool sets the source or destination address of an IPv4 packet and, with port, its TCP / UDP port or ICMP query identifier, with every checksum updated; false when it is no IPv4 packet

  • fn rewrite_quoted(pkt: &mut [u8], side: Side, addr: Ipv4, port: Option<u16>) -> bool sets the source or destination address (and port or query identifier) of the packet an ICMP error quotes, with the quote's checksums and the error's ICMP checksum; false when pkt is no ICMP error with a quoted IPv4 header

Source: docs/API.md in the JFLib repository.