NG_NAT(4)
π 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 suppliedstruct 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 followingstruct ng_nat_redirect_portmust 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 followingstruct ng_nat_redirect_addrmust 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 (seeprotocols(5)) to a machine on the local network. the followingstruct ng_nat_redirect_protomust 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 theNGM_NAT_LIST_REDIRECTSmessage). 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 followingstruct ng_nat_add_servermust 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 asstruct 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). seelibalias(3)for details. πNGM_NAT_LIBALIAS_INFO(libaliasinfo) β return internal statistics of thelibalias(3)instance asstruct 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 theinandouthooks. currently, supported types areDLT_RAW(raw IP datagrams , no offset applied, the default) andDLT_EN10MB(Ethernet).DLT_definitions can be found in<net/bpf.h>. if you want to work on theipfw(8)level you must use no additional offset by specifyingDLT_RAW. if, however, you attachng_natto a network interface directly andEN10MBis specified, then the extra offset will be applied to take into account the link-level header. in this modeng_natwould 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 theinandouthooks. π¬
okay so important lore bestie β in all redirection messages,local_addrandlocal_portmean the address and port of the target machine in the internal network, respectively. ifalias_addris zero, then the default aliasing address (set byNGM_NAT_SET_IPADDR) is used. connections can also be restricted to be accepted only from specific external machines by using a non-zeroremote_addrand/orremote_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:
libalias(3)ng_ipfw(4)natd(8)ng_ether(8)ngctl(8)
π 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
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 2005 Gleb Smirnoff <glebius@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms