跳转到内容
Ask AI

CGNAT Configuration Guide

此内容尚不支持你的语言。

IPv4 address exhaustion remains a critical constraint for Internet Service Providers (ISPs) while IPv4-based services continue to thrive. Carrier-Grade NAT (CGNAT), also known as Large-Scale NAT (LSN) or NAT444, is an operator-grade NAT44 technology that allows a large number of subscribers to share a small pool of public IPv4 addresses, significantly extending the lifespan of the IPv4 infrastructure. The standard behavior and requirements of CGNAT are defined in RFC 6888.

Compared with an ordinary NAT (such as a home router or enterprise egress NAT), CGNAT is not merely a matter of larger scale. It must meet additional carrier-grade requirements:

  • Massive numbers of concurrent subscribers and concurrent sessions.
  • High multiplexing ratio of public IPv4 addresses.
  • Subscriber traceability for law enforcement and audit.
  • Log volume control: per-session logging is impractical at carrier scale, so CGNAT assigns each subscriber a port block on a shared public IP address and logs only block allocation/release events.
  • Deterministic port resource allocation policies and per-user resource limits.
  • High availability and fast failover.

CGNAT is typically deployed on the path: subscriber CPE with a private address (or the shared address space 100.64.0.0/10 per RFC 6598) -> operator CGNAT device -> public IPv4 Internet.

  • CGNAT (Carrier-Grade NAT): An operator-grade IPv4 address and port translation function that allows multiple subscribers to share public IPv4 addresses.

  • NAPT (Network Address Port Translation): The translation method used by CGNAT, in which both the IP address and the transport-layer port number are translated. A unique public IP:port combination identifies each subscriber flow.

  • Port Block Allocation (PBA): The port resource management mechanism of CGNAT. The port range of each public IP address is divided into fixed-size port blocks, and blocks (rather than individual ports) are allocated to subscribers.

  • Port Block: A contiguous set of ports on one public IP address. A block is allocated to a subscriber as a whole, and ports within the block are used by that subscriber’s sessions.

  • Port Allocation Profile: The configurable policy that defines how port blocks are managed: block size, block allocation mode (on-demand/pre-alloc), port selection mode (random/sequence), reserved port range and block cooling time.

  • Address Pool: A set of public IPv4 addresses used for dynamic port block allocation, together with the port allocation profile applied to it.

  • Instance: The CGNAT service entity. An instance binds address pools, references ACLs for traffic diversion, and owns all per-instance policies such as filtering mode, user limits, aging timers and logging.

  • User: A subscriber identified by its inside (private) source IP address. Per RFC 6888 (Paired Pooling, REQ-2), all sessions of one user are mapped to the same public IP address.

  • Session: A translation entry identified by the five-tuple (protocol, inside IP/port, public IP/port, remote IP/port). Sessions are created within the port resources owned by the user.

  • Filtering Mode: The inbound (out2in) admission policy of an existing mapping: EIF (endpoint-independent), ADF (address-dependent) or APDF (address-and-port-dependent).

  • Hairpinning: The capability that allows two subscribers behind the same CGNAT instance to communicate with each other through their public mapping addresses.

  • Static Mapping: A manually configured, fixed translation between an inside endpoint and an outside endpoint. Address-level static mapping keeps the port unchanged; port-level static mapping fixes both the address and the port.

  • Deterministic NAT: A CGNAT mode in which the mapping between an inside address and its public IP + port block is derived from a fixed algorithm (the inside/outside address ratio), allowing the mapping to be reverse-derived without maintaining runtime binding state.

  • Traceability Log: Logs recording port block allocation/release (port-block mode) or session creation/deletion (session mode), exported via syslog or IPFIX.

In a typical CGNAT deployment, subscriber traffic reaches the inside interfaces of the CGNAT device, and the device translates the source address/port to a public address/port from the bound address pool before forwarding towards the public network on the outside interfaces. Return traffic is translated back by matching the session table.

Example: a subscriber packet with source 100.70.11.251:1234 destined to 8.8.8.8 is translated to source 23.159.240.10:2048 and forwarded to the public network; the return traffic is translated back to 100.70.11.251:1234.

ACL-based diversion decides which subscriber traffic enters the CGNAT translation pipeline. The ACL only describes the traffic matching conditions (source/destination address, protocol, ports); it does not perform translation itself:

  • Inside (in2out) direction: traffic matching the permit rules of the ACL bound to a CGNAT instance (via cgnat binding) is diverted into the CGNAT translation pipeline and uses the address pools bound to that instance for port block mapping and translation; traffic not matching any binding follows normal Layer-3 forwarding.
  • Outside (out2in) direction: all traffic arriving on outside interfaces is looked up in the session table. On a hit, the destination address/port are translated back to the subscriber; on a miss, the packet is never dropped but forwarded transparently, so non-CGNAT traffic (traffic destined to the device’s own addresses, pass-through services) is unaffected.

The port range of every public IP address in an address pool is divided into port blocks. Each block has exactly one of three states:

Block StateMeaning
FREEOwned by no user; available for dynamic allocation
ALLOCATEDAllocated to a user; dynamic sessions are using its ports
COOLINGReleased but still in the cooling period; cannot be re-allocated until the cooling time expires, preventing residual packets of the old user from being forwarded to a new user

Key policy knobs (defined by the port allocation profile):

  • Block size: the number of ports per block (default 2048).
  • Block allocation mode: on-demand (allocate a block only when the user actually needs ports, default) or pre-alloc (pre-allocate a fixed number of blocks per user).
  • Port selection mode: random (default) or sequence within a block.
  • Reserved ports: the leading port range excluded from block division (default 0-1023, the end value must be 2^n - 1).
  • Cooling time: how long a released block stays in COOLING state (default 120 seconds).

Per RFC 6888 Paired Pooling, once a user is bound to a public IP address, all subsequent blocks of that user are allocated from the same public IP. The public IP selection strategy balances users across IPs: IPs with block occupancy below 80% are preferred, choosing among them the one with the fewest bound users; when all IPs reach 80% occupancy, the IP with the lowest block occupancy is used, and new allocations are best-effort.

A user is identified by its inside source IP address. Per-user resource limits can be configured on the instance to prevent individual subscribers from exhausting shared resources:

  • Maximum number of port blocks per user (1-256).
  • Maximum number of ports per user (1-65535).
  • Maximum number of concurrent sessions per user (1-10240; unlimited if not configured).
  • Maximum session creation rate per user, in sessions per second over a rolling 1-second cycle (1-10240).

All blocks of a user are released and recycled when they have been idle for the configured timeout.

A session (mapping + session entry) is identified by the full five-tuple. Sessions are classified as dynamic sessions (created from PBA port resources; ports are released on deletion) or static sessions (created from a static mapping). Session aging follows TCP state and protocol type, with active refresh on traffic:

Session StateDefault Aging TimeDescription
TCP SYN-SENT / SYN-RCVD75 s
TCP ESTABLISHED7440 s
TCP FIN-RST (connection closed)60 s
UDP ACTIVE300 s
ICMP Query60 s
Other (stateless protocols)60 s

All aging timers are configurable per instance. In addition, TCP MSS clamping can be configured to avoid IP fragmentation when the path MTU decreases.

The filtering mode controls whether packets from external hosts are admitted through an existing mapping (out2in direction):

Filtering ModeBehavior
EIF (Endpoint-Independent Filtering, default)As long as the mapping exists, any external host can send packets to the public IP:port (full-cone behavior). Maximizes P2P application compatibility
ADF (Address-Dependent Filtering)Only packets from an external IP address that the user has already communicated with are allowed
APDF (Address-Port-Dependent Filtering)Only packets from the external IP address and port that the user has already communicated with are allowed

With hairpinning enabled, two subscribers behind the same CGNAT instance can access each other through the peer’s public mapping address: the device translates both the destination (to the peer’s inside address) and the source (to the local public mapping) of the hairpinned packet.

Static mappings provide fixed, predictable translations for servers or special subscribers inside the CGNAT domain. The public addresses used by static mappings belong to a static address pool, which is strictly isolated from the dynamic CGNAT address pool, so static and dynamic usage never contend for the same public IP:

  • Address-level static mapping (outside_ip <-> inside_ip, port unchanged): does not create mappings for all 65535 ports. The rule is stored in a static address table, and an exact mapping is created lazily only when traffic actually hits a specific port.
  • Port-level static mapping (outside_ip:outside_port <-> inside_ip:inside_port): the mapping is created immediately upon configuration.

Conflict checks at creation time: the outside address must not overlap with any dynamic address pool of the instance; the same public endpoint (outside_ip + protocol, or outside_ip + outside_port + protocol) and the same inside endpoint cannot be duplicated; address-level and port-level rules of the same protocol on the same IP are mutually exclusive; a static mapping cannot cover an existing dynamic mapping with the same inside endpoint.

In deterministic mode, the mapping of each inside address to a public IP address + port block is computed from a fixed algorithm: with the inside address count and the outside (pool) address count, the sharing ratio and the per-user port range (ports_per_host = 64512 / sharing_ratio) are derived, so any inside address maps to a fixed, predictable public endpoint. This allows an external system to reverse-derive the private address from a public IP:port and a timestamp without querying the device, which is ideal for traceability-centric deployments, and it reduces traceability logs to almost zero.

Two log modes are supported per instance:

  • Port-block log (default, recommended): logs are generated only when a port block is allocated or released. The log volume is about 1/1000 of session-level logging.
  • Session-level log: one log per mapping/session creation and deletion, for fine-grained audit.

Logs contain at least: protocol, private source IP/port, public source IP/port (or port block range), remote IP/port, timestamp (millisecond-grade) and VRF identification. Logs can be exported via syslog (up to 8 servers) or IPFIX.

OperationCommandDescription
Enter the system configuration viewconfigure terminal-
Enable CGNAT globallycgnat enable
OperationCommandDescription
Enter the system configuration viewconfigure terminal-
Create a port allocation profilecgnat port-allocation-profile profile_nameEnter the port-allocation-profile view
Configure the block sizeblock-size block_sizeDefault 2048. Non-modifiable after the profile is referenced by a pool
Configure the block allocation modeblock-alloc-mode on-demand | pre-allocDefault on-demand. Non-modifiable after being referenced
Configure the port selection modeport-alloc-mode random | sequenceDefault random. Non-modifiable after being referenced
Configure the reserved port rangereserved-port port-start port-endDefault 0-1023. Non-modifiable after being referenced
Configure pre-allocated blocks per userprealloc-blocks-per-user numberOnly effective in pre-alloc mode. Non-modifiable after being referenced
Configure the block cooling timecooling-time secondRange 1-3600, default 120. Modifiable after being referenced
OperationCommandDescription
Enter the system configuration viewconfigure terminal-
Create an addresses poolcgnat addresses-pool pool_name [range ip-start ip-end|prefix ip/mask] port-allocation-profile profile_name
OperationCommandDescription
Enter the system configuration viewconfigure terminal-
Create a dynamic instancecgnat instance cgnat_name dynamic [vrf vrf_name]Enter the CGNAT instance view
Bind an address pooloutside-address-pool pool_nameMultiple pools can be bound. Non-modifiable after the instance is bound to an ACL
Configure the outside VRFoutside-vrf vrf_nameNon-modifiable after the instance is bound to an ACL
Configure the filtering modefiltering-mode eif | adf | apdfDefault eif
Enable hairpinninghairpining enableDisabled by default
Configure per-user limitsmax-user-blocks 1-256 / max-user-ports 1-65535 / max-user-sessions 1-10240 / max-user-create-sessions-rate 1-10240
Configure session agingaging-tcp-syn / aging-tcp-established / aging-tcp-fin-rst / aging-udp / aging-icmp 1-262144Defaults: 75 / 7440 / 60 / 300 / 60 seconds
Configure TCP MSS clampingtcp-mss 160-9540Optional
Configure the log modelog mode port-block | sessionDefault port-block
Disable syslog outputsyslog disableEnabled by default
Enable IPFIX exportipfix enable / ipfix collector ip port src ip portGlobal IPFIX configuration refers to the IPFIX CLI manual
OperationCommandDescription
Enter the system configuration viewconfigure terminal-
Create a deterministic instancecgnat instance cgnat_name deterministic [vrf vrf_name]Enter the instance view
Configure the inside address rangeinside-address [range ip-start ip-end|prefix ip/mask]Multiple ranges can be configured. Non-modifiable after the instance is bound to an ACL
Bind an address pool and other policiesoutside-address-pool, aging-*, static-mapping, etc.Same semantics as the dynamic instance
OperationCommandDescription
Enter the instance viewcgnat instance cgnat_name dynamic-
Configure an address-level static mappingstatic-mapping outside ipaddress inside ipaddress [protocol tcp|udp|icmp]Port unchanged; mappings are created lazily on traffic hits
Configure a port-level static mappingstatic-mapping outside ipaddress port inside ipaddress port protocol tcp|udp|icmpMapping created immediately
OperationCommandDescription
Configure an ACL (refer to the ACL configuration)e.g. permit source subscriber_prefixThe ACL only describes matching conditions
Create a CGNAT bindingcgnat binding binding_name cgnat_name acl_nameTraffic matching the ACL permit rules is diverted to the instance
Enter the inside interface view and apply the ACLinterface ethernet / acl acl_name / cgnat-zone inside
Enter the outside interface viewinterface ethernet / cgnat-zone outsideNo ACL needs to be applied on the outside interface; out2in uses full diversion with session lookup, and non-matching traffic is passed through transparently
OperationCommandDescription
Enter the system configuration viewconfigure terminal-
Enter the CGNAT syslog viewcgnat syslog
Configure a syslog serverserver number target-ip target_ip protocol tcp|udp port l4_port [src-ip src_ip | vrf vrf_name]Up to 8 servers
OperationCommandDescription
Display CGNAT-enabled interfaces and zonesshow cgnat interface-
Display instance configuration and bound ACL statusshow cgnat instance config [name instance_name]
Display CGNAT bindingsshow cgnat binding
Display port allocation profilesshow cgnat port-allocation-profile [name profile_name]
Display address poolsshow cgnat addresses-pool [config|resource-status [name pool_name]]
Display instance resource usageshow cgnat instance resource-status [name instance_name]Check port block exhaustion

Network requirements

Subscribers in the address range 100.64.1.0/24 are connected to Ethernet14 (inside zone). The device translates their traffic to the public pool 23.159.240.0/24 and forwards it through Ethernet15 (outside zone). Port blocks of 2048 ports are allocated on demand, and port-block logs are sent to the syslog server 192.0.2.200.

Configuration steps

  1. Configure basic IP addresses and enable CGNAT globally.
sonic(config)# interface ethernet 14
sonic(config-if-14)# ip address 100.64.1.1/24
sonic(config-if-14)# interface ethernet 15
sonic(config-if-15)# ip address 23.159.240.254/24
sonic(config)# cgnat enable
  1. Configure the ACL matching the subscriber range (refer to the ACL configuration for ACL rule syntax).
sonic(config)# access-list L3 acl1 ingress
sonic(config-L3-acl-acl1)# rule 1 src-ip 100.64.1.0/24 packet-action permit
  1. Configure the port allocation profile and the addresses pool.
sonic(config)# cgnat port-allocation-profile pprof1
sonic(config-cgnat-port-allocation-profile-pprof1)# block-size 2048
sonic(config-cgnat-port-allocation-profile-pprof1)# block-alloc-mode on-demand
sonic(config-cgnat-port-allocation-profile-pprof1)# port-alloc-mode random
sonic(config-cgnat-port-allocation-profile-pprof1)# reserved-port 0 1023
sonic(config-cgnat-port-allocation-profile-pprof1)# cooling-time 120
sonic(config)# cgnat addresses-pool pool1 range 23.159.240.1 23.159.240.253 port-allocation-profile pprof1
  1. Create the dynamic instance and configure its policies.
sonic(config)# cgnat instance cgnat1 dynamic
sonic(config-cgnat-instance-dynamic-cgnat1)# outside-address-pool pool1
sonic(config-cgnat-instance-dynamic-cgnat1)# filtering-mode eif
sonic(config-cgnat-instance-dynamic-cgnat1)# hairpining enable
sonic(config-cgnat-instance-dynamic-cgnat1)# max-user-blocks 4
sonic(config-cgnat-instance-dynamic-cgnat1)# max-user-ports 8192
sonic(config-cgnat-instance-dynamic-cgnat1)# max-user-sessions 4096
sonic(config-cgnat-instance-dynamic-cgnat1)# aging-udp 300
sonic(config-cgnat-instance-dynamic-cgnat1)# log mode port-block
  1. Create the binding and configure the interface zones.
sonic(config)# cgnat binding bind1 cgnat1 acl1
sonic(config)# interface ethernet 14
sonic(config-if-14)# acl acl1 priority 1
sonic(config-if-14)# cgnat-zone inside
sonic(config)# interface ethernet 15
sonic(config-if-15)# cgnat-zone outside
  1. Configure the syslog server (optional, for traceability).
sonic(config)# cgnat syslog
sonic(config-cgnat-syslog)# server 1 target-ip 192.0.2.200 protocol udp port 514

Verify configuration

sonic# show cgnat interface
sonic# show cgnat instance config cgnat1
sonic# show cgnat binding

Send traffic from a subscriber (e.g. 100.64.1.11) to a public server. The packet should be translated with its source rewritten to an address in 23.159.240.0/24 and a port within a 2048-port block, and return traffic should be translated back:

Before translation: 100.64.1.11:51234 -> 198.51.100.10:80
After translation: 23.159.240.1:4100 -> 198.51.100.10:80

Check resource usage and sessions on the device:

sonic# show cgnat instance resource-status name cgnat1

Port block allocation/release logs should be visible on the syslog server.