Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
The ISO/IEC 29341-26-10 standard defines the WANIPConnection:1 service, the most widely used UPnP service in the Internet Gateway Device (IGD) suite. This service is responsible for managing an IP-routed WAN connection, including connection lifecycle control, NAT port mapping administration, and exposure of connection parameters such as external IP address, DNS server configuration, and connection status. It is the service that peer-to-peer applications, game consoles, and VoIP phones interact with when they need to open ports through a consumer router’s firewall.
The service type URN is urn:schemas-upnp-org:service:WANIPConnection:1. It is embedded within a WANConnectionDevice instance and is mutually exclusive with WANPPPConnection:1 — a given WANConnectionDevice should expose either an IP-based connection service or a PPP-based one, depending on the underlying WAN technology.
The WANIPConnection:1 service implements a well-defined state machine that models the lifecycle of an IP connection. The ConnectionStatus state variable transitions through five distinct states: Unconfigured (initial state, no connection parameters set), Connecting (attempting to establish the connection), Connected (IP connectivity established), PendingDisconnect (disconnect requested, awaiting cleanup), and Disconnected (connection terminated). Control points can trigger state transitions via the ForceTermination and RequestConnection actions, though the latter is typically optional as most gateways manage connection establishment autonomously.
| Action | Arguments (In) | Arguments (Out) | Description |
|---|---|---|---|
| GetStatusInfo | None | NewConnectionStatus, NewLastConnectionError, NewUptime | Retrieves current connection status and uptime |
| GetNATRSIPStatus | None | NewRSIPAvailable, NewNATEnabled | Queries NAT and RSIP capabilities |
| GetGenericPortMappingEntry | NewPortMappingIndex (ui2) | NewRemoteHost, NewExternalPort, NewProtocol, … | Enumerates port mappings by index |
| AddPortMapping | NewRemoteHost, NewExternalPort, NewProtocol, NewInternalPort, NewInternalClient, NewEnabled, NewPortMappingDescription, NewLeaseDuration | None | Creates a new NAT port mapping |
| DeletePortMapping | NewRemoteHost, NewExternalPort, NewProtocol | None | Removes a port mapping |
| GetExternalIPAddress | None | NewExternalIPAddress | Returns the external (WAN-facing) IP address |
The NAT port mapping management capability is arguably the most critical feature of the service. Each port mapping is identified by a tuple of (RemoteHost, ExternalPort, Protocol). The AddPortMapping action creates an entry in the NAT table that forwards inbound packets matching the specified criteria to an internal host. The LeaseDuration parameter (in seconds) enables dynamic port mapping with automatic expiration — a crucial feature for applications that need temporary port access without leaving stale mappings in the NAT table.
RemoteHost parameter. When RemoteHost is an empty string, the mapping applies to all remote hosts (equivalent to a wildcard). However, some gateways reject empty-string remote hosts or fail to match inbound packets correctly when RemoteHost is specified. Implementations should treat an empty RemoteHost as an IPv4 address of 0.0.0.0 / CIDR prefix 0.0.0.0/0 to ensure correct NAT behaviour.Implementing the WANIPConnection:1 service efficiently requires careful architectural decisions. The port mapping table is a shared resource accessed by both the UPnP control path and the data-plane NAT engine. In Linux-based systems using netfilter/iptables or nftables, each AddPortMapping action translates to an iptables rule insertion, which can be a relatively expensive operation (millions of CPU cycles for rule set validation). For high-performance gateways, a batched update mechanism is recommended: accumulate UPnP port mapping changes over a short interval (e.g., 100 ms) and commit them to the kernel NAT table in a single atomic transaction.
The GetGenericPortMappingEntry action requires the device to maintain an indexed list of port mappings. The naive approach of parsing the kernel NAT table on each query is prohibitively slow when there are hundreds of mappings. A better design maintains a user-space shadow table that mirrors the kernel’s NAT state, updated via netlink event listeners. This shadow table supports O(1) indexed lookups and can be sorted by port number for consistent enumeration order.
Lease duration management is another important design consideration. The WANIPConnection:1 specification allows a LeaseDuration value of 0 to indicate an infinite/permanent lease. However, for security-conscious implementations, it is good practice to enforce a maximum lease duration (e.g., 7 days) even for infinite-lease requests, forcing applications to periodically renew their port mappings. This prevents orphaned mappings from accumulating when an application crashes without deleting its mappings.
GetGenericPortMappingEntry / AddPortMapping interface by mapping PCP opcodes to UPnP actions. This provides a unified NAT configuration API for both IPv4 and IPv6 traffic.GetGenericPortMappingEntry action returns an error code (730) when the requested index exceeds the number of available mappings.AddAnyPortMapping action that automatically selects an available external port if the requested one is occupied.AddPortMapping lease duration 0 and a non-zero value?