> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/upg/2.17/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/upg/2.17/features/cg-nat.md).

# Carrier-grade NAT

Carrier-grade Network Address Translation (CG-NAT) is used by Internet service providers to allow multiple devices in private networks to share a limited number of public IP addresses, helping manage IPv4 address exhaustion.

User Plane Gateway (UPG) NAT allows the Control Plane to manage NAT binding allocation for user traffic. The CG-NAT feature, which handles large-scale network address translations, is only available in the TDF mode. UPG manages the NAT pools and reports their status to the Control Plane using PFCP messages, following the BBF WT-459.2 standard.

## Terminology

These are key terms related to the CG-NAT feature:

- **CG-NAT (Carrier-Grade NAT)**: A large-scale NAT implementation optimised for handling a huge number of users.
- **Inbound**: The side of the router facing the client whose addresses are translated.
- **Outbound**: The side of the router, usually facing the Internet, with traffic after NAT translation.
- **i2o traffic**: Traffic moving from the inbound (client) to the outbound (Internet) network, typically user uploads.
- **o2i traffic**: Traffic moving from the outbound (Internet) to the inbound (client) network, typically user downloads.
- **NAT port block**: A range of ports on an external IP endpoint dedicated to a single NAT allocation.
- **NAT endpoint**: A router IP address in the outbound network used for NAT. Many NAT users share the same IP, but each has a different port block.
- **NAT binding**: Links a NAT port block to a specific PFCP session.
- **NAT pool**: A group of public IP addresses used for NAT in the same network.
- **NAT flow**: A temporary connection state that tracks the mapping between internal and external IP:port pairs.

## Architecture

Each session gets a single NAT binding from a specific NAT pool, which is named during session creation by the Control Plane. The NAT pool configuration includes the NWI, NAT endpoints, and port blocks allocated to those endpoints. The NAT pool uses the NWI interface to match and route outbound traffic.

![NAT resources hierarchy](https://docs.cennso.com/api/assets/components/upg/2.17/assets/nat.svg)

UPG CG-NAT supports TCP, UDP, ICMP echo, and ICMP error messages. It tracks temporary port allocations in the flows table to handle translation and matching on translated addresses.

UPG CG-NAT flows are Endpoint-Dependent (ED), meaning a new flow is created for each destination IP:port pair. This type of NAT flows is commonly used for CG-NAT because it improves security by isolating flows and makes efficient use of ports.

For optimization, UPG CG-NAT uses two types of flows:

- **TCP/UDP flows**: These are integrated into the UPG flows table and inherit its configuration.
- **ICMP flows**: These can't be optimised like TCP/UDP flows, so they use a separate ICMP NAT flow table. ICMP flows have a limit on the number of flows per session to prevent RAM exhaustion, which can occur with aggressive port usage, such as port scanning. The default limit is 64 flows per session. You can change it using this CLI command:

   ```bash
   upf nat config icmp-max-flows-per-binding {LIMIT}
   ```

### NAT binding allocation

The NAT binding allocation process includes these steps:

1. Available NAT pools are shared with the SMF node during the PFCP Association Setup Response.
2. In the PFCP Session Establishment Request, SMF must indicate a NAT request with BBF Apply Action IE and provide the NAT pool name using BBF NAT Port Block IE.
3. If the NAT binding is created successfully, UPG returns the assigned external IP and port block in the PFCP Session Establishment Response.
4. If UPG fails to allocate a NAT binding, the PFCP Session Establishment Request is rejected, and a TP Error Report IE includes the failure reason, for example `FAR ID <ID>, Error creating NAT binding for pool '<pool-name>': out of blocks`.

## Metrics

UPG CG-NAT-specific metrics are available with NAT pool statistics. These metrics are identified by the `name` label, which contains the NAT pool name.

- `vpp_nat_pool_info` and `vpp_nat_pool_block_size` show the configured NAT pool interface and block size.
- `vpp_nat_pool_utilization` and `vpp_nat_pool_capacity` show the usage of NAT pool blocks.
- `vpp_nat_pool_bytes_total` and `vpp_nat_pool_packets_total` provide NAT pool traffic statistics, with the `direction` label indicating `i2o` (inbound to outbound) or `o2i` (outbound to inbound) direction.
- `vpp_nat_pool_flow_count` provides the number of flows created, with the `type` label showing whether it's a `tcpudp` or `icmp` flow.

The `vpp_nat_errors_total` metric tracks error events that usually result in packet drops. Typical combinations of node and error values are explained in this table:

| `node` value | Direction | `error` value | Description |
| :--- | :--- | :--- | :--- |
| upf-ip4-forward | `i2o` | `unsupported_ip_proto` | UE originated packet with unsupported IP protocol |
| upf-ip4-forward | `i2o` | `out_of_ports` | UE consumed all available NAT block for connections to a specific IP:Port pair |
| upf-ip4-nat-forward | `i2o` | `unsupported_icmp_type` | UE originated ICMP packet with unsupported ICMP type |
| upf-ip4-nat-forward | `i2o` | `icmp_flows_limit_per_binding` | UE reached the limit of ICMP flows per binding |
| upf-ip4-nat-forward | `i2o` | `icmp_out_of_ports` | UE consumed all available NAT blocks for ICMP messages to a specific IP:Identifier pair |
| upf-ip4-nat-dpo | `o2i` | `unknown_flow` | NAT endpoint received a packet with no flow assigned |
| upf-ip4-nat-dpo | `o2i` | `unknown_icmp_flow` | NAT endpoint received an ICMP packet with no flow assigned |
| upf-ip4-nat-dpo | `o2i` | `unsupported_ip_protocol` | NAT endpoint received an ICMP packet with unsupported ICMP type |
