NG_NAT(4)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1, nixpkgs unstable

πŸ’– NG_NAT(4) πŸ’–

okay bestie, so this one is ng_nat β€” that's the NAT netgraph node type, and it's literally the kernel interfaces manual page for it. buckle up, she's a long one πŸŽ€


✨ SYNOPSIS ✨

#include <netgraph/ng_nat.h>

πŸ¦‹ DESCRIPTION πŸ¦‹

so, an ng_nat node does network address translation (NAT) of IPv4 packets that pass through it. that's the whole vibe. a nat node uses the libalias(3) engine to do the actual packet aliasing β€” she's not doing it alone, she's got a whole engine behind her πŸ’…


🌸 HOOKS 🌸

this node type has two hooks, and they're giving main character energy in opposite directions:

  • out β€” packets received on this hook are considered outgoing and will be masqueraded to a configured address. bye bestie, you're getting a new face ✨
  • in β€” packets coming on this hook are considered incoming and will be dealiased. welcome home, we see the real you 🫢

πŸ‘‘ CONTROL MESSAGES πŸ‘‘

this node type supports the generic control messages, plus all of these cuties:

  • NGM_NAT_SET_IPADDR (setaliasaddr) β€” configure the aliasing address for a node. after both hooks have been connected and the aliasing address was configured, the node is ready for aliasing operation. like, now she's officially in business fr fr πŸ’–
  • NGM_NAT_SET_MODE (setmode) β€” set the node's operation mode using the supplied struct ng_nat_mode.
struct ng_nat_mode {
        uint32_t        flags;
        uint32_t        mask;
};
/* Supported flags: */
#define NG_NAT_LOG                      0x01
#define NG_NAT_DENY_INCOMING            0x02
#define NG_NAT_SAME_PORTS               0x04
#define NG_NAT_UNREGISTERED_ONLY        0x10
#define NG_NAT_RESET_ON_ADDR_CHANGE     0x20
#define NG_NAT_PROXY_ONLY               0x40
#define NG_NAT_REVERSE                  0x80
#define NG_NAT_UNREGISTERED_CGN         0x100
#define NG_NAT_UDP_EIM                  0x200

the corresponding libalias flags can be found by replacing the NG_NAT prefix with PKT_ALIAS. iykyk πŸ”₯

  • NGM_NAT_SET_TARGET (settarget) β€” configure the target address for a node. when an incoming packet that isn't associated with any pre-existing aliasing link arrives at the host machine, it will be sent to the specified address. she catches strays and gives them a home 🏠
  • NGM_NAT_REDIRECT_PORT (redirectport) β€” redirect incoming connections arriving to given port(s) to another host and port(s). the following struct ng_nat_redirect_port must be supplied as the argument.
#define NG_NAT_DESC_LENGTH      64
struct ng_nat_redirect_port {
        struct in_addr  local_addr;
        struct in_addr  alias_addr;
        struct in_addr  remote_addr;
        uint16_t        local_port;
        uint16_t        alias_port;
        uint16_t        remote_port;
        uint8_t         proto;
        char            description[NG_NAT_DESC_LENGTH];
};

redirection is assigned a unique ID which is returned as the response to this message, and information about the redirection gets added to the list of static redirects which later can be retrieved by the NGM_NAT_LIST_REDIRECTS message. so she remembers everything, no cap πŸ’€

  • NGM_NAT_REDIRECT_ADDR (redirectaddr) β€” redirect traffic for a public IP address to a machine on the local network. this function is known as static NAT. the following struct ng_nat_redirect_addr must be supplied as the argument.
struct ng_nat_redirect_addr {
        struct in_addr  local_addr;
        struct in_addr  alias_addr;
        char            description[NG_NAT_DESC_LENGTH];
};

a unique ID for this redirection is returned as the response to this message. πŸŽ€

  • NGM_NAT_REDIRECT_PROTO (redirectproto) β€” redirect incoming IP packets of protocol proto (see protocols(5)) to a machine on the local network. the following struct ng_nat_redirect_proto must be supplied as the argument.
struct ng_nat_redirect_proto {
        struct in_addr  local_addr;
        struct in_addr  alias_addr;
        struct in_addr  remote_addr;
        uint8_t         proto;
        char            description[NG_NAT_DESC_LENGTH];
};

a unique ID for this redirection is returned as the response to this message. πŸ¦‹

  • NGM_NAT_REDIRECT_DYNAMIC (redirectdynamic) β€” mark the redirection with the specified ID as dynamic, i.e. it will serve for exactly one next connection and then will be automatically deleted from the internal links table. only fully specified links can be made dynamic. the redirection with this ID is also immediately deleted from the user-visible list of static redirects (available through the NGM_NAT_LIST_REDIRECTS message). one and done, literally πŸ™ˆ
  • NGM_NAT_REDIRECT_DELETE (redirectdelete) β€” delete the redirection with the specified ID (currently active connections are not affected). she leaves the group chat but the receipts stay πŸ’…
  • NGM_NAT_ADD_SERVER (addserver) β€” add another server to a pool. this is used to transparently offload network load on a single server and distribute the load across a pool of servers, also known as LSNAT (RFC 2391). the following struct ng_nat_add_server must be supplied as the argument.
struct ng_nat_add_server {
        uint32_t        id;
        struct in_addr  addr;
        uint16_t        port;
};

first, the redirection is set up by NGM_NAT_REDIRECT_PORT or NGM_NAT_REDIRECT_ADDR. then, the ID of that redirection is used in multiple NGM_NAT_ADD_SERVER messages to add the necessary number of servers. for redirections created by NGM_NAT_REDIRECT_ADDR, the port is ignored and could have any value. the original redirection's parameters local_addr and local_port are also ignored after NGM_NAT_ADD_SERVER was used (they are effectively replaced by the server pool). squad goals, she's sharing the load 🫢✨

  • NGM_NAT_LIST_REDIRECTS (listredirects) β€” return the list of configured static redirects as struct ng_nat_list_redirects.
struct ng_nat_listrdrs_entry {
        uint32_t        id;             /* Anything except zero */
        struct in_addr  local_addr;
        struct in_addr  alias_addr;
        struct in_addr  remote_addr;
        uint16_t        local_port;
        uint16_t        alias_port;
        uint16_t        remote_port;
        uint16_t        proto;          /* Valid proto or NG_NAT_REDIRPROTO_ADDR */
        uint16_t        lsnat;          /* LSNAT servers count */
        char            description[NG_NAT_DESC_LENGTH];
};
struct ng_nat_list_redirects {
        uint32_t                total_count;
        struct ng_nat_listrdrs_entry redirects[];
};
#define NG_NAT_REDIRPROTO_ADDR  (IPPROTO_MAX + 3)

entries of the redirects array are returned in the unified format for all redirect types. ports are meaningful only if the protocol is either TCP or UDP, and static NAT redirection (created by NGM_NAT_REDIRECT_ADDR) is indicated by proto set to NG_NAT_REDIRPROTO_ADDR. if the lsnat servers counter is greater than zero, then local_addr and local_port are also meaningless. so like, read the room before you trust those fields 😭

  • NGM_NAT_PROXY_RULE (proxyrule) β€” specify a transparent proxying rule (a string must be supplied as the argument). see libalias(3) for details. πŸ”
  • NGM_NAT_LIBALIAS_INFO (libaliasinfo) β€” return internal statistics of the libalias(3) instance as struct ng_nat_libalias_info.
struct ng_nat_libalias_info {
        uint32_t        icmpLinkCount;
        uint32_t        udpLinkCount;
        uint32_t        tcpLinkCount;
        uint32_t        sctpLinkCount;
        uint32_t        pptpLinkCount;
        uint32_t        protoLinkCount;
        uint32_t        fragmentIdLinkCount;
        uint32_t        fragmentPtrLinkCount;
        uint32_t        sockCount;
};

in case ng_nat failed to retrieve a certain counter from its libalias(3) instance, the corresponding field is returned as UINT32_MAX. yeah, that's the "idk her" value πŸ’€

  • NGM_NAT_SET_DLT (setdlt) β€” sets the data link type on the in and out hooks. currently, supported types are DLT_RAW (raw IP datagrams , no offset applied, the default) and DLT_EN10MB (Ethernet). DLT_ definitions can be found in <net/bpf.h>. if you want to work on the ipfw(8) level you must use no additional offset by specifying DLT_RAW. if, however, you attach ng_nat to a network interface directly and EN10MB is specified, then the extra offset will be applied to take into account the link-level header. in this mode ng_nat would also inspect the appropriate type field in the Ethernet header and pass-through any datagrams that are not IP packets. she checks IDs at the door, no gatecrashing πŸ”₯
  • NGM_NAT_GET_DLT (getdlt) β€” this control message returns the current data link type of the in and out hooks. πŸ“¬
okay so important lore bestie β€” in all redirection messages, local_addr and local_port mean the address and port of the target machine in the internal network, respectively. if alias_addr is zero, then the default aliasing address (set by NGM_NAT_SET_IPADDR) is used. connections can also be restricted to be accepted only from specific external machines by using a non-zero remote_addr and/or remote_port.

each redirection is assigned an ID which can later be used for redirection manipulation on an individual basis (e.g. removal). this ID is guaranteed to be unique until the node shuts down (it will not be reused after deletion), and is returned to the user after making each new redirection, or can be found in the stored list of all redirections. the description is passed to and from the node unchanged, together with the ID providing a way for several entities to concurrently manipulate redirections in an automated way. collaborative slay, no stepping on each other's toes πŸ’œ


🌈 SHUTDOWN 🌈

this node shuts down upon receipt of a NGM_SHUTDOWN control message, or when both hooks are disconnected. and that's her exit, iconic to the end πŸ‘‘

🌸 EXAMPLES 🌸

Okay bestie, this is the part where it gets fun β€” real setups, real commands, all of it copy-paste-able. πŸŽ€

✨ Example 1: injecting packets into a nat node via ng_ipfw(4)

In the following example, the packets are injected into a nat node using the ng_ipfw(4) node. Slay, let's go:

# Create NAT node
ngctl mkpeer ipfw: nat 60 out
ngctl name ipfw:60 nat
ngctl connect ipfw: nat: 61 in
ngctl msg nat: setaliasaddr x.y.35.8

# Divert traffic into NAT node
ipfw add 300 netgraph 61 all from any to any in via fxp0
ipfw add 400 netgraph 60 all from any to any out via fxp0

# Let packets continue with after being (de)aliased
sysctl net.inet.ip.fw.one_pass=0

Like, the setaliasaddr is literally the main character here β€” that's your alias IP, periodt. πŸ’…


πŸ¦‹ Example 2: right after ng_iface(4), serial line w/ HDLC

The ng_nat node can be inserted right after the ng_iface(4) node in the graph. In the following example, we perform masquerading on a serial line with HDLC encapsulation. Ngl this one's kind of iconic:

/usr/sbin/ngctl -f- <<-SEQ
        mkpeer cp0: cisco rawdata downstream
        name cp0:rawdata hdlc
        mkpeer hdlc: nat inet in
        name hdlc:inet nat
        mkpeer nat: iface out inet
        msg nat: setaliasaddr x.y.8.35
SEQ
ifconfig ng0 x.y.8.35 x.y.8.1

The way that mkpeer chain just flows β€” hdlc β†’ nat β†’ iface. Chef's kiss. 🧚


🌈 Example 3: straight onto the physical interface via ng_ether(4)

The ng_nat node can also be attached directly to the physical interface via the ng_ether(4) node in the graph. In the following example, we perform masquerading on a Ethernet interface connected to a public network. Ethernet girlies rise up 🫢:

ifconfig igb0 inet x.y.8.35 netmask 0xfffff000
route add default x.y.0.1
/usr/sbin/ngctl -f- <<-SEQ
        mkpeer igb0: nat lower in
        name igb0:lower igb0_NAT
        connect igb0: igb0_NAT: upper out
        msg igb0_NAT: setdlt 1
        msg igb0_NAT: setaliasaddr x.y.8.35
SEQ

Obsessed with setdlt 1 showing up here β€” don't forget it or the vibe check fails, fr fr. 😭


πŸ‘‘ SEE ALSO πŸ‘‘

Go read these next, bestie, they're literally the supporting cast:


πŸŽ€ HISTORY πŸŽ€

The ng_nat node type was implemented in FreeBSD 6.0. Vintage but still serving looks, no cap. ⭐


πŸ’– AUTHORS πŸ’–

Gleb Smirnoff <glebius@FreeBSD.org> β€” the legend who made all this happen. Respect the icon. πŸ™


And that's the whole page, bestie β€” you ate. This one came from the Debian build, dated December 6, 2024, filed under NG_NAT(4). Now go mkpeer something cute. πŸ’œβœ¨

πŸ“¦ where this page comes from