RFC 6887
Port Control Protocol (PCP)
Port Control Protocol (PCP) ๐ฆ
okay bestie, this is RFC 6887 and it's a whole vibe ๐ It's a Standards Track document, which means it's an official Internet standard that went through public review and got approved by the IESG. Here's the header squad:
- Who wrote it: D. Wing, Ed. (Cisco), S. Cheshire (Apple), M. Boucadair (France Telecom), R. Penno (Cisco), and P. Selkirk (ISC). The "Ed." means D. Wing was the editor ๐ซถ
- Category: Standards Track
- ISSN: 2070-1721
- Date: April 2013
- Obsoletes and updates: the header doesn't list anything here, so this RFC doesn't obsolete or update any other document according to this title block โจ
Abstract
okay so the whole point of PCP is that an IPv6 or IPv4 host can tell a NAT or a simple firewall what to do with incoming packets ๐ฅ It decides how they get translated and forwarded. It also lets the host make its outgoing NAT keepalive messages more efficient ๐
Status of This Memo
this part is the legal-ish bit, but it's iconic so let's go ๐
This is an Internet Standards Track document ๐
This document is a product of the Internet Engineering Task Force (IETF). It represents the consensus of the IETF community. It got public review and was approved for publication by the Internet Engineering Steering Group (IESG). If you want more info about Internet Standards, check out Section 2 of RFC 5741 ๐
For the current status of this document, any errata, and how to send feedback, go to http://www.rfc-editor.org/info/rfc6887 โจ
Copyright Notice
Copyright (c) 2013 IETF Trust and the persons identified as the document authors. All rights reserved ๐
This document is subject to BCP 78 and the IETF Trust's Legal Provisions Relating to IETF Documents (http://trustee.ietf.org/license-info) as they were in effect on the date this document was published. Please read these carefully, because they describe your rights and restrictions for this document ๐ Any Code Components pulled out of this document must include the Simplified BSD License text, as described in Section 4.e of the Trust Legal Provisions. They come with no warranty, as described in the Simplified BSD License โญ
Table of Contents ๐ง
1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . 4
2. Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1. Deployment Scenarios . . . . . . . . . . . . . . . . . . . 5
2.2. Supported Protocols . . . . . . . . . . . . . . . . . . . 5
2.3. Single-Homed Customer Premises Network . . . . . . . . . . 5
3. Terminology . . . . . . . . . . . . . . . . . . . . . . . . . 6
4. Relationship between PCP Server and Its PCP-Controlled
Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5. Note on Fixed-Size Addresses . . . . . . . . . . . . . . . . . 10
6. Protocol Design Note . . . . . . . . . . . . . . . . . . . . . 11
7. Common Request and Response Header Format . . . . . . . . . . 13
7.1. Request Header . . . . . . . . . . . . . . . . . . . . . . 14
7.2. Response Header . . . . . . . . . . . . . . . . . . . . . 15
7.3. Options . . . . . . . . . . . . . . . . . . . . . . . . . 16
7.4. Result Codes . . . . . . . . . . . . . . . . . . . . . . . 19
8. General PCP Operation . . . . . . . . . . . . . . . . . . . . 20
8.1. General PCP Client: Generating a Request . . . . . . . . . 21
8.1.1. PCP Client Retransmission . . . . . . . . . . . . . . 22
8.2. General PCP Server: Processing a Request . . . . . . . . . 24
8.3. General PCP Client: Processing a Response . . . . . . . . 25
8.4. Multi-Interface Issues . . . . . . . . . . . . . . . . . . 27
8.5. Epoch . . . . . . . . . . . . . . . . . . . . . . . . . . 27
9. Version Negotiation . . . . . . . . . . . . . . . . . . . . . 29
10. Introduction to MAP and PEER Opcodes . . . . . . . . . . . . . 30
10.1. For Operating a Server . . . . . . . . . . . . . . . . . . 33
10.2. For Operating a Symmetric Client/Server . . . . . . . . . 35
10.3. For Reducing NAT or Firewall Keepalive Messages . . . . . 37
10.4. For Restoring Lost Implicit TCP Dynamic Mapping State . . 38
11. MAP Opcode . . . . . . . . . . . . . . . . . . . . . . . . . . 39
11.1. MAP Operation Packet Formats . . . . . . . . . . . . . . . 40
11.2. Generating a MAP Request . . . . . . . . . . . . . . . . . 43
11.2.1. Renewing a Mapping . . . . . . . . . . . . . . . . . . 44
11.3. Processing a MAP Request . . . . . . . . . . . . . . . . . 44
11.4. Processing a MAP Response . . . . . . . . . . . . . . . . 48
11.5. Address Change Events . . . . . . . . . . . . . . . . . . 49
11.6. Learning the External IP Address Alone . . . . . . . . . . 50
12. PEER Opcode . . . . . . . . . . . . . . . . . . . . . . . . . 50
12.1. PEER Operation Packet Formats . . . . . . . . . . . . . . 51
12.2. Generating a PEER Request . . . . . . . . . . . . . . . . 54
12.3. Processing a PEER Request . . . . . . . . . . . . . . . . 55
12.4. Processing a PEER Response . . . . . . . . . . . . . . . . 56
13. Options for MAP and PEER Opcodes . . . . . . . . . . . . . . . 57
13.1. THIRD_PARTY Option for MAP and PEER Opcodes . . . . . . . 57
13.2. PREFER_FAILURE Option for MAP Opcode . . . . . . . . . . . 59
13.3. FILTER Option for MAP Opcode . . . . . . . . . . . . . . . 61
14. Rapid Recovery . . . . . . . . . . . . . . . . . . . . . . . . 63
14.1. ANNOUNCE Opcode . . . . . . . . . . . . . . . . . . . . . 64
14.1.1. ANNOUNCE Operation . . . . . . . . . . . . . . . . . . 65
14.1.2. Generating and Processing a Solicited ANNOUNCE
Message . . . . . . . . . . . . . . . . . . . . . . . 65
14.1.3. Generating and Processing an Unsolicited ANNOUNCE
Message . . . . . . . . . . . . . . . . . . . . . . . 66
14.2. PCP Mapping Update . . . . . . . . . . . . . . . . . . . . 67
15. Mapping Lifetime and Deletion . . . . . . . . . . . . . . . . 69
15.1. Lifetime Processing for the MAP Opcode . . . . . . . . . . 71
16. Implementation Considerations . . . . . . . . . . . . . . . . 72
16.1. Implementing MAP with EDM Port-Mapping NAT . . . . . . . . 72
16.2. Lifetime of Explicit and Implicit Dynamic Mappings . . . . 72
16.3. PCP Failure Recovery . . . . . . . . . . . . . . . . . . . 72
16.3.1. Recreating Mappings . . . . . . . . . . . . . . . . . 73
16.3.2. Maintaining Mappings . . . . . . . . . . . . . . . . . 73
16.3.3. SCTP . . . . . . . . . . . . . . . . . . . . . . . . . 74
16.4. Source Address Replicated in PCP Header . . . . . . . . . 75
16.5. State Diagram . . . . . . . . . . . . . . . . . . . . . . 76
17. Deployment Considerations . . . . . . . . . . . . . . . . . . 77
17.1. Ingress Filtering . . . . . . . . . . . . . . . . . . . . 77
17.2. Mapping Quota . . . . . . . . . . . . . . . . . . . . . . 77
18. Security Considerations . . . . . . . . . . . . . . . . . . . 78
18.1. Simple Threat Model . . . . . . . . . . . . . . . . . . . 78
18.1.1. Attacks Considered . . . . . . . . . . . . . . . . . . 79
18.1.2. Deployment Examples Supporting the Simple Threat
Model . . . . . . . . . . . . . . . . . . . . . . . . 79
18.2. Advanced Threat Model . . . . . . . . . . . . . . . . . . 80
18.3. Residual Threats . . . . . . . . . . . . . . . . . . . . . 80
18.3.1. Denial of Service . . . . . . . . . . . . . . . . . . 80
18.3.2. Ingress Filtering . . . . . . . . . . . . . . . . . . 81
18.3.3. Mapping Theft . . . . . . . . . . . . . . . . . . . . 81
18.3.4. Attacks against Server Discovery . . . . . . . . . . . 81
19. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 82
19.1. Port Number . . . . . . . . . . . . . . . . . . . . . . . 82
19.2. Opcodes . . . . . . . . . . . . . . . . . . . . . . . . . 82
19.3. Result Codes . . . . . . . . . . . . . . . . . . . . . . . 82
19.4. Options . . . . . . . . . . . . . . . . . . . . . . . . . 82
20. Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . 83
21. References . . . . . . . . . . . . . . . . . . . . . . . . . . 84
21.1. Normative References . . . . . . . . . . . . . . . . . . . 84
21.2. Informative References . . . . . . . . . . . . . . . . . . 84
Appendix A. NAT-PMP Transition . . . . . . . . . . . . . . . . . . 87
1. Introduction โจ
okay so PCP gives you a way to control how incoming packets get forwarded by the devices sitting upstream of you ๐ฆ Think Network Address Translator IPv6/IPv4 (NAT64), Network Address Translator IPv4/IPv4 (NAT44), and IPv6 and IPv4 firewalls. It also has a way to cut down on application keepalive traffic ๐ PCP is built for Carrier-Grade NATs (CGNs) and small NATs (like residential ones ๐ ), dual-stack and IPv6-only Customer Premises Equipment (CPE) routers, and every transition scenario we currently know about for getting to IPv6-only CPE routers ๐
PCP lets hosts run servers for a long time (like a network-attached home security camera ๐ท) or for a short time (like while you're playing a game or on a phone call ๐ฎ๐). This works even when the host is behind a NAT device. That includes being behind a CGN run by their Internet service provider, or behind an IPv6 firewall built into their CPE router ๐ซถ
PCP lets applications create mappings from an external IP address, protocol, and port to an internal IP address, protocol, and port ๐ These mappings are required for inbound communications to reach machines sitting behind a NAT or a firewall, so they're kind of a big deal ๐
After you make a mapping for incoming connections, you still need to tell remote computers the IP address, protocol, and port for that incoming connection ๐ง This usually gets done in an application-specific way. For example, a computer game might use a rendezvous server built for that game (or for that game developer). A SIP phone would use a SIP proxy. A client using DNS-Based Service Discovery [RFC6763] would use DNS Update [RFC2136] [RFC3007] ๐ PCP does not do this rendezvous part itself ๐ The rendezvous function may support IPv4, IPv6, or both. Depending on that support and on whether the application supports IPv4 or IPv6, the PCP client may need an IPv4 mapping, an IPv6 mapping, or both ๐
Lots of NAT-friendly applications send frequent application-level messages so that their session doesn't get timed out by a NAT ๐ These are commonly called "NAT keepalive" messages, even though they aren't sent to the NAT itself. They're actually sent 'through' the NAT ๐ธ Applications can send these less often by using PCP to learn (and influence) the NAT mapping lifetime. That helps cut bandwidth on the subscriber's access network, traffic to the server, and battery drain on mobile devices ๐โก
Lots of NATs and firewalls include Application Layer Gateways (ALGs) that create mappings for applications which set up extra streams or accept incoming connections ๐ ALGs built into NATs may also change the application payload ๐ญ Industry experience has shown that these ALGs are bad for protocol evolution ๐ PCP lets an application make its own mappings in NATs and firewalls, which means there's less reason to deploy ALGs in them ๐โจ
2. Scope โจ
2.1. Deployment Scenarios โจ
okay so PCP can show up in a bunch of different deployment scenarios, and it's giving everywhere ๐๐
- Basic NAT [RFC3022]
- Network Address and Port Translation [RFC3022], such as commonly deployed in residential NAT devices ๐
- Carrier-Grade NAT [RFC6888]
- Dual-Stack Lite (DS-Lite) [RFC6333]
- NAT that is Layer-2 Aware [L2NAT] ๐ง
- Dual-Stack Extra Lite [RFC6619]
- NAT64, both Stateless [RFC6145] and Stateful [RFC6146]
- IPv4 and IPv6 simple firewall control [RFC6092] ๐ฅ
- IPv6-to-IPv6 Network Prefix Translation (NPTv6) [RFC6296] ๐ฆ
2.2. Supported Protocols ๐
bestie, the PCP Opcodes in this document are built to support transport-layer protocols that use a 16-bit port number ๐. That's stuff like TCP, UDP, Stream Control Transmission Protocol (SCTP) [RFC4960], and Datagram Congestion Control Protocol (DCCP) [RFC4340].
Now the protocols that don't use a port number are a little different, ngl ๐. Think Resource Reservation Protocol (RSVP), IP Encapsulating Security Payload (ESP) [RFC4303], ICMP, and ICMPv6. Those ones are supported for IPv4 firewall, IPv6 firewall, and NPTv6 functions, but they're out of scope for any NAT functions. Not me saying "out of scope" like it's a bad thing, it's just a boundary ๐
2.3. Single-Homed Customer Premises Network ๐ก
okay this one is important, so pay attention ๐. PCP assumes a single-homed IP address model. What that means is that for a given IP address of a host, there's only one default route to reach other hosts on the Internet from that source IP address.
Why does this matter so much? Well, once a PCP mapping is created and an inbound packet (e.g., TCP SYN) gets rewritten and delivered to a host, the outbound response (e.g., TCP SYNACK) has to take the same (reverse) path. That way it goes through the same NAT and gets the necessary inverse rewrite performed ๐. It's literally the only way the handshake stays iconic ๐ญ
This restriction exists because otherwise you'd need a PCP-enabled NAT for every egress ๐, since the host couldn't reliably tell which egress path its packets would take. On top of that, the client would need to be able to reliably make the same internal/external mapping in every NAT gateway. In general that's not possible, because the other NATs might already have the necessary external port mapped to another host ๐ โโ๏ธ. Not a vibe.
3. Terminology โจ
Okay bestie, listen up ๐ง. The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this doc mean exactly what they're defined to mean in "Key words for use in RFCs to Indicate Requirement Levels" [RFC2119]. No vibes-based interpretation allowed ๐ .
Internal Host: A host that's served by a NAT gateway or protected by a firewall. This is the host that gets the incoming traffic from a PCP mapping request, or the host that started an implicit dynamic outbound mapping (e.g., by sending a TCP SYN) across a firewall or a NAT. Main character energy ๐.
Remote Peer Host: A host that an internal host is talking to. This can also be another internal host (or even the same internal host). If a NAT is involved, the NAT would need to hairpin the traffic [RFC4787] ๐.
Internal Address: The address of an internal host that's served by a NAT gateway or protected by a firewall ๐ .
External Address: The address of an internal host as seen by the remote peers on the Internet it's talking to, after any NAT gateways on the path have done their translation thing. An external address is generally a public routable (i.e., non-private) address. If an internal host is protected by a pure firewall with no address translation on the path, its external address is the same as its internal address ๐.
Endpoint-Dependent Mapping (EDM): This is a term for NAT operation where an implicit mapping created by outgoing traffic (e.g., TCP SYN) from a single internal address, protocol, and port to different remote peers and ports may get assigned different external ports. A later PCP mapping request for that same internal address, protocol, and port may get assigned yet another different external port. Chaotic but real ๐ญ. This term covers both Address-Dependent Mapping and Address and Port-Dependent Mapping [RFC4787].
Endpoint-Independent Mapping (EIM): This is a term for NAT operation where all mappings from a single internal address, protocol, and port to different remote peers and ports all get the same external address and port. Way more stable, iconic ๐ง.
Remote Peer Address: The address of a remote peer, as seen by the internal host. A remote address is generally a publicly routable address. If the remote peer is itself served by a NAT gateway, the remote address may actually be the remote peer's external address. But since this remote translation is usually invisible to software running on the internal host, the distinction can safely be ignored for the purposes of this document ๐.
Third Party: In the common case, an internal host manages its own mappings using PCP requests, and the internal address of those mappings is the same as the source IP address of the PCP request packet.
In the case where one device is managing mappings on behalf of some other device that doesn't implement PCP, the presence of the THIRD_PARTY option in the MAP request signifies that the specified address, rather than the source IP address of the PCP request packet, should be used as the internal address for the mapping ๐ซถ.
Mapping, Port Mapping, Port Forwarding: A NAT mapping creates a relationship between an internal IP address, protocol, and port, and an external IP address, protocol, and port. More specifically, it creates a translation rule. Packets destined to the external IP address, protocol, and port have their destination address and port translated to the internal address and port. And the other way around: packets from the internal IP address, protocol, and port have their source address and port translated to the external address and port ๐.
In the case of a pure firewall, the "mapping" is the identity function, translating an internal IP address, protocol, and port number to the same external IP address, protocol, and port number. Firewall filtering, applied in addition to that identity mapping function, is separate from the mapping itself ๐งฑ.
Mapping Types: There are three dimensions to classifying mapping types: how they're created (implicitly/explicitly), their primary purpose (outbound/inbound), and how they're deleted (dynamic/static). Implicit mappings are created as a side effect of some other operation. Explicit mappings are created by a mechanism that explicitly deals with mappings. Outbound mappings exist mainly to facilitate outbound communication. Inbound mappings exist mainly to facilitate inbound communication. Dynamic mappings get deleted when their lifetime expires, or through other protocol action. Static mappings are permanent until the user chooses to delete them ๐.
- Implicit dynamic mappings are created implicitly as a side effect of traffic such as an outgoing TCP SYN or outgoing UDP packet. Those packets weren't originally designed to create NAT (or firewall) state, but they can have that effect when they pass through a NAT (or firewall) device. Implicit dynamic mappings usually have a finite lifetime, though the client using them generally doesn't know what that lifetime is ๐คทโโ๏ธ.
- Explicit dynamic mappings are created as a result of explicit PCP MAP and PEER requests. Like a DHCP address lease, explicit dynamic mappings have a finite lifetime, and this lifetime is communicated to the client. Also like a DHCP address lease, if the client wants a mapping to persist, the client must prove that it's still present by periodically renewing the mapping so it doesn't expire. If a PCP client goes away, any mappings it created will be automatically cleaned up when they expire ๐งน.
- Explicit static mappings are created by manual configuration (e.g., via command-line interface or another user interface) and persist until the user changes that manual configuration ๐.
Both implicit and explicit dynamic mappings are dynamic in the sense that they're created on demand, as requested (implicitly or explicitly) by the internal host, and they have a lifetime. After the lifetime, the mapping is deleted unless the lifetime is extended by action from the internal host (e.g., sending more traffic or sending another PCP request) โณ.
Static mappings are, by their nature, always explicit. Static mappings differ from explicit dynamic mappings in that their lifetime is effectively infinite (they exist until manually removed). Otherwise, they behave exactly the same as explicit MAP mappings ๐.
While all mappings are, by necessity, bidirectional (most Internet communication requires information to flow in both directions to work), it can be helpful to loosely identify them by their 'primary' purpose:
- Outbound mappings exist primarily to enable outbound communication. For example, when a host calls
connect()to make an outbound connection, a NAT gateway will create an implicit dynamic outbound mapping to facilitate that outbound communication ๐. - Inbound mappings exist primarily to enable listening servers to receive inbound connections. Generally, when a client calls
listen()to listen for inbound connections, a NAT gateway will not implicitly create any mapping to facilitate that inbound communication. A PCP MAP request can be used explicitly to create a dynamic inbound mapping to enable the desired inbound communication ๐ฅ.
Explicit static (manual) mappings and explicit dynamic (MAP) mappings both let internal hosts receive inbound traffic that isn't in direct response to any immediately preceding outbound communication. In other words, they let internal hosts run a "server" that other hosts on the Internet can reach ๐ฅ๏ธโจ.
PCP Client: A PCP software instance responsible for issuing PCP requests to a PCP server. Several independent PCP clients can exist on the same host, and several PCP clients can be located in the same local network. A PCP client can issue PCP requests on behalf of a third-party device it's authorized to act for. An interworking function from Universal Plug and Play Internet Gateway Device (UPnP IGDv1 [IGDv1]) to PCP is another example of a PCP client. A PCP server in a NAT gateway that is itself a client of another NAT gateway (nested NAT) may itself act as a PCP client to the upstream NAT ๐งฉ.
PCP-Controlled Device: A NAT or firewall that controls or rewrites packet flows between internal hosts and remote peer hosts. PCP manages the mappings on this device ๐๏ธ.
PCP Server: A PCP software instance that lives on the PCP-Controlled Device, receives PCP requests from the PCP client, and creates appropriate state in response to each request ๐ง .
Subscriber: The unit of billing for a commercial ISP. A subscriber may have a single IP address from the commercial ISP, which can be shared among multiple hosts using a NAT gateway so they look like a single host to the ISP. Or a subscriber may have multiple IP addresses provided by the commercial ISP. Either way, the IP address or addresses the ISP provides may themselves be further translated by a Carrier-Grade NAT (CGN) operated by the ISP ๐งพ.
4. Relationship between PCP Server and Its PCP-Controlled Device ๐
The PCP server receives and responds to PCP requests. The PCP server functionality is typically a capability of a NAT or firewall device, as shown in Figure 1. It's also possible for the PCP functionality to be provided by some other device, which talks to the actual NAT(s) or firewall(s) via some other proprietary mechanism, as long as from the PCP client's perspective, that split setup is indistinguishable from the integrated one ๐ช.
+-----------------+
+------------+ | NAT or firewall |
| PCP client |-<network>-+ with +---<Internet>
+------------+ | PCP server |
+-----------------+
Figure 1: PCP-Enabled NAT or Firewall
A NAT or firewall device, sitting between the PCP client and the Internet, might implement simple or advanced firewall functionality. This can be a side effect of the technology the device uses (e.g., a network address and port translator, because of its port rewriting, normally requires connections to be initiated from an inside host towards the Internet). Or it might be an explicit firewall policy to deny unsolicited traffic from the Internet ๐ฅ.
Some firewall devices deny certain unsolicited traffic from the Internet (e.g., TCP, UDP to most ports) but allow certain other unsolicited traffic from the Internet (e.g., UDP port 500 and IP ESP) [RFC6092]. Such default filtering (or lack thereof) is out of scope of PCP itself ๐ โโ๏ธ.
If a client device wants to receive traffic and supports PCP, and doesn't have prior knowledge of such default filtering policy, it SHOULD use PCP to request the necessary mappings to receive the desired traffic ๐ฆ.
5. Note on Fixed-Size Addresses โจ
okay so bestie, PCP keeps things simple by always using fixed-size 128-bit IP address fields ๐ for both IPv6 and IPv4 addresses. Same-size slots make building and parsing packets way less messy ๐
When the field holds an IPv6 address, it's just the IPv6 address stored as is. Easy peasy ๐ฆ
When the field holds an IPv4 address, PCP uses an IPv4-mapped IPv6 address [RFC4291] from ::ffff:0:0/96. The first 80 bits are zero, the next 16 bits are one, and the last 32 bits hold the IPv4 address ๐ธ This is totally distinguishable from a native IPv6 address, because an IPv4-mapped IPv6 address [RFC4291] would not be valid for a mapping, so nobody's getting confused ๐
When you're checking for an IPv4-mapped IPv6 address, all of the first 96 bits MUST be checked for the pattern. Checking only bits 81-96 for ones is NOT enough. Don't be that person ๐
The all-zeros IPv6 address MUST be written by filling the whole 128-bit field with zeros, which looks like :: ๐ง
The all-zeros IPv4 address MUST be written as 80 bits of zeros, then 16 bits of ones, then 32 bits of zeros, which looks like ::ffff:0:0 ๐
6. Protocol Design Note ๐
okay this part is lowkey my favourite ๐ญ PCP can be seen as a regular request/response protocol, just like a bunch of other UDP-based request/response protocols, and it works perfectly fine implemented that way ๐ But you can also think of it as a hint/notification protocol, and that little mindset shift can make implementations way simpler ๐ง โจ
Instead of insisting that every response is tied to exactly one request, you can picture the messages between the PCP client and PCP server as two fairly independent streams going in opposite directions:
- A stream of hints flowing from PCP client to PCP server, where the client tells the server what it would like the state of its mappings to be, and
- A stream of notifications flowing from PCP server to PCP client, where the server tells the clients what the state of its mappings actually is.
Honestly, some of this is needed anyway in a UDP-based request/response protocol, since UDP packets can get lost, duplicated, or reordered ๐
So in this view, the client sends hints at various intervals saying what it wants, and the server sends notifications saying what's really going on with its mappings ๐ซถ The two streams are only loosely connected. A client request (hint) usually gets a server response (notification), but not always: a request can get no response if a packet is lost, and a server response can show up out of nowhere with no request right before it. That second case happens when a server configuration change, like a new external IP address on a NAT gateway, changes a mapping's state ๐
The timing of client requests comes from a client timing state machine, which cares about two things. (i) Has the client not yet got a response to a previous request? That's a retransmission. (ii) Has the client already got a response saying how long the mapping will stay active? That's a renewal. This is why PCP's retransmissions and renewals are exactly the same packet on the wire โจ Retransmissions usually back off with exponentially increasing intervals while the client waits for an answer, and renewals usually come with exponentially decreasing intervals as the expiry time gets closer. But from the server's point of view both packets are identical, and both just say "I want this mapping to exist (or keep existing)" ๐ฆ
A PCP server usually replies because a client asked, but not always. If a server is too overloaded to respond, it's allowed to quietly ignore a request and let the client retransmit ๐ And if outside factors change a NAT gateway's or firewall's configuration, the PCP server can send unsolicited responses to clients to tell them the new state of their mappings. Those reconfigurations are expected to be rare, since they can be disruptive for clients, but if they happen, PCP gives servers a way to pass the news along promptly, without waiting for the next periodic renewal request ๐
This design goal is also why PCP request and response messages have no transaction ID ๐ โโ๏ธ A transaction ID would be unnecessary, and it would limit the protocol and make implementations more complicated for no reason. A PCP server response (a.k.a. a notification) is self-describing and complete. It says the internal and external addresses, protocol, and ports for a mapping, plus its remaining lifetime ๐ If the client actually wants that mapping, it can match it up using the internal address, protocol, and port, then update its state with the current external address, port, and remaining lifetime. If the client doesn't want that mapping, it can safely ignore the message. No client action is required for unexpected mapping notifications ๐ธ
Also, in today's world a NAT gateway can have a static mapping that the client device has no explicit knowledge of, and no way to change ๐คทโโ๏ธ And a client device can be connected straight to the public Internet with a globally routable IP address, in which case it effectively has "mappings" for all of its listening ports. That device has to take care of its own security ๐ It can't count on some other network device blocking all incoming packets, periodt.
7. Common Request and Response Header Format ๐
All PCP messages go over UDP, with a maximum UDP payload length of 1100 octets ๐ฆ Every PCP message has a request or response header, which contains an Opcode, any Opcode-specific info that's relevant, and zero or more options. Any numeric value bigger than a single octet (result codes, lifetimes, Epoch times, and so on) is written in conventional IETF network order, which means most significant octet first ๐ข Non-numeric stuff is written as is on every platform, with no byte swapping. IP addresses and ports go into PCP messages the same way they go into IP or TCP headers ๐ง
The layout of the common header, and how the PCP client and PCP server behave, are covered in the next sections. Everything in this section applies to all Opcodes. The behaviour of the Opcodes defined in this document is described in Sections 10, 11, and 12 โจ
7.1. Request Header ๐
All requests look like this:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Version = 2 |R| Opcode | Reserved |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Requested Lifetime (32 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| PCP Client's IP Address (128 bits) |
| |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
: :
: (optional) Opcode-specific information :
: :
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
: :
: (optional) PCP Options :
: :
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Figure 2: Common Request Packet Format
Here's what each field means ๐ซถ
Version: This document specifies protocol version 2. PCP clients and servers that follow this document use the value 2 ๐ This field is used for version negotiation, as described in Section 9.R: Indicates Request (0) or Response (1) ๐Opcode: A 7-bit value that specifies the operation to be performed. MAP and PEER Opcodes are defined in Sections 11 and 12 ๐บ๏ธReserved: 16 reserved bits. These MUST be zero on transmission and MUST be ignored on reception ๐คซRequested Lifetime: An unsigned 32-bit integer, in seconds, ranging from 0 to 2^32-1 seconds โณ This is used by the MAP and PEER Opcodes defined in this document for their requested lifetime.PCP Client's IP Address: The source IPv4 or IPv6 address in the IP header that the PCP client used when it sent this PCP request ๐ An IPv4 address is represented using an IPv4-mapped IPv6 address. The PCP Client IP Address in the PCP message header is used to detect an unexpected NAT on the path between the PCP client and the PCP-controlled NAT or firewall device. See Section 8.1 ๐ต๏ธโโ๏ธOpcode-specific information: Payload data for this Opcode. The length of this data is determined by the Opcode definition ๐ฆPCP Options: Zero, one, or more options that are legal for both a PCP request and for this Opcode. See Section 7.3 ๐
7.2. Response Header ๐
okay so every response wears the same outfit, and it's giving organized ๐ Here's the fit:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Version = 2 |R| Opcode | Reserved | Result Code |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Lifetime (32 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Epoch Time (32 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Reserved (96 bits) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
: :
: (optional) Opcode-specific response data :
: :
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
: (optional) Options :
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Figure 3: Common Response Packet Format
Here's the breakdown of each field, bestie ๐ซถ
- Version: Responses from servers compliant with this specification MUST use version 2. This is set by the server.
- R: Indicates Request (0) or Response (1). All Responses MUST use 1. This is set by the server.
- Opcode: The 7-bit Opcode value. The server copies this value from the request.
- Reserved: 8 reserved bits, MUST be sent as 0, MUST be ignored when received. This is set by the server.
- Result Code: The result code for this response. See Section 7.4 for values. This is set by the server.
- Lifetime: An unsigned 32-bit integer, in seconds, ranging from 0 to 2^32-1 seconds. On an error response, this indicates how long clients should assume they'll get the same error response from that PCP server if they repeat the same request. On a success response for the PCP Opcodes that create a mapping (MAP and PEER), the Lifetime field indicates the lifetime for this mapping. This is set by the server.
- Epoch Time: The server's Epoch Time value. See Section 8.5 for discussion. This value is set by the server, in both success and error responses.
- Reserved: 96 reserved bits. For requests that were successfully parsed, this MUST be sent as 0, MUST be ignored when received. This is set by the server. For requests that were not successfully parsed, the server copies the last 96 bits of the PCP Client's IP Address field from the request message into this corresponding 96-bit field of the response.
- Opcode-specific information: Payload data for this Opcode. The length of this data is determined by the Opcode definition.
- PCP Options: Zero, one, or more options that are legal for both a PCP response and for this Opcode. See Section 7.3.
7.3. Options ๐
a PCP Opcode can be extended with one or more options, and options can show up in both requests and responses. The design choice about whether a piece of info goes in the base Opcode format or in an option was a trade-off between packet size and code complexity. ngl, it's a very real engineering vibe โ๏ธ
Info that's pretty much always needed goes in the fixed Opcode data. That makes the code simpler to build and parse, because the field always lives in the same spot, but it wastes space whenever that field is all zeros because nobody needed it. Info that's needed less often goes in an option. That makes the code a little more complicated, but it saves space when the info isn't needed. Bonus: an implementation that never uses that info doesn't even have to write code for it. For example, a client that never requests mappings on behalf of some other device doesn't need to implement code to generate the THIRD_PARTY option, and a PCP server that doesn't implement the necessary security measures to create third-party mappings safely doesn't need to implement code to parse the THIRD_PARTY option. main character energy ๐ฆ
Options use the following Type-Length-Value format:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Option Code | Reserved | Option Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
: (optional) Data :
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Figure 4: Options Header
The fields go like this ๐ธ
- Option Code: 8 bits. Its most significant bit indicates if this option is mandatory (0) or optional (1) to process.
- Reserved: 8 bits. MUST be set to 0 on transmission and MUST be ignored on reception.
- Option Length: 16 bits. Indicates the length of the enclosed data, in octets. Options with length of 0 are allowed. Options that are not a multiple of 4 octets long are followed by one, two, or three 0 octets to pad their effective length in the packet to be a multiple of 4 octets. The Option Length reflects the semantic length of the option, not including any padding octets.
- Data: Option data.
If several options are included in a PCP request, they MAY be encoded in any order by the PCP client, but MUST be processed by the PCP server in the order in which they appear. It's the PCP client's job to make sure the server has enough room to reply without going over the 1100-octet size limit. If the reply would be too big, the server generates an error ๐
If, while processing a PCP request (options included), an error shows up that causes a PCP error response to be generated, the PCP request MUST cause no state change in the PCP server or the PCP-controlled device. In other words, it rolls back any tentative changes it might have made while processing the request. Such an error response MUST consist of a complete copy of the request packet, with the error code and other appropriate fields set in the header. no undo button left behind ๐งน
An option MAY appear more than once in a request or in a response, if the option's definition permits it. If the definition only allows the option to appear once, but it shows up more than once in a request, and the PCP server understands the option, the PCP server MUST respond with the MALFORMED_OPTION result code. If the PCP server runs into an invalid option (for example, the PCP option length is longer than the UDP packet length), the error MALFORMED_OPTION SHOULD be returned, rather than MALFORMED_REQUEST, because that helps the client understand how the packet was malformed. If a PCP response would have exceeded the maximum PCP message size, the PCP server SHOULD respond with MALFORMED_REQUEST.
If the overall option structure of a request can't be parsed successfully (for example, a nonsensical option length), the PCP server MUST generate an error response with code MALFORMED_OPTION. ๐
If the overall option structure of a request is valid, then how each individual option gets handled depends on the most significant bit in the option code. If that bit is set, handling the option is optional, and a PCP server MAY process or ignore it, totally at its discretion. If that bit is clear, handling the option is mandatory. In that case, a PCP server MUST return the error MALFORMED_OPTION if the option contents are malformed, or UNSUPP_OPTION if the option is unrecognized, unimplemented, or disabled, or if the client isn't authorized to use the option. In error responses, all options are returned. In success responses, all processed options are included and unprocessed options are not included.
Because the PCP client can't reject a response containing an Option, PCP clients MUST ignore Options they don't understand that appear in responses, including Options in the mandatory-to-process range. That said, if a client explicitly requests an Option where getting it right requires processing the Option data in the response, that client SHOULD implement code to do that. โจ
Different options are valid for different Opcodes. For example:
- The
THIRD_PARTYoption is valid for both MAP and PEER Opcodes. - The
FILTERoption is valid only for the MAP Opcode (for the PEER Opcode it would have no meaning). - The
PREFER_FAILUREoption is valid only for the MAP Opcode (for the PEER Opcode, similar semantics are automatically implied).
7.4. Result Codes ๐
Okay bestie, these are the result codes a PCP server can send back for any Opcode ๐ซถ The only success is 0, and everything else is an error. If a server runs into several errors while handling one request, it SHOULD go with the most specific error message ๐ฏ Every error is either a "long lifetime" or a "short lifetime" one, which is just a hint for server devs about what to put in the Lifetime field. It is RECOMMENDED that short lifetime errors use a 30-second lifetime and long lifetime errors use a 30-minute lifetime โฐ
0SUCCESS: Success, all good โจ1UNSUPP_VERSION: the version number at the start of the PCP Request header isn't recognized by this server. Long lifetime error. This document describes PCP version 2 ๐2NOT_AUTHORIZED: the operation is disabled for this PCP client, or the client asked for something the server's security policy won't allow. Long lifetime error ๐3MALFORMED_REQUEST: the request couldn't be parsed. Long lifetime error ๐4UNSUPP_OPCODE: unsupported Opcode. Long lifetime error5UNSUPP_OPTION: unsupported option, but only if the option is in the mandatory-to-process range. Long lifetime error6MALFORMED_OPTION: malformed option, like it shows up too many times or has an invalid length. Long lifetime error7NETWORK_FAILURE: the server, or the device it controls, is having some kind of network trouble (e.g., it hasn't gotten an external IP address yet). Short lifetime error ๐ง๏ธ8NO_RESOURCES: the request is well-formed and valid, but the server doesn't have enough resources to do it right now. For example, the NAT device can't make more mappings at the moment, is short on CPU or memory, or can't handle the request because of some other temporary situation. The same request may work in the future. This is a system-wide error, different fromUSER_EX_QUOTA. It can be the catch-all if no other error message fits. Short lifetime error ๐งบ9UNSUPP_PROTOCOL: unsupported transport protocol, e.g., SCTP in a NAT that only handles UDP and TCP. Long lifetime error10USER_EX_QUOTA: this attempt to create a new mapping would go over this subscriber's port quota. Short lifetime error ๐11CANNOT_PROVIDE_EXTERNAL: the suggested external port and/or external address can't be provided. This error MUST only be returned for:- MAP requests that included the PREFER_FAILURE option (normal MAP requests will return an available external port)
- MAP requests for the SCTP protocol (PREFER_FAILURE is implied)
- PEER requests
See Section 13.2 for details of the PREFER_FAILURE Option. The error lifetime depends on the reason for the failure ๐ค
12ADDRESS_MISMATCH: the source IP address of the request packet doesn't match the PCP Client's IP Address field, because of an unexpected NAT on the path between the PCP client and the PCP-controlled NAT or firewall. Long lifetime error ๐ช13EXCESSIVE_REMOTE_PEERS: the server wasn't able to create the filters in this request. This result code MUST only be returned if the MAP request contained the FILTER option. See Section 13.3 for details of the FILTER Option. Long lifetime error ๐ซ
8. General PCP Operation ๐ธ
PCP messages MUST be sent over UDP [RFC0768]. Every PCP request gets at least one response, so PCP doesn't need a reliable transport protocol ๐ฆ
When a server gets multiple identical requests, it will usually send back identical responses, unless its state changed between those requests because of other activity ๐ For example, a request could show up while the PCP-controlled device has no mappings available, so the server sends an error response. Then if mappings become available and another copy of that same request arrives (maybe it got duplicated somewhere in the network), the server will allocate a mapping and send a non-error response. A PCP client MUST handle such updated responses for any request it sends, most notably to support rapid recovery (Section 14) ๐ซ Also see the Protocol Design Note (Section 6).
8.1. General PCP Client: Generating a Request ๐
This section covers the stuff specific to a PCP client, for any Opcode. The procedures for the MAP Opcode are in Section 11, and the procedures for the PEER Opcode are in Section 12.
Before sending its first PCP message, the PCP client has to figure out which server to use ๐บ๏ธ It does the following steps to find its PCP server:
- if a PCP server is configured (e.g., in a configuration file or via DHCP), that single configuration source is used as the list of PCP server(s), else
- the default router list (for IPv4 and IPv6) is used as the list of PCP server(s). So if a PCP client has both an IPv4 and IPv6 address, it will have an IPv4 PCP server (its IPv4 default router) for its IPv4 mappings, and an IPv6 PCP server (its IPv6 default router) for its IPv6 mappings ๐ง
For the purposes of this document, only a single PCP server address is supported. If future specifications define configuration methods that give a longer list of PCP server addresses, those specifications will define how clients pick one or more addresses from that list ๐
With that PCP server address, the PCP client builds its PCP request. The request contains a PCP common header, a PCP Opcode and payload, and (possibly) options. Like with all UDP client software on any operating system, when several independent PCP clients live on the same host, each one uses a distinct source port number so requests and replies don't get mixed up ๐งฉ The PCP client's source port SHOULD be randomly generated [RFC6056] ๐ฒ
The PCP client MUST include the source IP address of the PCP message in the PCP request. This is typically its own IP address; see Section 16.4 for how this can be coded. This is how an unexpected NAT on the path between the PCP client and the PCP-controlled NAT or firewall gets detected, so the PCP-controlled NAT doesn't waste resources making pointless non-functional mappings ๐ โโ๏ธ When such an intervening non-PCP-aware inner NAT is detected, mappings must first be created by some other means in the inner NAT, before mappings can be usefully created in the outer PCP-controlled NAT. Once mappings exist in the inner NAT by some other means, the PCP client should then use the inner NAT's external address as the client IP address. That tells the outer PCP-controlled NAT that the client knows about the inner NAT and has already set up mappings in it some other way, so the outer NAT's mappings won't be a pointless waste ๐
8.1.1. PCP Client Retransmission ๐
PCP clients are responsible for reliable delivery of PCP request messages ๐ซก If a PCP client doesn't get an expected response from a server, the client must retransmit its message. The retransmissions MUST use the same Mapping Nonce value (see Sections 11.1 and 12.1). The client starts the message exchange by sending a message to the server. The exchange keeps going for as long as the client wants to keep the mapping, and it ends when the PCP client isn't interested in the PCP transaction anymore (e.g., the application that asked for the mapping no longer wants it) or, optionally, when the exchange is considered to have failed under the retransmission mechanism described below โจ
The client's retransmission behavior is controlled by these variables:
RT: Retransmission timeout, calculated as described belowIRT: Initial retransmission time, SHOULD be 3 secondsMRC: Maximum retransmission count, SHOULD be 0 (0 indicates no maximum)MRT: Maximum retransmission time, SHOULD be 1024 secondsMRD: Maximum retransmission duration, SHOULD be 0 (0 indicates no maximum)RAND: Randomization factor, calculated as described below
With each message transmission or retransmission, the client sets RT by the rules below. If RT runs out before a response arrives, the client retransmits the request and computes a new RT ๐ฐ๏ธ
Every new RT calculation includes a new randomization factor (RAND), which is a random number picked with a uniform distribution between -0.1 and +0.1. The randomization factor is there to minimize synchronization of messages sent by PCP clients ๐ The algorithm for picking a random number doesn't need to be cryptographically sound. The algorithm SHOULD produce a different sequence of random numbers from each invocation of the PCP client.
RT gets initialized based on IRT:
RT = (1 + RAND) * IRT
RT for each later message transmission is based on the previous value of RT, subject to the upper bound set by MRT. If MRT is 0, there's no upper limit on RT, and MRT is treated as "infinity". The new RT is calculated like this, where RTprev is the current value of RT:
RT = (1 + RAND) * MIN (2 * RTprev, MRT)
MRC sets an upper bound on how many times a client may retransmit a message. Unless MRC is zero, the message exchange fails once the client has sent the message MRC times ๐
MRD sets an upper bound on how long a client may keep retransmitting a message. Unless MRD is zero, the message exchange fails once MRD seconds have passed since the client first sent the message โณ
If both MRC and MRD are non-zero, the message exchange fails whenever either of the conditions in the two previous paragraphs is met. If both MRC and MRD are zero, the client keeps sending the message until it gets a response or stops wanting a mapping ๐
Once a PCP client has successfully received a response from a PCP server on that interface, it resets RT to a value randomly picked in the range 1/2 to 5/8 of the mapping lifetime, as described in Section 11.2.1, "Renewing a Mapping", and sends later PCP requests for that mapping to that same server ๐
Note: If the server's state changes between retransmissions and the server's response is delayed or lost, the state in the PCP client and server may not be synchronized. This isn't unique to PCP, it also happens with other network protocols (e.g., TCP). In the unlikely event that this de-synchronization happens, PCP heals itself after lifetime seconds ๐ฉน
8.2. General PCP Server: Processing a Request โจ
okay so this part is all about what the PCP server does when a request lands on its doorstep ๐ช Processing SHOULD be performed in the order of the following paragraphs, so follow the order, bestie ๐
A PCP server MUST only accept normal (non-THIRD_PARTY) PCP requests from a client on the same interface that it would normally receive packets from that client on, and it MUST silently ignore PCP requests arriving on any other interface. For example, a residential NAT gateway accepts PCP requests only when they arrive on its (LAN) interface connecting to the internal network, and silently ignores any PCP requests arriving on its external (WAN) interface. A PCP server that supports THIRD_PARTY requests MAY be configured to accept THIRD_PARTY requests on other configured interfaces (see Section 13.1 for details on the THIRD_PARTY Option). ๐ฆ
Upon receiving a request, the PCP server parses and validates it. A valid request contains a valid PCP common header, one valid PCP Opcode, and zero or more options (which the server might or might not comprehend). If an error is encountered during processing, the server generates an error response that is sent back to the PCP client. Processing of an Opcode and its options is specific to each Opcode. ๐ธ
Error responses have the same packet layout as success responses, with certain fields from the request copied into the response, and other fields assigned by the PCP server set as indicated in Figure 3. โจ
Copying request fields into the response is super important, because this is what lets a client figure out which request a given response belongs to. For Opcodes that are understood by the PCP server, it follows the requirements of that Opcode to copy the appropriate fields. For Opcodes that are not understood by the PCP server, it simply generates the UNSUPP_OPCODE response, copies fields from the PCP header, and copies the rest of the PCP payload as is (without attempting to interpret it). ๐ง
All responses (both error and success) contain the same Opcode as the request, but with the R bit set. ๐
Any error response has a non-zero result code, and is created by:
- Copying the entire UDP payload, or 1100 octets, whichever is less, and zero-padding the response to a multiple of 4 octets if necessary
- Setting the
Rbit - Setting the result code
- Setting the Lifetime, Epoch Time, and Reserved fields
- Updating other fields in the response, as indicated by 'set by the server' in the PCP response field description
A success response has a zero result code, and is created by:
- Copying the first 4 octets of request packet header
- Setting the
Rbit - Setting the result code to zero
- Setting the Lifetime, Epoch Time, and Reserved fields
- Possibly setting Opcode-specific response data if appropriate
- Adding any processed options to the response message
If the received PCP request message is less than 2 octets long, it is silently dropped. ๐
If the R bit is set, the message is silently dropped. ๐
If the first octet (version) is a version that is not supported, a response is generated with the UNSUPP_VERSION result code, and the Version Negotiation steps detailed in Section 9 are followed. ๐ฅ
Otherwise, if the version is supported but the received message is shorter than 24 octets, the message is silently dropped. ๐ญ
If the server is overloaded by requests (from a particular client or from all clients), it MAY simply silently discard requests, as the requests will be retried by PCP clients, or it MAY generate the NO_RESOURCES error response. the server gets to pick its vibe here ๐
If the length of the message exceeds 1100 octets, is not a multiple of 4 octets, or is too short for the Opcode in question, it is invalid and a MALFORMED_REQUEST response is generated, and the response message is truncated to 1100 octets. ๐ซ
The PCP server compares the source IP address (from the received IP header) with the field PCP Client IP Address. If they do not match, the error ADDRESS_MISMATCH MUST be returned. This is done to detect and prevent accidental use of PCP where a non-PCP-aware NAT exists between the PCP client and PCP server. If the PCP client wants such a mapping, it needs to ensure that the PCP field matches its apparent IP address from the perspective of the PCP server. this is the NAT plot twist, iykyk ๐
8.3. General PCP Client: Processing a Response
The PCP client receives the response and verifies that the source IP address and port belong to the PCP server of a previously sent PCP request. If not, the response is silently dropped. ๐ซถ
If the received PCP response message is less than 4 octets long, it is silently dropped. ๐ฅบ
If the R bit is clear, the message is silently dropped. ๐
If the error code is UNSUPP_VERSION, Version Negotiation processing continues as described in Section 9. ๐
Responses shorter than 24 octets, longer than 1100 octets, or not a multiple of 4 octets are invalid and ignored. ๐๏ธ
The PCP client then validates that the Opcode matches a previous PCP request. If the response does not match a previous PCP request, the response is ignored. The response is further matched by comparing fields in the response Opcode-specific data to fields in the request Opcode-specific data, as described by the processing for that Opcode. If that fails, the response is ignored. ๐
After these matches are successful, the PCP client checks the Epoch Time field (see Section 8.5) to determine if it needs to restore its state to the PCP server. A PCP client SHOULD be prepared to receive multiple responses from the PCP server at any time after a single request is sent. This allows the PCP server to inform the client of mapping changes such as an update or deletion. For example, a PCP server might send a SUCCESS response and, after a configuration change on the PCP server, later send a NOT_AUTHORIZED response. so yes, the server can change its mind, and you need to be ready ๐ง
A PCP client MUST be prepared to receive responses for requests it never sent (which could have been sent by a previous PCP instance on this same host, by a previous host that used the same client IP address, or by a malicious attacker) by simply ignoring those unexpected messages. ๐ก๏ธ
If the error ADDRESS_MISMATCH is received, it indicates the presence of a NAT between the PCP client and PCP server. Procedures to resolve this problem are beyond the scope of this document. ๐ง
For both success and error responses, a Lifetime value is returned. The lifetime indicates how long this response should be considered valid by the client (i.e., for success results, how long the mapping will last, and for failure results, how long the same failure condition should be expected to persist). The PCP client SHOULD impose an upper limit on this returned value (to protect against absurdly large values, e.g., 5 years), detailed in Section 15, "Mapping Lifetime and Deletion". โณ
If the result code is 0 (SUCCESS), the request succeeded. ๐
If the result code is not 0, the request failed, and the PCP client SHOULD NOT resend the same request for the indicated lifetime of the error (as limited by the sanity checking detailed in Section 15). do not spam the server, bestie ๐ โโ๏ธ
If the PCP client has discovered a new PCP server (e.g., connected to a new network), the PCP client MAY immediately begin communicating with this PCP server, without regard to hold times from communicating with a previous PCP server. โจ
8.4. Multi-Interface Issues
Hosts that desire a PCP mapping might be multi-interfaced (i.e., own several logical/physical interfaces). Indeed, a host can be configured with several IPv4 addresses (e.g., WiFi and Ethernet) or be dual-stacked. These IP addresses may have distinct reachability scopes (e.g., if IPv6, they might have global reachability scope, as is the case for a Global Unicast Address (GUA) [RFC3587], or limited scope, as is the case for a Unique Local Address (ULA) [RFC4193]). ๐
IPv6 addresses with global reachability (e.g., GUAs) SHOULD be used as the source address when generating a PCP request. IPv6 addresses without global reachability (e.g., ULAs) SHOULD NOT be used as the source interface when generating a PCP request. If IPv6 privacy addresses [RFC4941] are used for PCP mappings, a new PCP request will need to be issued whenever the IPv6 privacy address is changed. This PCP request SHOULD be sent from the IPv6 privacy address itself. It is RECOMMENDED that the client delete its mappings to the previous privacy address after it no longer needs those old mappings. clean up after yourself, the way you'd clean your room ๐งน
Due to the ubiquity of IPv4 NAT, IPv4 addresses with limited scope (e.g., private addresses [RFC1918]) MAY be used as the source interface when generating a PCP request. ๐
8.5. Epoch ๐ฐ๏ธ
okay so every PCP response the server sends has an Epoch Time field, and it ticks up by one every second โฐ if that number ever acts weird, it's a little hint to clients that the server might have lost state ๐ clients respond to that hint by promptly renewing their mappings, so the server's state gets restored fast ๐โจ
If the PCP server resets or loses the state of its explicit dynamic mappings (that is, those mappings created by PCP requests), due to reboot, power failure, or any other reason, it MUST reset its Epoch time to its initial starting value (usually zero) to provide this hint to PCP clients. After resetting, it goes right back to incrementing the Epoch Time value by one every second ๐
Similarly, if the external IP address(es) of the NAT (controlled by the PCP server) changes, the Epoch time MUST be reset ๐ a PCP server MAY maintain one Epoch Time value for all PCP clients, or it MAY maintain distinct Epoch Time values (per PCP client, per interface, or based on other criteria). this choice is implementation-dependent, so it's totally up to the server ๐ซถ
Whenever a client receives a PCP response, it validates the received Epoch Time value using integer arithmetic, following this procedure:
- If this is the first PCP response the client has received from this PCP server, the Epoch Time value is treated as necessarily valid. Otherwise:
- If the current PCP server Epoch time (
curr_server_time) is less than the previously received PCP server Epoch time (prev_server_time) by more than one second, then the client treats the Epoch time as obviously invalid (time should not go backwards). ๐ โโ๏ธ The server Epoch time apparently going backwards by up to one second is NOT deemed invalid, so that minor packet reordering on the path from PCP server to PCP client doesn't trigger a cascade of unnecessary mapping renewals. If the server Epoch time passes this check, then further validation checks are performed:- The client computes the difference between its current local time (
curr_client_time) and the time the previous PCP response was received from this PCP server (prev_client_time):client_delta = curr_client_time - prev_client_time; - The client computes the difference between the current PCP server Epoch time (
curr_server_time) and the previously received Epoch time (prev_server_time):server_delta = curr_server_time - prev_server_time; - If
client_delta+2 < server_delta - server_delta/16orserver_delta+2 < client_delta - client_delta/16, then the client treats the Epoch Time value as invalid, else the client treats the Epoch Time value as valid.
- The client computes the difference between its current local time (
- If the current PCP server Epoch time (
- The client records the current time values for use in its next comparison:
prev_client_time = curr_client_timeandprev_server_time = curr_server_time๐
If the PCP client determined that the Epoch Time value it received was invalid, then it concludes that the PCP server may have lost state, and promptly renews all its active port mapping leases following the mapping recreation procedure described in Section 16.3.1 ๐จ๐
Notes:
- The client clock MUST never go backwards. If
curr_client_timeis found to be less thanprev_client_time, then this is a client bug, and how the client deals with this client bug is implementation specific. ๐ - The calculations above are constructed to allow
client_deltaandserver_deltato be computed as unsigned integer values. ๐ข - The "+2" in the calculations above is to accommodate quantization errors in client and server clocks (up to one-second quantization error each in server and client time intervals). ๐งฎ
- The "/16" in the calculations above is to accommodate inaccurate clocks in low-cost devices. This allows for a total discrepancy of up to 1/16 (6.25%) to be considered benign. For example, if one clock were to run too fast by 3% while the other clock ran too slow by 3%, then the client would not consider this difference to be anomalous or indicative of a restart having occurred. This tolerance is strict enough to be effective at detecting reboots, while not being so strict as to generate false alarms. โจ
9. Version Negotiation โจ
okay so a PCP client sends its requests using PCP version number 2 ๐ should later updates to this document specify different message formats with a version number greater than 2, it's expected that PCP servers will still support version 2 alongside the newer versions. however, if a server returns a response with result code UNSUPP_VERSION, the client MAY log an error message to inform the user that it is too old to work with this server ๐
should later updates to this document specify different message formats with a version number greater than 2, and backwards compatibility is wanted, this first octet can be used for forward and backward compatibility ๐
if future PCP versions greater than 2 are specified, version negotiation goes like this:
- The client sends its first request using the highest (i.e., presumably 'best') version number it supports. ๐
- If the server supports that version, it responds normally. ๐ซถ
- If the server does not support that version, it replies giving a result containing the result code
UNSUPP_VERSION, and the closest version number it does support. If the server supports a range of versions higher than the client's requested version, the server returns the lowest of that supported range. If the server supports a range of versions lower than the client's requested version, the server returns the highest of that supported range. ๐ง - If the client receives an
UNSUPP_VERSIONresult containing a version it does support, it records this fact and proceeds to use this message version for subsequent communication with this PCP server (until a possible futureUNSUPP_VERSIONresponse if the server is later updated, at which point the version negotiation process repeats). If the version number in theUNSUPP_VERSIONresponse is zero, then that means this is a NAT-PMP server [RFC6886], and a client MAY choose to communicate with it using the older NAT-PMP protocol, as described in Appendix A. ๐ฆ - If the client receives an
UNSUPP_VERSIONresult containing a version it does not support, then the client SHOULD try the next-lower version supported by the client. The attempt to use the next-lower version repeats until the client has tried version 2. If using version 2 fails, the client MAY log an error message to inform the user that it is too old to work with this server, and the client SHOULD set a timer to retry its request in 30 minutes or the returned Lifetime value, whichever is smaller. By automatically retrying in 30 minutes, the protocol accommodates an upgrade of the PCP server ๐๐ธ
10. Introduction to MAP and PEER Opcodes โจ
okay bestie, the MAP and PEER Opcodes have four main jobs ๐ and yes, they're all iconic:
- a host operating a server and wanting an incoming connection (Section 10.1);
- a host operating a client and server on the same port (Section 10.2);
- a host operating a client and wanting to optimize the application keepalive traffic (Section 10.3); and
- a host operating a client and wanting to restore lost state in its NAT (Section 10.4).
We'll go through these one by one ๐ฆ, and there's a state diagram in Section 16.5 too. It's non-normative, so think of it as a cute visual aid ๐ง
When you're operating a server (see Sections 10.1 and 10.2), the PCP client needs to know if it wants an IPv4 listener, an IPv6 listener, or both on the Internet ๐ It also needs to know if it has an IPv4 address or an IPv6 address configured on one of its interfaces. It combines both of those facts to decide which of its PCP servers to send the request to (e.g., an IPv4 address or an IPv6 address), and whether to send one or two MAP requests for each of its interfaces. For example, if the PCP client only has an IPv4 address but wants both IPv6 and IPv4 listeners, it sends a MAP request containing the all-zeros IPv6 address in the Suggested External Address field, and then sends a second MAP request containing the all-zeros IPv4 address in the Suggested External Address field. ๐
If the PCP client has both an IPv4 and an IPv6 address but only wants an IPv4 listener, it sends one MAP request from its IPv4 address (if the PCP server supports NAT44 or IPv4 firewall) or one MAP request from its IPv6 address (if the PCP server supports NAT64). The PCP client can simply request the mapping it wants to find out whether the PCP server supports it. ๐ Applications that embed IP addresses in their payloads (e.g., FTP, SIP) will find it beneficial to avoid address family translation, if possible. ๐ซถ
The MAP and PEER requests include a Suggested External IP Address field. Some PCP-controlled devices, especially CGN but also multi-homed NPTv6 networks, have a pool of public-facing IP addresses. PCP lets the client indicate whether it wants a mapping assigned on a specific address from that pool, or on any address from that pool. ๐ธ Some applications will break if mappings are created on different IP addresses (e.g., active mode FTP), so applications should carefully consider the implications of using this capability. ๐ญ
Static mappings for that internal address (e.g., those created by a command-line interface on the PCP server or PCP-controlled device) may exist for a certain external address. If the suggested external IP address is the IPv4 or IPv6 all-zeros address, PCP SHOULD assign its mappings to the same external address. This can also help applications that use a mix of static mappings and PCP-created mappings, which is very much a main character move ๐ If, on the other hand, the suggested external IP address contains a non-zero IP address, the PCP server SHOULD create a mapping to that external address, even if there are other mappings from that same internal address to a different external address. ๐
Once an internal address has no implicit dynamic mappings and no explicit dynamic mappings in the PCP-controlled device, a subsequent implicit or explicit mapping for that internal address MAY be assigned to a different External address. Generally, this reassignment happens when a CGN device is load balancing newly seen internal addresses across its public pool of external addresses. ๐
The following table summarizes how various common PCP deployments use IPv6 and IPv4 addresses. ๐ง
The 'internal' address is implicitly the same as the source IP address of the PCP request, except when the THIRD_PARTY option is used.
The 'external' address is the Suggested External Address field of the MAP or PEER request, and its address family is usually the same as the 'internal' address family, except when technologies like NAT64 are used.
The 'remote peer' address is the remote peer IP address of the PEER request or the FILTER option of the MAP request. It is always the same address family as the 'internal' address, even when NAT64 is used. In NAT64, the IPv6 PCP client is not necessarily aware of the NAT64 or of the actual IPv4 address of the remote peer, so it expresses the IPv6 address from its own perspective, as shown in Figure 5. ๐ง
internal external PCP remote peer actual remote peer
-------- ------- --------------- ------------------
IPv4 firewall IPv4 IPv4 IPv4 IPv4
IPv6 firewall IPv6 IPv6 IPv6 IPv6
NAT44 IPv4 IPv4 IPv4 IPv4
NAT46 IPv4 IPv6 IPv4 IPv6
NAT64 IPv6 IPv4 IPv6 IPv4
NPTv6 IPv6 IPv6 IPv6 IPv6
Figure 5: Address Families with MAP and PEER
Note that the internal address and the remote peer address are always the same address family, and the external address and the actual remote peer address are always the same address family. ๐
10.1. For Operating a Server ๐ฅ๏ธ
A host operating a server (e.g., a web server) listens for traffic on a port, but the server never initiates traffic from that port. For this to work across a NAT or a firewall, the host needs to (a) create a mapping from a public IP address, protocol, and port to itself using the MAP Opcode, as described in Section 11; (b) publish that public IP address, protocol, and port via some sort of rendezvous server (e.g., DNS, a SIP message, or a proprietary protocol); and (c) ensure that any other non-PCP-speaking packet filtering middleboxes on the path (e.g., host-based firewall, network-based firewall, or other NATs) will also allow the incoming traffic. Publishing the public IP address and port is out of scope for this specification. To accomplish (a), the host follows the procedures described in this section. ๐ง
As normal, the application needs to begin listening on a port. Then, the application constructs a PCP message with the MAP Opcode, with the external address set to the appropriate all-zeros address, depending on whether it wants a public IPv4 or IPv6 address. ๐ฆ
The following pseudocode shows how PCP can be reliably used to operate a server:
/* start listening on the local server port */
int s = socket(...);
bind(s, ...);
listen(s, ...);
getsockname(s, &internal_sockaddr, ...);
bzero(&external_sockaddr, sizeof(external_sockaddr));
while (1)
{
/* Note: The "time_to_send_pcp_request()" check below includes:
* 1. Sending the first request
* 2. Retransmitting requests due to packet loss
* 3. Resending a request due to impending lease expiration
* 4. Resending a request due to server state loss
* The PCP packet sent is identical in all four cases; from
* the PCP server's point of view they are the same operation.
* The suggested external address and port may be updated
* repeatedly during the lifetime of the mapping.
* Other fields in the packet generally remain unchanged.
*/
if (time_to_send_pcp_request())
pcp_send_map_request(internal_sockaddr.sin_port,
internal_sockaddr.sin_addr,
&external_sockaddr, /* will be zero the first time */
requested_lifetime, &assigned_lifetime);
if (pcp_response_received())
update_rendezvous_server("Client Ident", external_sockaddr);
if (received_incoming_connection_or_packet())
process_it(s);
if (other_work_to_do())
do_it();
/* ... */
block_until_we_need_to_do_something_else();
}
Figure 6: Pseudocode for Using PCP to Operate a Server
10.2. For Operating a Symmetric Client/Server ๐ช
okay bestie, this is the one where a host runs a client AND a server on the very same port ๐ฆ Think Symmetric RTP [RFC4961] or SIP Symmetric Response Routing (rport) [RFC3581]. The host sets up a local listener first, (usually) sends its local and public IP addresses, protocol, and ports to a rendezvous service (which is out of scope of this document ๐), and then starts an outbound connection from that same source address and same port. To pull this off, the app follows the procedure in this section ๐
An application that is using the same port for outgoing connections as well as incoming connections MUST first signal its operation of a server using the PCP MAP Opcode, as described in Section 11, and receive a positive PCP response before it sends any packets from that port. No skipping ahead, periodt โจ
Discussion: ngl this part is a lot, so let's break it down ๐ง A PCP client doesn't know in advance if it's behind a NAT or firewall. When the host joins a new network, the client can try to request a mapping using PCP. If that works, it knows it has a mapping, and that's iconic ๐ If it gets no PCP response after multiple retries, then either it's not behind a NAT or firewall and has unfettered connectivity, or it is behind a NAT or firewall that doesn't support PCP (and it may still have working connectivity thanks to static mappings the user made manually ๐ซ ). Retrying a bunch of times before giving up and assuming unfettered connectivity adds delay in that case ๐ญ Starting outbound TCP connections right away skips that wait, and it works if the NAT has endpoint-independent mapping (EIM) behavior. But it can fail if the NAT has endpoint-dependent mapping (EDM) behavior ๐ Waiting long enough to let an explicit PCP MAP mapping get created (if possible) means the same external port gets used for all the later implicit dynamic mappings (e.g., TCP SYNs) sent from that internal address, protocol, and port ๐ง PCP supports both EIM and EDM NATs, so clients need to assume they may be dealing with an EDM NAT. In that case, the client gets more reliable connectivity if it tries explicit PCP MAP requests first, before starting any outbound TCP connections from that internal address and port ๐ซถ For more on using PCP with EDM NATs, check out Section 16.1 ๐ธ
The following pseudocode shows how PCP can be used to operate a symmetric client and server ๐
/* start listening on the local server port */
int s = socket(...);
bind(s, ...);
listen(s, ...);
getsockname(s, &internal_sockaddr, ...);
bzero(&external_sockaddr, sizeof(external_sockaddr));
while (1)
{
/* Note: The "time_to_send_pcp_request()" check below includes:
* 1. Sending the first request
* 2. Retransmitting requests due to packet loss
* 3. Resending a request due to impending lease expiration
* 4. Resending a request due to server state loss
*/
if (time_to_send_pcp_request())
pcp_send_map_request(internal_sockaddr.sin_port,
internal_sockaddr.sin_addr,
&external_sockaddr, /* will be zero the first time */
requested_lifetime, &assigned_lifetime);
if (pcp_response_received())
update_rendezvous_server("Client Ident", external_sockaddr);
if (received_incoming_connection_or_packet())
process_it(s);
if (need_to_make_outgoing_connection())
make_outgoing_connection(s, ...);
if (data_to_send())
send_it(s);
if (other_work_to_do())
do_it();
/* ... */
block_until_we_need_to_do_something_else();
}
Figure 7: Pseudocode for Using PCP to Operate a
Symmetric Client/Server
10.3. For Reducing NAT or Firewall Keepalive Messages ๐
so this one is for a host running a client (think XMPP client or SIP client ๐ฑ). It sends from a port and may get responses, but it never accepts incoming connections from other remote peers on that port. It wants to make sure the flow to its remote peer doesn't get killed for inactivity by an on-path NAT or firewall ๐ To do that, the app follows the procedure in this section โจ
Middleboxes, like NATs or firewalls, generally need to see occasional traffic or they'll wipe their session state, and then apps break ๐ฉ To avoid that, lots of apps send keepalive traffic all the time, mainly just to keep state alive with those middleboxes. Apps can cut down on that application keepalive traffic by using PCP ๐
Note: For reasons beyond NAT, an app might want application-level keepalives too, like detecting a broken path between the client and server, keeping state alive on the remote peer, or detecting a powered-down client. These aren't related to keeping middlebox state alive, and PCP can't do anything useful to reduce them ๐ โโ๏ธ
To use PCP for this, the app first connects to its server as normal. After that, it sends a PCP request with the PEER Opcode, as described in Section 12, to learn and/or extend the lifetime of its mapping ๐ฆ
The following pseudocode shows how PCP can be reliably used with a dynamic socket, to reduce application keepalive messages ๐ง
/* make outgoing connection to server */
int s = socket(...);
connect(s, &remote_peer, ...);
getsockname(s, &internal_sockaddr, ...);
bzero(&external_sockaddr, sizeof(external_sockaddr));
while (1)
{
/* Note: The "time_to_send_pcp_request()" check below includes:
* 1. Sending the first request
* 2. Retransmitting requests due to packet loss
* 3. Resending a request due to impending lease expiration
* 4. Resending a request due to server state loss
*/
if (time_to_send_pcp_request())
pcp_send_peer_request(internal_sockaddr.sin_port,
internal_sockaddr.sin_addr,
&external_sockaddr, /* will be zero the first time */
remote_peer, requested_lifetime, &assigned_lifetime);
if (data_to_send())
send_it(s);
if (received_incoming_data())
process_it(s);
if (other_work_to_do())
do_it();
/* ... */
block_until_we_need_to_do_something_else();
}
Figure 8: Pseudocode Using PCP with a Dynamic Socket
10.4. For Restoring Lost Implicit TCP Dynamic Mapping State ๐
okay so picture this: a NAT loses its state after a crash or power failure ๐ฅ Now clients need to re-establish their TCP mappings on the NAT. That way, servers on the Internet see traffic from the same IP address and port again, and sessions can pick up right where they left off ๐ฌ This is super useful for long-lived connections (like instant messaging) or connections moving a ton of data (like FTP) ๐
Here's how it works: first, the client establishes a TCP connection normally, then sends a PEER request/response, and remembers the external address and external port. Later, once the NAT has lost state, the client sends a PEER request with the suggested external port and suggested external address it remembered from the previous session. That creates a mapping in the NAT that works exactly like an implicit dynamic mapping ๐ช Then the client resumes sending TCP data to the server ๐
Note: This procedure works well for TCP, provided: - (i) the NAT creates a new implicit dynamic outbound mapping only for outbound TCP segments with the SYN bit set (i.e., the newly booted NAT silently drops outbound data segments from the client when the NAT doesn't have an active mapping for those segments), and - (ii) the newly booted NAT does not send a TCP RST in response to receiving unexpected inbound TCP segments. This procedure works less well for UDP, though ๐ฌ As soon as outbound UDP traffic is seen by the NAT, a new UDP implicit dynamic outbound mapping gets created (probably on a different port) ๐
11. MAP Opcode โจ
This section defines an Opcode that controls inbound forwarding from a NAT (or firewall) to an internal host ๐ช
MAP: Create an explicit dynamic mapping between an Internal Address
+ Port and an External Address + Port.
PCP servers SHOULD provide a configuration option so administrators can disable MAP support if they want to ๐ โโ๏ธ
Mappings created by PCP MAP requests are, by definition, endpoint-independent mappings (EIMs) with endpoint-independent filtering (EIF) (unless the FILTER option is used). That's true even on a NAT that usually creates endpoint-dependent mappings (EDM) or endpoint-dependent filtering (EDF) for outgoing connections ๐ฆ The whole point of an (unfiltered) MAP mapping is to receive inbound traffic from any remote endpoint, not just one specific remote endpoint ๐
Also, all NAT mappings (created by PCP or otherwise) are by necessity bidirectional and symmetric ๐ For any packet going in one direction (in or out) that the NAT translates, a reply going in the opposite direction needs the matching opposite translation so it arrives at the right endpoint ๐ฏ So if a client creates a MAP mapping, and later sends an outgoing packet using that mapping's internal address, protocol, and port, the NAT should translate that packet's internal address and port to the mapping's external address and port. That way, replies addressed to the external address and port get translated back to the mapping's internal address and port correctly ๐
On operating systems that allow multiple listening servers to bind to the same internal address, protocol, and port, servers MUST ensure that they have exclusive use of that internal address, protocol, and port ๐ (e.g., by binding the port using INADDR_ANY, or using SO_EXCLUSIVEADDRUSE or similar) before sending their PCP MAP request. This makes sure no other PCP clients on the same machine are also listening on the same internal protocol and internal port ๐
As a side effect of creating a mapping, ICMP messages associated with the mapping MUST be forwarded (and also translated, if appropriate) for the duration of the mapping's lifetime ๐จ This is done so ICMP messages can still be used by hosts, without application programmers or PCP client implementations needing to use PCP separately to create ICMP mappings for those flows ๐ซถ
The operation of the MAP Opcode is described in this section.
11.1. MAP Operation Packet Formats ๐
okay so the MAP Opcode has a similar packet layout for both requests and responses ๐ if the assigned external IP address and port in the PCP response always match the internal IP address and port from the PCP request, then it's purely a firewall ๐งฑ. otherwise it's a Network Address Translator that might also do firewall-like stuff โจ
here's the Opcode-specific info in a MAP request ๐
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Mapping Nonce (96 bits) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Protocol | Reserved (24 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Internal Port | Suggested External Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Suggested External IP Address (128 bits) |
| |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Figure 9: MAP Opcode Request
here's what each field does, bestie ๐ซถ
Requested lifetime (in common header): how long you want this mapping to last, in seconds ๐ฐ๏ธ the value 0 means "delete" ๐๏ธ
Mapping Nonce: a random value the PCP client picks ๐ฒ see Section 11.2, "Generating a MAP Request". zero is a legal value, but it's super unlikely, like roughly one in 2^96 requests ๐คฏ
Protocol: the upper-layer protocol for this Opcode, with values taken from the IANA protocol registry [proto_numbers] ๐ for example it's 6 (TCP) if you want a TCP mapping and 17 (UDP) for a UDP mapping ๐ the value 0 has a special meaning, it's "all protocols" ๐
Reserved: 24 reserved bits ๐ซง they MUST be sent as 0 and MUST be ignored when received ๐
Internal Port: the internal port for the mapping ๐ช the value 0 means "all ports" ๐ง and it's legal when the lifetime is zero (a delete request), if the protocol doesn't use 16-bit port numbers, or if the client is asking for "all ports" ๐ if the protocol is zero (meaning "all protocols"), then internal port MUST be zero on transmission and MUST be ignored on reception ๐ญ
Suggested External Port: the external port you'd like for the mapping ๐ this is super handy for refreshing a mapping, especially after the PCP server loses state ๐ if the PCP client doesn't know the external port, or doesn't have a preference, it MUST use 0 ๐
Suggested External IP Address: the suggested external IPv4 or IPv6 address ๐ also handy for refreshing a mapping after the PCP server loses state ๐ซ if the PCP client doesn't know the external address, or doesn't have a preference, it MUST use the address-family-specific all-zeros address (see Section 5) ๐ซ
the internal address for the request is the source IP address of the PCP request message itself ๐ฌ unless the THIRD_PARTY option is used ๐
here's the Opcode-specific info in a MAP response packet ๐ฆ
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Mapping Nonce (96 bits) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Protocol | Reserved (24 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Internal Port | Assigned External Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Assigned External IP Address (128 bits) |
| |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Figure 10: MAP Opcode Response
and the response fields, bestie ๐
Lifetime (in common header): on an error response, this tells clients how long they should assume they'll keep getting the same error if they repeat the same request ๐ โโ๏ธ on a success response, it's the lifetime for this mapping, in seconds โณ
Mapping Nonce: copied from the request ๐
Protocol: copied from the request ๐
Reserved: 24 reserved bits, they MUST be sent as 0 and MUST be ignored when received ๐ซง
Internal Port: copied from the request ๐
Assigned External Port: on a success response, this is the assigned external port for the mapping ๐ on an error response, the suggested external port is copied from the request ๐
Assigned External IP Address: on a success response, this is the assigned external IPv4 or IPv6 address for the mapping ๐ธ an IPv4 address is encoded using IPv4-mapped IPv6 address ๐งฉ on an error response, the suggested external IP address is copied from the request ๐
11.2. Generating a MAP Request ๐ ๏ธ
this section is all about how a PCP client sends requests with the MAP Opcode ๐งโ๐ป
the request MAY contain values in the Suggested External Port and Suggested External IP Address fields โจ this lets the PCP client try to rebuild lost state on the PCP server, which improves the chances of existing connections surviving ๐ซถ it also helps the PCP client avoid having to change info kept at its rendezvous server ๐งธ of course, because of other activity on the network (like other users or network renumbering), the PCP server might not be able to grant the suggested external IP address, protocol, and port ๐ฌ in that case it will assign a different external IP address and port ๐
a PCP client MUST be written assuming that it may never be assigned the external port it suggests ๐ if it's recreating state after a NAT gateway crash, the suggested external port was previously allocated to this client, so it's likely to be available for this client to keep using ๐ฅน in all other cases, the client MUST assume that it is unlikely that its suggested external port will be granted ๐ for example, when lots of subscribers share a Carrier-Grade NAT, popular ports such as 80, 443, and 8080 are likely to be in high demand ๐ฅ at most one client can have each of those popular ports for each external IP address, and all the other clients get other, dynamically allocated, external ports ๐ฒ some ISPs may, by policy, choose not to grant those external ports to anyone, so none of their clients are ever assigned external ports 80, 443, or 8080 ๐
if the protocol doesn't use 16-bit port numbers (e.g., RSVP, IP protocol number 46), the port number MUST be zero ๐ข this causes all traffic matching that protocol to be mapped ๐
if the client wants all protocols mapped, it uses protocol 0 (zero) and internal port 0 (zero) ๐
the Mapping Nonce is randomly chosen by the PCP client ๐ฒ following accepted practices for generating unguessable random numbers [RFC4086] ๐ it's used as part of validating PCP responses (see below) by the PCP client, and for validating mapping refreshes by the PCP server ๐งพ the client MUST use a different mapping nonce for each PCP server it communicates with ๐ซต it is RECOMMENDED to choose a new random mapping nonce whenever the PCP client is initialized ๐ฑ the client MAY use a different mapping nonce for every mapping ๐
11.2.1. Renewing a Mapping ๐
an existing mapping SHOULD have its lifetime extended by the PCP client for as long as the client wants that mapping to keep existing ๐ฆ to do this, the PCP client sends a new MAP request indicating the internal port ๐จ the PCP MAP request SHOULD also include the currently assigned external IP address and port in the Suggested External IP Address and Suggested External Port fields ๐ง that way, if the PCP server has lost state, it can recreate the lost mapping with the same parameters ๐ซถ
the PCP client SHOULD renew the mapping before its expiry time ๐ซฃ otherwise it'll be removed by the PCP server (see Section 15, "Mapping Lifetime and Deletion") ๐๏ธ to reduce the risk of accidentally syncing up renewal requests, a random jitter component should be included ๐ฒ it is RECOMMENDED that PCP clients send a single renewal request packet at a time chosen with uniform random distribution in the range 1/2 to 5/8 of expiration time ๐ฏ if no SUCCESS response is received, then the next renewal request should be sent 3/4 to 3/4 + 1/16 to expiration, and then another 7/8 to 7/8 + 1/32 to expiration, and so on ๐ but renewal requests MUST NOT be sent less than four seconds apart ๐ a PCP client MUST NOT send a flood of ever-closer-together requests in the last few seconds before a mapping expires ๐ญ๐ซถ
11.3. Processing a MAP Request ๐
Okay bestie, this part is all about how a PCP server handles a request with the MAP Opcode ๐ง. Processing SHOULD be done in the order of the following paragraphs, so go in order โจ.
The Protocol, Internal Port, and Mapping Nonce fields from the MAP request get copied straight into the MAP response ๐ซถ. If the THIRD_PARTY option is there and the PCP server processed it, that gets copied into the response too ๐.
If the requested lifetime is non-zero, then:
- If both the protocol and internal port are non-zero, that's a request to create a mapping or extend the lifetime of an existing one ๐ฆ. If the PCP server or PCP-controlled device does not support the protocol, the UNSUPP_PROTOCOL error MUST be returned ๐.
- If the protocol is non-zero and the internal port is zero, that's a request to create or extend a mapping for all incoming traffic for that entire protocol, a 'wildcard' (all-ports) mapping for that protocol ๐. If this request cannot be fulfilled in its entirety, the UNSUPP_PROTOCOL error MUST be returned.
- If both the protocol and internal port are zero, that's a request to create or extend a mapping for all incoming traffic for all protocols (commonly called a 'DMZ host') ๐. If this request cannot be fulfilled in its entirety, the UNSUPP_PROTOCOL error MUST be returned.
- If the protocol is zero and the internal port is non-zero, the request is invalid and the PCP server MUST return a MALFORMED_REQUEST error to the client ๐.
If the requested lifetime is zero, it means a request to delete an existing mapping.
Further processing of the lifetime is described in Section 15, "Mapping Lifetime and Deletion" ๐ธ.
If the PCP server is operating in the Simple Threat Model (Section 18.1), and the internal port, protocol, and internal address match an existing explicit dynamic mapping, but the mapping nonce does not match, the request MUST be rejected with a NOT_AUTHORIZED error, with the lifetime of the error indicating the duration of that existing mapping ๐. The PCP server only needs to remember one Mapping Nonce value for each explicit dynamic mapping. This specification makes no statement about mapping nonce with the Advanced Threat Model.
If the internal port, protocol, and internal address match an existing static mapping (which will have no nonce), then a PCP reply is sent giving the external address and port of that static mapping, using the nonce from the PCP request. The server does not record the nonce ๐งธ.
If an option with value less than 128 exists (i.e., mandatory to process) but that option does not make sense (e.g., the PREFER_FAILURE option is included in a request with lifetime=0), the request is invalid and generates a MALFORMED_OPTION error ๐
โโ๏ธ.
If the PCP-controlled device is stateless (that is, it does not establish any per-flow state, and simply rewrites the address and/or port in a purely algorithmic fashion, including no rewriting), the PCP server simply returns an answer indicating the external IP address and port yielded by this stateless algorithmic translation ๐งฎ. This lets the PCP client learn its external IP address and port as seen by remote peers. Examples of stateless translators include stateless NAT64, 1:1 NAT44, and NPTv6 [RFC6296], all of which modify addresses but not port numbers, plus pure firewalls, which modify neither the address nor the port ๐งฑ.
It is possible that a mapping might already exist for a requested internal address, protocol, and port. If so, the PCP server takes the following actions:
- If the MAP request contains the PREFER_FAILURE option, but the suggested external address and port do not match the external address and port of the existing mapping, the PCP server MUST return CANNOT_PROVIDE_EXTERNAL ๐ซ.
- If the existing mapping is static (created outside of PCP), the PCP server MUST return the external address and port of the existing mapping in its response, and SHOULD indicate a lifetime of
2^32-1seconds, regardless of the suggested external address and port in the request ๐. - If the existing mapping is explicit dynamic inbound (created by a previous MAP request), the PCP server MUST return the existing external address and port in its response, regardless of the suggested external address and port in the request. Additionally, the PCP server MUST update the lifetime of the existing mapping, in accordance with Section 15, "Mapping Lifetime and Deletion" โฐ.
- If the existing mapping is dynamic outbound (created by outgoing traffic or a previous PEER request), the PCP server SHOULD create a new explicit inbound mapping, replicating the ports and addresses from the outbound mapping. The outbound mapping keeps existing, and stays in effect if the explicit inbound mapping is later deleted ๐.
If no mapping exists for the internal address, protocol, and port, and the PCP server is able to create a mapping using the suggested external address and port, it SHOULD do so. This is super beneficial for re-establishing state lost in the PCP server (e.g., due to a reboot) ๐. There are, however, cases where the PCP server is not able to create a new mapping using the suggested external address and port:
- The suggested external address, protocol, and port is already assigned to another existing explicit or implicit mapping (i.e., is already forwarding traffic to some other internal address and port) ๐ง.
- The suggested external address, protocol, and port is already used by the NAT gateway for one of its own services, for example, TCP port 80 for the NAT gateway's own configuration web pages, or UDP ports 5350 and 5351, used by PCP itself. A PCP server MUST NOT create client mappings for external UDP ports 5350 or 5351 ๐ โโ๏ธ.
- The suggested external address, protocol, and port is otherwise prohibited by the PCP server's policy ๐.
- The suggested external IP address, protocol, or suggested port are invalid or invalid combinations (e.g., external address 127.0.0.1, ::1, a multicast address, or the suggested port is not valid for the protocol) ๐ฅด.
- The suggested external address does not belong to the NAT gateway ๐ .
- The suggested external address is not configured to be used as an external address of the firewall or NAT gateway โ๏ธ.
If the PCP server cannot assign the suggested external address, protocol, and port, then:
- If the request contained the PREFER_FAILURE option, the PCP server MUST return CANNOT_PROVIDE_EXTERNAL ๐ค.
- If the request did not contain the PREFER_FAILURE option, and the PCP server can assign some other external address and port for that protocol, then the PCP server MUST do so and return the newly assigned external address and port in the response. In no case is the client penalized for a 'poor' choice of suggested external address and port ๐ซ . The suggested external address and port may be used by the server to guide its choice of what external address and port to assign, but in no case do they cause the server to fail to allocate an external address and port where otherwise it would have succeeded. The presence of a non-zero suggested external address or port is merely a hint; it never does any harm ๐.
A PCP-controlled device MUST NOT create mappings for a protocol not indicated in the request. For example, if the request was for a TCP mapping, an additional corresponding UDP mapping MUST NOT be automatically created ๐ซ๐ฆ.
Mappings typically consume state on the PCP-controlled device, so it is RECOMMENDED that a per-host and/or per-subscriber limit be enforced by the PCP server to prevent exhausting the mapping state ๐ง . If this limit is exceeded, the result code USER_EX_QUOTA is returned.
If all of the preceding operations were successful (did not generate an error response), then the requested mapping is created or refreshed as described in the request, and a SUCCESS response is built ๐โจ.
11.4. Processing a MAP Response ๐
This section describes what the PCP client does when it receives a PCP response for the MAP Opcode ๐ฌ.
After performing common PCP response processing, the response is further matched with a previously sent MAP request by comparing the internal IP address (the destination IP address of the PCP response, or other IP address specified via the THIRD_PARTY option), the protocol, the internal port, and the mapping nonce. Other fields are not compared, because the PCP server sets those fields. The PCP server will send a Mapping Update (Section 14.2) if the mapping changes (e.g., due to IP renumbering) ๐ง.
If the result code is NO_RESOURCES and the request was for the creation or renewal of a mapping, the PCP client SHOULD NOT send further requests for any new mappings to that PCP server for the (limited) value of the lifetime ๐ โโ๏ธ. If the result code is NO_RESOURCES and the request was for the deletion of a mapping, the PCP client SHOULD NOT send further requests of any kind to that PCP server for the (limited) value of the lifetime ๐.
On a success response, the PCP client can use the external IP address and port as needed. Typically, the PCP client will share the external IP address and port with another host on the Internet using an application-specific rendezvous mechanism, such as DNS SRV records ๐๐.
After a success response, for as long as renewal is desired, the PCP client MUST set a timer or otherwise schedule an event to renew the mapping before its lifetime expires โณ. Renewing a mapping is performed by sending another MAP request, exactly as described in Section 11.2, except that the suggested external address and port SHOULD be set to the values received in the response ๐. From the PCP server's point of view, a MAP request to renew a mapping is identical to a MAP request to create a new mapping, and it is handled identically ๐ฏโโ๏ธ. Indeed, in the event of PCP server state loss, a renewal request from a PCP client will appear to the server to be a request to create a new mapping, with a particular suggested external address and port, which happen to be what the PCP server previously assigned ๐ง. See also Section 16.3.1, "Recreating Mappings".
On an error response, the client SHOULD NOT repeat the same request to the same PCP server within the lifetime returned in the response ๐ฅบ.
11.5. Address Change Events ๐ฆ
Okay so a customer premises router can totally end up with a brand new external IP address ๐ญ. Reasons include a reboot, a power outage, a DHCP lease expiring, or the ISP just doing its own thing. If that happens, traffic that was headed to a host's old address might get delivered to a different host that now has that address. This hits every mapping type, implicit or explicit, ngl. Honestly this problem already exists today when a host's IP gets reassigned, even without PCP and without an ISP-run CGN. The fix is the same as always: renumbering problems are caused by host renumbering, so they go away if you avoid renumbering. PCP as defined in this document doesn't have any machinery to shrink the renumbering problem ๐ .
When an internal host changes its internal IP address (say its DHCP server hands it a different one), the NAT or firewall keeps sending traffic to the old address. The host usually stops getting traffic sent to that old address. So if it still wants to receive stuff, it has to install new mappings for its new address ๐ซถ. The Suggested External Port probably won't be fulfilled by the PCP server, because that port is still forwarding to the old address. So a mapping will likely get a new external port number and/or external IP address. Also, this renumbering isn't expected to happen routinely for most hosts, since most of them renew their DHCP leases before they expire (or re-request the same address after a reboot), and most DHCP servers say yes and give the host the same address it had before โจ.
A host might gain or lose interfaces while mappings are still active (plugging an Ethernet cable in or out, joining or leaving a WiFi network ๐). Because of that, if the PCP client is sending a PCP request to maintain state on the PCP server, it SHOULD make sure those requests keep using the same interface (for example when refreshing mappings). If the PCP client is sending a PCP request to create new state on the PCP server, it MAY use a different source interface or a different source address.
11.6. Learning the External IP Address Alone ๐ฅ
NAT-PMP [RFC6886] has a mechanism that lets clients learn just the external IP address, without also asking for a port mapping. NAT-PMP was built for residential NAT gateways, where that makes sense because a typical home gateway has only one external IP address. PCP has bigger main character energy ๐ and also supports Carrier-Grade NATs (CGNs), which may have a whole pool of external IP addresses, not just one. A client may not get any particular external IP address from that pool until it has at least one implicit, explicit, or static port mapping, and even then only for as long as that mapping stays valid.
If client software just wants to show the user's external IP address for cosmetic reasons, it can request a short-lived mapping (for example to the Discard service (TCP/9 or UDP/9) or some other port) and then display the resulting external IP address. But heads up bestie ๐: once that mapping expires, a later implicit or explicit dynamic mapping might map to a different external IP address.
12. PEER Opcode ๐ง
This section defines an Opcode for controlling dynamic outbound mappings.
PEER: Create a new dynamic outbound mapping to a remote peer's IP
address and port, or extend the lifetime of an existing
outbound mapping.
How this Opcode works is spelled out right here in this section ๐.
PCP servers SHOULD provide a configuration option to allow administrators to disable PEER support if they wish.
Okay so a mapping created or managed by PEER acts almost exactly like an implicit dynamic outbound mapping, the kind that pops up as a side effect of a packet (for example a TCP SYN) sent by the host ๐ฆ. That means mappings created or managed using PCP PEER requests may be endpoint-independent mapping (EIM) or endpoint-dependent mapping (EDM), with endpoint-independent filtering (EIF) or endpoint-dependent filtering (EDF). It all depends on what the NAT gateway or firewall already does for the implicit outbound mappings it creates on its own from watching outgoing traffic from internal hosts โจ.
12.1. PEER Operation Packet Formats
okay so the PEER Opcode lets a PCP client make a brand new explicit dynamic outbound mapping ๐ฆ (it works a lot like the outbound mapping that gets created implicitly when a host sends an outbound TCP SYN), or it can extend the lifetime of one that already exists โจ
Here's the Opcode layout for PEER. The request and response packets line up so that related fields sit at the same offsets, which is so clean ๐ญ
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Mapping Nonce (96 bits) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Protocol | Reserved (24 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Internal Port | Suggested External Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Suggested External IP Address (128 bits) |
| |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Remote Peer Port | Reserved (16 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Remote Peer IP Address (128 bits) |
| |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Figure 11: PEER Opcode Request
here's what each field does ๐
- Requested Lifetime (in common header): how long, in seconds, you want this mapping to last. ngl this is important: you cannot shrink a mapping's lifetime (or delete it with requested lifetime=0) using PEER ๐
- Mapping Nonce: a random value the PCP client picks ๐ฒ (see Section 12.2, "Generating a PEER Request"). Zero is technically legal, but it's super unlikely, about one in 2^96 requests ๐
- Protocol: the upper-layer protocol this Opcode is about. The values come from the IANA protocol registry [proto_numbers]. For example it's 6 (TCP) for a TCP mapping and 17 (UDP) for a UDP one. Protocol MUST NOT be zero. no zeros allowed, bestie ๐ โโ๏ธ
- Reserved: 24 reserved bits. They MUST be set to 0 on transmission and MUST be ignored on reception ๐ซถ
- Internal Port: the internal port for the mapping. Internal port MUST NOT be zero ๐ธ
- Suggested External Port: the external port you'd like. If the PCP client doesn't know it, or has no preference, it MUST use 0 ๐ง
- Suggested External IP Address: the external IP you'd like. If the PCP client doesn't know it, or has no preference, it MUST use the address-family-specific all-zeros address (see Section 5) ๐
- Remote Peer Port: the remote peer's port for the mapping. Remote peer port MUST NOT be zero ๐
- Reserved: 16 more reserved bits. They MUST be set to 0 on transmission and MUST be ignored on reception โจ
- Remote Peer IP Address: the remote peer's IP address, seen from the PCP client's point of view. This is the main character move ๐ because the client doesn't have to worry about NAT64 or NAT46, which both make the client's idea of the peer's IP differ from the peer's actual IP. This field lets the PCP client and server tell apart multiple connections coming from the same port on an internal host to different servers ๐ An IPv6 address goes in directly, and an IPv4 address goes in using the IPv4-mapped address syntax (Section 5).
when you're trying to re-create a lost mapping, the suggested external IP address and port get set to whatever External IP Address and Port the PCP server sent back in an earlier PEER response. On an initial PEER request, the external IP address and port are just set to zero ๐ฆ
also, semantics like the PREFER_FAILURE option are automatically implied by PEER requests ๐ฅ If the Suggested External IP Address or Suggested External Port is non-zero and the PCP server can't honor the suggested external IP address, protocol, or port, the PCP server MUST return a CANNOT_PROVIDE_EXTERNAL error response. The PREFER_FAILURE option is neither required nor allowed in PEER requests. If a PCP server gets a PEER request that contains the PREFER_FAILURE option, it MUST return a MALFORMED_REQUEST error response ๐
here's the PEER response diagram ๐
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Mapping Nonce (96 bits) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Protocol | Reserved (24 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Internal Port | Assigned External Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Assigned External IP Address (128 bits) |
| |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Remote Peer Port | Reserved (16 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Remote Peer IP Address (128 bits) |
| |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Figure 12: PEER Opcode Response
and the response fields ๐ซง
- Lifetime (in common header): on a success response, this is how long the mapping lasts, in seconds. On an error response, it tells clients how long they should assume they'll keep getting the same error if they repeat the same request โณ
- Mapping Nonce: copied from the request ๐
- Protocol: copied from the request ๐
- Reserved: 24 reserved bits. They MUST be set to 0 on transmission and MUST be ignored on reception ๐ท
- Internal Port: copied from the request ๐
- Assigned External Port: on a success response, this is the external port that was actually assigned to the mapping. On an error response, the suggested external port is copied from the request ๐ฏ
- Assigned External IP Address: on a success response, this is the external IPv4 or IPv6 address that was actually assigned to the mapping. On an error response, the suggested external IP address is copied from the request ๐
- Remote Peer Port: copied from the request ๐
- Reserved: 16 reserved bits. They MUST be set to 0 on transmission and MUST be ignored on reception ๐ซง
- Remote Peer IP Address: copied from the request ๐
12.2. Generating a PEER Request
this section is all about what the client does when it builds a message with the PEER Opcode ๐ง
the PEER Opcode MAY be sent before or after bidirectional communication with the remote peer has been set up ๐ฆ
if you send it before, it counts as a PEER-created mapping, which makes a brand new dynamic outbound mapping on the PCP-controlled device ๐ฑ
if you send it after, the PCP client gets to learn the IP address, port, and lifetime of the external address and port that were assigned to the implicit dynamic outbound mapping that already exists. It can also extend that lifetime, which helps reduce NAT or firewall keepalive messages (see Section 10.3) ๐
PEER requests are also really handy for restoring mappings after a NAT has lost its mapping state, like after a crash ๐ ๏ธ
the Mapping Nonce is randomly chosen by the PCP client. The randomness follows accepted practices for generating unguessable random numbers [RFC4086]. The nonce is used to validate PCP responses (see below) on the client side, and it's also used to validate mapping refreshes on the PCP server side ๐ The client MUST use a different mapping nonce for each PCP server it talks to. It's RECOMMENDED to pick a new random mapping nonce whenever the PCP client is initialized ๐ The client MAY use a different mapping nonce for every mapping ๐งฉ
the PEER Opcode has a Remote Peer Address field, and it's always written from the PCP client's point of view ๐ Heads up: when the PCP-controlled device is doing address family translation (NAT46 or NAT64), the remote peer address the PCP client sees is different from the remote peer address on the other side of the translation device. So keep track of which side you're looking at, bestie ๐ โจ
12.3. Processing a PEER Request โจ
Okay bestie, this is the server's game plan when a PEER request lands ๐ . Processing SHOULD be done in the order of the paragraphs below ๐ซถ.
The server copies these fields from the PEER request into its response:
- Protocol
- Internal Port
- Remote Peer IP Address
- Remote Peer Port
- Mapping Nonce
Some NATs and firewalls check destination addresses, and they won't create an implicit dynamic mapping if the destination is invalid (like 127.0.0.1) ๐. If a PCP-controlled device does that check for implicit mappings, it SHOULD also check the remote peer IP address, protocol, and port for PEER-created explicit dynamic mappings. If that check says the remote peer IP address of a PEER request is invalid, no mapping gets made and you get a MALFORMED_REQUEST error back ๐.
When the PCP server gets a PEER request, it looks through its mapping table for a match on the five-tuple { Protocol, Internal Address, Internal Port, Remote Peer Address, Remote Peer Port } ๐.
If nothing matches and the suggested external address and port are either zero or can be honored for the requested Protocol, the server creates a new mapping โจ. The reason is iconic: it avoids a race where the first outgoing packet reaches the NAT or firewall before the mapping exists, and it lets PEER recreate a lost outbound dynamic mapping (see Section 16.3.1, "Recreating Mappings"). After that, this PEER-created mapping is treated as if it were an implicit dynamic outbound mapping (like when the PCP client sends a TCP SYN), and the server returns a lifetime that fits that kind of mapping. Heads up ๐: on many NATs and firewalls, those lifetimes are super short until bidirectional traffic shows up.
If nothing matches and the suggested external address and port can't be honored, no new state gets created and the server returns CANNOT_PROVIDE_EXTERNAL ๐ญ.
If a mapping matches and no earlier PEER Opcode was successfully processed for it, the server ignores the Suggested External Address and Port values, adjusts that mapping's lifetime (more on that below), and returns info about the existing mapping ๐. This lets a client extend an existing mapping's lifetime and/or learn its external address, port, and lifetime. The mapping nonce gets remembered for this mapping.
If the server is operating in the Simple Threat Model (Section 18.1), and the internal port, protocol, and internal address match an existing mapping, but the mapping nonce doesn't match (meaning a previous PEER request was already processed), the request MUST be rejected with a NOT_AUTHORIZED error. The lifetime on that error shows how long the existing mapping lasts. The PCP server only needs to remember one Mapping Nonce value per mapping. This specification makes no statement about mapping nonces with the Advanced Threat Model.
The Lifetime value of the PEER Opcode is handled in Section 15, "Mapping Lifetime and Deletion" โณ. Sending a PEER request with a very short requested lifetime is a sneaky way to check how long an existing mapping lasts ๐ต๏ธ. So that PCP clients can send fewer NAT and firewall keepalive messages, it is RECOMMENDED that lifetimes of mappings created or lengthened with PEER be longer than the lifetimes of implicitly created mappings ๐ธ.
If everything above worked (no error response), the server sends a SUCCESS response, with the Lifetime field holding the mapping's lifetime ๐.
If a PEER-created or PEER-managed mapping isn't renewed using PEER, it goes back to the NAT's usual behaviour for implicit mappings. For example, continued outbound traffic keeps it alive, as per the NAT or firewall device's existing policy ๐ฆ. A PEER-created or PEER-managed mapping can be terminated at any time by action of the TCP client or server (e.g., due to TCP FIN or TCP RST), as per the NAT or firewall device's existing policy ๐.
12.4. Processing a PEER Response ๐ง
This section is the client's side: how it handles a response with the PEER Opcode ๐.
After the usual PCP response processing, the response gets matched with an outstanding PEER request. The client compares the internal IP address (the destination IP address of the PCP response, or whatever IP address is specified via the THIRD_PARTY option), the protocol, the internal port, the remote peer address, the remote peer port, and the mapping nonce ๐. Other fields aren't compared, because the PCP server sets those to describe the mapping the Opcode created. The PCP server will send a Mapping Update (Section 14.2) if the mapping changes (e.g., due to IP renumbering) ๐ฃ.
If the result code is NO_RESOURCES and the request was for creating or renewing a mapping, the PCP client SHOULD NOT send further requests for any new mappings to that PCP server for the (limited) value of the lifetime ๐
โโ๏ธ.
On a successful response, the application can use the assigned Lifetime value to reduce how often it sends application keepalives for that particular NAT mapping โญ. Of course, there may be other app-specific reasons to send keepalives more often ๐ . For example, the PCP assigned lifetime could be one hour, but the app might want to keep its server state (like "busy" / "away") updated more than once an hour. If the response shows an unexpected IP address or port (e.g., due to IP renumbering), the PCP client will want to re-establish its connection to its remote server ๐ญ.
If the PCP client wants to keep this mapping alive beyond the indicated lifetime, it MAY rely on continued inside-to-outside traffic to make sure the mapping continues to exist, or it MAY send a new PCP request before it expires โฐ. The recommended timings for renewing PEER mappings are the same as for MAP mappings, as described in Section 11.2.1 ๐ซถ.
Note: Implementations need to expect the PEER response may contain an external IP address with a different family than the remote peer IP address, e.g., when NAT64 or NAT46 are being used. ๐
13. Options for MAP and PEER Opcodes ๐
These options are for the MAP and PEER Opcodes. They MUST NOT appear with other Opcodes, unless those other Opcodes permit it ๐ โโ๏ธ.
13.1. THIRD_PARTY Option for MAP and PEER Opcodes ๐ฆ
This option is for when a PCP client wants to control a mapping to an internal host other than itself. It gets used with both MAP and PEER ๐ง.
Serious moment ๐จ: because of security concerns with the THIRD_PARTY option, this option MUST NOT be implemented or used unless the network the PCP messages are sent on is fully trusted. For example, if access control lists (ACLs) are installed on the PCP client, the PCP server, and the network between them, so those ACLs allow only communications from a trusted PCP client to the PCP server ๐.
A management device would use this option to control a PCP server on behalf of users ๐ผ. Picture a management device in a network operations center that shows a user interface to end users or network operations staff, and sends PCP requests with the THIRD_PARTY option to the appropriate PCP server โจ.
The THIRD_PARTY option is formatted like this:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Option Code=1 | Reserved | Option Length=16 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Internal IP Address (128 bits) |
| |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Figure 13: THIRD_PARTY Option
The fields are described below ๐:
Internal IP Address: Internal IP address for this mapping.
Option Name: THIRD_PARTY
Number: 1
Purpose: Indicates the MAP or PEER request is for a host other
than the host sending the PCP option.
Valid for Opcodes: MAP, PEER
Length: 16 octets
May appear in: request. May appear in response only if it
appeared in the associated request.
Maximum occurrences: 1
A THIRD_PARTY option MUST NOT contain the same address as the source address of the packet ๐
โโ๏ธ. This is because many PCP servers may not implement the THIRD_PARTY option at all, and with those servers, a client redundantly using the THIRD_PARTY option to specify its own IP address would cause such mapping requests to fail where they would otherwise have succeeded ๐ญ. A PCP server receiving a THIRD_PARTY option specifying the same address as the source address of the packet MUST return a MALFORMED_REQUEST result code ๐.
A PCP server MAY be configured to permit or to prohibit the use of the THIRD_PARTY option โ๏ธ. If this option is permitted, properly authorized clients may perform these operations on behalf of other hosts ๐ซถ. If this option is prohibited, and a PCP server receives a PCP MAP request with a THIRD_PARTY option, it MUST generate a UNSUPP_OPTION response ๐.
It is RECOMMENDED that customer premises equipment implementing a PCP server be configured to prohibit third-party mappings by default ๐ . With that default, if a user wants to create a third-party mapping, they need to interact out-of-band with their customer premises router (e.g., using its HTTP administrative interface) ๐ธ.
It is RECOMMENDED that service provider NAT and firewall devices implementing a PCP server be configured to permit the THIRD_PARTY option, when sent by a properly authorized host ๐ฅ. If the packet arrives from an unauthorized host, the PCP server MUST generate an UNSUPP_OPTION error ๐ซ.
Note that the THIRD_PARTY option isn't needed for today's common scenario of an ISP offering a single IP address to a customer who uses NAT to share that address locally ๐. In this setup, all the customer's hosts look like one single host from the ISP's point of view ๐ค.
When a PCP client uses the THIRD_PARTY option to make and maintain mappings on behalf of some other device, it may be beneficial, where possible, to verify that the other device is actually present and active on the network ๐. Otherwise, the PCP client risks maintaining those mappings forever, long after the device that needed them has gone ๐. That would defeat the whole purpose of PCP mappings having a finite lifetime, so they can be automatically deleted once they're no longer needed ๐งน.
13.2. PREFER_FAILURE Option for MAP Opcode ๐
Okay bestie, this option is only used with the MAP Opcode ๐
It basically says: if the PCP server can't map both the suggested external port and the suggested external address, the PCP server should not create a mapping. Without this option, the default vibe is that it does create a mapping anyway. Lowkey a big difference โจ
PREFER_FAILURE is never necessary for a PCP client to manage mappings for itself, and using it adds extra work for both the PCP client and the PCP server ๐ So it's not really the move for everyday use. It exists for interworking with non-PCP mapping protocols that have different semantics than PCP (e.g., UPnP IGDv1 interworking [PNP-IGD-PCP], where UPnP IGDv1 only lets the client dictate mapping a specific port), or for separate port allocation systems that allocate ports to a subscriber (e.g., a subscriber-accessed web portal run by the same ISP that runs the PCP server). A PCP server MAY support this option if its designers want to support those downstream devices or separate port allocation systems. PCP servers that aren't meant to interface with those systems are not required to support it. PCP clients other than UPnP IGDv1 interworking clients, or other than a separate port allocation system, SHOULD NOT use this option, because it results in inefficient operation, and they can't safely assume that every PCP server will implement it ๐ฅบ It's anticipated that this option will be deprecated down the road as more clients adopt PCP natively and the need for it fades ๐ธ
The PREFER_FAILURE option is formatted like this:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Option Code=2 | Reserved | Option Length=0 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Figure 14: PREFER_FAILURE Option
Option Name: PREFER_FAILURE
Number: 2
Purpose: indicates that the PCP server should not create an
alternative mapping if the suggested external port and address
cannot be mapped.
Valid for Opcodes: MAP
Length: 0
May appear in: request. May appear in response only if it
appeared in the associated request.
Maximum occurrences: 1
The result code CANNOT_PROVIDE_EXTERNAL is returned if the suggested external address, protocol, and port can't be mapped ๐ฌ That can happen because the external port is already mapped to another host's outbound dynamic mapping, an inbound dynamic mapping, or a static mapping, or because the same internal address, protocol, and port already have an outbound dynamic mapping that's mapped to a different external port than the one suggested. It can also happen because the external address is no longer available (e.g., due to renumbering). The server MAY set the lifetime in the response to the remaining lifetime of the conflicting mapping + TIME_WAIT [RFC0793], rounded up to the next larger integer number of seconds โณ
If a PCP request contains the PREFER_FAILURE option and has zero in the Suggested External Port field, it's invalid, full stop. The PCP server MUST reject such a message with the MALFORMED_OPTION error code ๐ซ
PCP servers MAY choose to rate-limit their handling of PREFER_FAILURE requests ๐ก๏ธ That protects them from a rapid flurry of 65535 consecutive PREFER_FAILURE requests from clients probing to find out which external ports are free.
There can be a race condition between the MAP Opcode using the PREFER_FAILURE option and Mapping Update (Section 14.2) ๐ For example, a previous host on the local network might have had the same internal address earlier, with a mapping for the same internal port. At about the same moment the current host sends a MAP Request using PREFER_FAILURE, the PCP server could send a spontaneous Mapping Update for the old mapping because of an external configuration change, and that could look like a reply to the new mapping request. So the PCP client MUST validate that the external IP address, protocol, port, and nonce in a success response match the associated suggested values from the request. If they don't match, it's because the Mapping Update was sent before the MAP request was processed ๐ซถ
13.3. FILTER Option for MAP Opcode ๐ง
This option is only used with the MAP Opcode ๐
This option says that filtering incoming packets is desired โจ The protocol being filtered is indicated by the Protocol field in the MAP Request. The remote peer IP address and remote peer port in the FILTER option indicate the permitted remote peer's source IP address and source port for packets from the Internet, and other traffic from other addresses gets blocked ๐ง The remote peer prefix length says how much of the remote peer's IP address is significant, so one option can permit an entire subnet ๐
After processing a MAP request that contains the FILTER option and generating a successful response, the PCP-controlled device will drop packets received on its public-facing interface that don't match the filter fields. After dropping the packet, if its security policy allows, the PCP-controlled device MAY also generate an ICMP error in response to the dropped packet ๐ โโ๏ธ
The FILTER option is kind of a performance optimization, and it's giving efficiency ๐ All software using PCP to receive incoming connections also has to handle the case where it might be directly connected to the Internet and receive unrestricted incoming TCP connections and UDP packets. So if it wants to restrict incoming traffic to a specific source address or group of source addresses, that software already needs to check the source address of incoming traffic and reject the unwanted stuff. The FILTER option is especially useful for battery-powered wireless devices, though, because they can save battery by not having to wake up just to reject unwanted traffic ๐
The FILTER option is formatted like this:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Option Code=3 | Reserved | Option Length=20 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Reserved | Prefix Length | Remote Peer Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Remote Peer IP address (128 bits) |
| |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Figure 15: FILTER Option Layout
These fields are described below:
Reserved: 8 reserved bits, MUST be sent as 0 and MUST be ignored when received.Prefix Length: indicates how many bits of the IPv4 or IPv6 address are relevant for this filter. The value 0 indicates "no filter", and will remove all previous filters. See below for detail.Remote Peer Port: the port number of the remote peer. The value 0 indicates "all ports".Remote Peer IP address: the IP address of the remote peer.
Option Name: FILTER
Number: 3
Purpose: specifies a filter for incoming packets
Valid for Opcodes: MAP
Length: 20 octets
May appear in: request. May appear in response only if it
appeared in the associated request.
Maximum occurrences: as many as fit within maximum PCP message
size
The Prefix Length tells you how many bits of the address are used for the filter ๐ง For IPv4 addresses (which are encoded using the IPv4-mapped address format (::FFFF:0:0/96)), that means valid prefix lengths are between 96 and 128 bits, inclusive. So just add 96 to the IPv4 prefix length. For IPv6 addresses, valid prefix lengths are between 0 and 128 bits, inclusive. Values outside those ranges make the PCP server return the MALFORMED_OPTION result code ๐
If multiple occurrences of the FILTER option exist in the same MAP request, they're processed in the order received (as per normal PCP option processing), and they MAY overlap the filtering requested ๐ If there's an existing mapping (with or without a filter) and the server receives a MAP request with FILTER, the filters in the new request get added to any existing filters. If a MAP request has a lifetime of 0 and contains the FILTER option, the error MALFORMED_OPTION is returned.
If any occurrences of the FILTER option in a request packet aren't successfully processed, an error is returned (e.g., MALFORMED_OPTION if one of the options was malformed). As with other PCP errors, returning an error means no state gets changed in the PCP server or in the PCP-controlled device, so nothing's left half-done ๐งผ
To remove all existing filters, use Prefix Length 0. There's no mechanism to remove a specific filter, so this is the only way ๐งน
To change an existing filter, the PCP client sends a MAP request containing two FILTER options. The first option has a prefix length of 0 (to delete all existing filters), and the second contains the new remote peer's IP address, protocol, and port. Any other FILTER options in that PCP request add more allowed remote peers ๐
The PCP server or the PCP-controlled device is expected to have a limit on the number of remote peers it can support. That limit might be as small as one ๐ฅฒ If a MAP request would exceed this limit, the entire MAP request is rejected with the result code EXCESSIVE_REMOTE_PEERS, and the state on the PCP server stays unchanged.
All PCP servers MUST support at least one filter per MAP mapping. No exceptions, periodt ๐ ๐ฅ
14. Rapid Recovery โจ
okay so PCP has this thing called rapid recovery ๐ฉน, and it lets clients fix broken mappings in seconds instead of the minutes or even hours it'd take if they just waited for the next routine renewal. Mappings can break when a NAT gateway reboots and forgets everything, or when its external IP address changes and the current mapping state stops being valid. Lowkey chaotic, but fixable!
Picture this: you're ssh'd into a remote machine ๐ป, your router needs a reboot (or you're swapping it for brand new hardware), and your ssh connection survives. That's the rapid recovery feature being iconic. ๐ฆ
Rapid recovery is a performance optimization on top of PCP's normal self-healing ๐ธ. Without it, clients still rebuild their correct state when they renew, but that can take hours instead of seconds, and it probably won't happen fast enough to stop active TCP connections from timing out ๐ญ.
There are two mechanisms for rapid recovery, and they're both below. If you don't implement and deploy one, app developers start feeling like they have to refresh their PCP state way more often than they should, which means extra network traffic ๐. So a PCP server that can lose state (say, after a reboot) or might have a mapping change (say, from IP renumbering) MUST implement either the Announce Opcode or the Mapping Update mechanism, and it SHOULD implement both ๐ซถ.
14.1. ANNOUNCE Opcode ๐ฃ
This rapid recovery mechanism uses the ANNOUNCE Opcode. When the PCP server loses its state (like after a reboot), it resets its Epoch time to its starting value (usually zero) and sends the ANNOUNCE response to the link-scoped multicast address (more on that address below) if there's a multicast network on its local interface. Or, if it's been configured with the IP address(es) and port(s) of PCP client(s), it sends unicast ANNOUNCE responses to those address(es) and port(s) instead.
ANNOUNCE isn't available on every network, though. Networks without a multicast link between the PCP server and its clients are out ๐ โโ๏ธ. Also, a PCP client can send an ANNOUNCE request (unicast), and it gets a unicast ANNOUNCE response just like any other Opcode.
When clients get PCP response packets with an anomalous Epoch time, they figure out that the server lost state and rebuild their lost mappings. Detective energy ๐ต๏ธโโ๏ธโจ.
14.1.1. ANNOUNCE Operation ๐ง
The PCP ANNOUNCE Opcode requests and responses have no Opcode-specific payload, so the length of the Opcode-specific data is zero ๐. The Requested Lifetime field in requests and the Lifetime field in responses are both set to 0 when sent, and ignored when received.
When a PCP server gets an ANNOUNCE request, it parses it first and generates a SUCCESS if parsing and processing go well. An error comes back if the client's IP Address field doesn't match the packet source address, or if the request is otherwise malformed, like a packet length under 24 octets ๐. Also, options MAY be sent with the PCP ANNOUNCE Opcode in the future, so PCP clients and servers need to be ready to receive options with ANNOUNCE too ๐.
Discussion: Client-to-server requests go from any client source port to listening UDP port5351on the server. Server-to-client multicast notifications go from the server's UDP port (5351) to listening UDP port5350on the client. Why two different ports? Because one device can wear multiple hats ๐ฉ. A multi-function home gateway might do NAT (PCP server) and printer sharing (which wants a PCP client). A home computer (PCP client) might also offer "Internet Sharing" (NAT), which means it needs to act as a PCP server. The software doing the server job might not be the same software doing the client job. The server side needs to listen for unicast client requests, but the client side needs to listen for multicast restart announcements. A lot of networking APIs make it really hard for two independent listeners to catch both unicasts and multicasts on the same port at the same time ๐ตโ๐ซ. So two ports it is. Problem solved, bestie ๐.
14.1.2. Generating and Processing a Solicited ANNOUNCE Message ๐
The PCP ANNOUNCE Opcode MAY be sent (unicast) by a PCP client. The Requested Lifetime value MUST be set to zero.
When the PCP server receives an ANNOUNCE Opcode and successfully parses and processes it, it generates a SUCCESS response with an assigned lifetime of zero.
This functionality lets a PCP client find out a server's Epoch, or check whether a PCP server is running, without changing the server's state ๐. Zero drama, zero side effects, iconic โจ.
14.1.3. Generating and Processing an Unsolicited ANNOUNCE Message ๐ข
When sending unsolicited responses, the ANNOUNCE Opcode MUST have a result code equal to zero (SUCCESS). The packet MUST be sent from the unicast IP address and UDP port number that PCP requests are received on, so that the PCP response processing in Section 8.3 accepts the message ๐ฏ. This message is most typically multicast, but it can also be unicast. Multicast PCP restart announcements go to 224.0.0.1:5350 and/or [ff02::1]:5350, as described below. Sending PCP restart announcements via unicast means the PCP server has to know the IP address(es) and port(s) of its listening clients. So unicast restart announcements only work for PCP servers that still remember their clients' addresses and ports even after they've lost the rest of their state ๐ง .
When a PCP server device that supports this reboots, restarts its NAT engine, or otherwise gets into a state where it may have lost some or all of its previous mapping state (or where it doesn't even know whether it had prior state it lost), it MUST tell PCP clients by unicasting or multicasting a gratuitous PCP ANNOUNCE Opcode response packet ๐ฃ, shown below, over the paths where it accepts PCP requests.
If it sends a multicast ANNOUNCE, the rules are:
- A PCP server device that accepts PCP requests over IPv4 sends the Restart Announcement to the IPv4 multicast address
224.0.0.1:5350(224.0.0.1is the All Hosts multicast group address). - A PCP server device that accepts PCP requests over IPv6 sends the Restart Announcement to the IPv6 multicast address
[ff02::1]:5350(ff02::1is for all nodes on the local segment). - A PCP server device that accepts PCP requests over both IPv4 and IPv6 sends a pair of Restart Announcements, one to each multicast address.
If it sends unicast ANNOUNCE messages instead, it sends an ANNOUNCE response message to the IP address(es) and port(s) of its PCP clients ๐.
Packets get lost, so to cover that, the PCP server device MAY transmit these packets (or packet pairs) up to ten times ๐. Each one carries an appropriate Epoch Time value to reflect the time that passed between transmissions. This is only allowed as long as the interval between the first two notifications is at least 250 ms, and the interval between subsequent notifications at least doubles โฑ๏ธ.
A PCP client that sends PCP requests to a PCP server over a multicast-capable path, implements the Restart Announcement feature, and wants to receive these announcements MUST listen for PCP Restart Announcements (gratuitous PCP ANNOUNCE Opcode response packets) on the appropriate multicast-capable interfaces where it sends PCP requests. It MAY also listen for unicast announcements from the server, using the UDP port it already uses to send unicast PCP requests to that server and receive unicast PCP responses from it ๐ซถ. The rules here:
- A PCP client device that sends PCP requests using IPv4 listens for packets sent to the IPv4 multicast address
224.0.0.1:5350. - A PCP client device that sends PCP requests using IPv6 listens for packets sent to the IPv6 multicast address
[ff02::1]:5350. - A PCP client device that sends PCP requests using both IPv4 and IPv6 listens for both types of Restart Announcement.
The SO_REUSEPORT socket option (or an equivalent) should be used for the multicast UDP port, if the host OS needs it to let multiple independent listeners share the same multicast UDP port ๐งโ๐คโ๐ง.
When a PCP client gets a unicasted or multicasted PCP ANNOUNCE Opcode response packet, it MUST (like it does with every received PCP response packet) check the announcement's source IP address. If the Epoch Time value is outside the expected range for that server, the client MUST wait a random amount of time between 0 and 5 seconds ๐ฒ. That random wait stops all the PCP clients from syncing up and stampeding the server at once. Then, for every PCP mapping it made at that server address, the client sends new PCP requests to rebuild any lost mapping state ๐ง. The Suggested External IP Address and Suggested External Port fields in the client's renewal requests let the client remind the restarted server of the mappings it had before, so in lots of cases the old state can be recreated ๐งฉ. For servers that reboot fairly quickly, it's usually possible to rebuild lost state fast enough that existing TCP connections and UDP communications don't time out, and everything just keeps going ๐.
As with every PCP response message, if the Epoch Time value is within the expected range for that server, the PCP client does not recreate its mappings. Also, as with every PCP response message, after receiving and validating the ANNOUNCE message, the client updates its own Epoch time for that server, as described in Section 8.5 ๐ .
14.2. PCP Mapping Update โจ
okay so this rapid recovery mechanism is for when the PCP server remembers its state but realizes its existing mappings are no longer valid ๐ญ (like when IP renumbering changes the external IP address of a PCP-controlled NAT) ๐ธ
it's anticipated that servers that get routinely reconfigured by an administrator, or that have their WAN address changed a lot, will implement this feature (e.g., residential CPE routers) ๐ . servers that aren't routinely reconfigured will not implement this feature (e.g., service provider-operated CGN) ๐.
if a PCP server device has not forgotten its mapping state, but for some other reason figures out that some or all of its mappings are unusable (e.g., when a home gateway gets a different external IPv4 address from the upstream DHCP server), then the PCP server device automatically repairs its mappings and notifies its clients by following the procedure below ๐.
for PCP-managed mappings, for each one the PCP server device should update the external IP address and external port to appropriate available values, and then send unicast PCP MAP or PEER responses (whichever fits the mapping) to tell the PCP client about the new external IP address and external port ๐ฆ. these unsolicited responses are identical to the MAP or PEER responses that normally come back for client MAP or PEER requests, with newly updated External IP Address and External Port values, and they go to the same client IP address and port the server used for the prior response for that mapping โจ.
- if the earlier associated request contained the THIRD_PARTY option, the THIRD_PARTY option MUST also appear in the Mapping Update, because the PCP client needs it to disambiguate the response ๐ง
- if the earlier associated request contained the PREFER_FAILURE option, and the same external IP address, protocol, and port can't be provided, the error
CANNOT_PROVIDE_EXTERNALSHOULD be sent ๐ - if the earlier associated request contained the FILTER option, the filters get moved to the new mapping and the FILTER option is sent in the Mapping Update response ๐
- non-mandatory options SHOULD NOT be sent in the Mapping Update response ๐
Discussion: It could have been possible to design this so that the PCP server (1) sent an ANNOUNCE Opcode to the PCP client, the PCP client reacted by (2) sending a new MAP request and (3) receiving a MAP response. Instead, the server can create a shortcut for that design by simply sending the message it would have sent in (3). big brain energy ๐ง โจ
to handle packet loss, the PCP server device SHOULD transmit such packets three times, with an appropriate Epoch Time value in each to reflect the passage of time between transmissions ๐. the interval between the first two notifications MUST be at least 250 ms, and the third packet goes out after a 500-ms interval โฑ๏ธ. once the PCP server has received a refreshed state for that mapping, the PCP server SHOULD cease those retransmissions for that mapping, because there's no point continuing to send messages about a mapping that's already sorted ๐
.
when a PCP client gets an updated MAP or PEER response, it uses the info in the response to adjust rendezvous servers or reconnect to servers, respectively ๐ซถ. for MAP, that means updating the DNS entries or other address and port info recorded with some kind of application-specific rendezvous server. for PEER responses giving a CANNOT_PROVIDE_EXTERNAL error, it typically means establishing new connections to servers. anytime the external address or port changes, existing TCP and UDP connections will be lost. PCP can't avoid that, but it does give immediate notification of the event to lessen the impact ๐ญ๐ธ
15. Mapping Lifetime and Deletion ๐
the PCP client requests a certain lifetime, and the PCP server responds with the assigned lifetime ๐ฆ. the PCP server MAY grant a lifetime smaller or larger than the requested one. the PCP server SHOULD be configurable for permitted minimum and maximum lifetimes, and the minimum value SHOULD be 120 seconds โญ. the maximum value SHOULD be the remaining lifetime of the IP address assigned to the PCP client if that info is available (e.g., from the DHCP server), or half the lifetime of IP address assignments on that network if the remaining lifetime isn't available, or 24 hours ๐ง. excessively long lifetimes can cause port consumption even if the internal host isn't interested in the traffic anymore or isn't connected to the network anymore ๐. these recommendations aren't strict, so deployments should evaluate the trade-offs to set their own minimum and maximum Lifetime values ๐ .
once a PCP server has responded positively to a MAP request for a certain lifetime, the port mapping stays active for the duration of that lifetime, unless the PCP client reduces the lifetime (to a shorter one or to zero) or the PCP server loses its state (e.g., crashes) ๐. mappings created by PCP MAP requests aren't special or different from mappings created in other ways. in particular, it's implementation-dependent whether outgoing traffic extends the lifetime of such mappings beyond the PCP-assigned lifetime ๐คทโโ๏ธ. PCP clients MUST NOT depend on this behavior to keep mappings active, and MUST explicitly renew their mappings as required by the Lifetime field in PCP response messages ๐.
upon receipt of a PCP response with an absurdly long assigned lifetime, the PCP client SHOULD behave as if it received a more sane value (e.g., 24 hours) and renew the mapping accordingly ๐. this makes sure that if the static mapping gets removed, the client keeps maintaining the mapping it wants ๐.
an application that forgets its PCP-assigned mappings (e.g., the application or OS crashes) will request new PCP mappings ๐. this may eat up port mappings if the application binds to a different internal port every time it runs. the application will also likely start new outbound TCP connections, which create implicit dynamic outbound mappings without using PCP, and those will eat up port mappings too. if there's a port mapping quota for the internal host, frequent restarts like this can exhaust the quota ๐ญ.
to help clean up PCP state, when the PCP-controlled device is collocated with the address assignment (DHCP) server, like in a typical residential CPE, it is RECOMMENDED that when an IP address becomes invalid (e.g., the DHCP lease expires, or the DHCP client sends an explicit DHCP RELEASE), the PCP-controlled device SHOULD also discard any dynamic mapping state relating to that expired IP address ๐งน.
when using NAT, the same external port may get assigned to different internal hosts at different times ๐ฆ. for example, if an internal host using an external port stops sending traffic on it, its mapping may expire, and then later that same external port may be assigned to a new internal host. the new internal host could then receive incoming traffic that was meant for the previous one. this generally happens by accident, and the reassignment only happens after the current holder has stopped using the port for some period of time. it would be totally unacceptable if an attacker could use PCP to speed up this reassignment on purpose in order to steal traffic meant for the current holder, by (i) spoofing PCP requests using the current holder's source IP address and mapping nonce to fraudulently delete the mapping or shorten its lifetime, and then (ii) claiming the external port for itself ๐จ๐ค.
so, in the simple security model, to protect against this attack, PCP MUST NOT allow a PCP request (even one that appears to come from the current holder of the mapping) to cause a mapping to expire sooner than it would have expired naturally because of outbound traffic keeping the mapping active ๐ก๏ธ. a PCP server MUST set the lifetime of a mapping to no less than the remaining time before the mapping would expire if no further outbound traffic is seen for that mapping. this means a MAP or PEER request with a lifetime of 0 will only set the assigned lifetime to 0 (i.e., delete the mapping) if the internal host hasn't sent a packet using that mapping for the idle-timeout time. otherwise, the assigned lifetime will be the remaining idle-timeout time ๐.
finally, to reduce unwanted traffic and data corruption for both TCP and UDP, the assigned external port created by the MAP Opcode or PEER Opcode SHOULD NOT be reused for an interval equal to the reuse time limit enforced by the NAT for its implicit dynamic mappings (typically, the maximum TCP segment lifetime of 2 minutes [RFC0793]) ๐ธ. also, to reduce port stealing attacks, the assigned external port SHOULD NOT be reused for an interval equal to the time the PCP-controlled device would normally keep an idle (no traffic) implicit dynamic mapping (e.g., 2 minutes for UDP [RFC4787] and 124 minutes for TCP [RFC5382]) ๐ต๏ธโโ๏ธ. however, within these time windows, the PCP server SHOULD allow an external port to be reclaimed by the same client, where "same client" means "same internal IP address, internal port, and mapping nonce" ๐ซถ.
15.1. Lifetime Processing for the MAP Opcode ๐ฆ
if the requested lifetime is zero, then:
- if both the protocol and internal port are non-zero, it means a request to delete the indicated mapping immediately ๐๏ธ
- if the protocol is non-zero and the internal port is zero, it means a request to delete a previous 'wildcard' (all-ports) mapping for that protocol. the nonce MUST match the nonce used to create the 'wildcard' mapping ๐
- if both the protocol and internal port are zero, it means a request to delete a previous 'DMZ host' (all incoming traffic for all protocols) mapping. the nonce MUST match the nonce used to create the 'DMZ host' mapping ๐
- if the protocol is zero and the internal port is non-zero, then the request is invalid and the PCP server MUST return a
MALFORMED_REQUESTerror to the client ๐ โโ๏ธ
in requests where the requested Lifetime is 0, the Suggested External Address and Suggested External Port fields MUST be set to zero on transmission and MUST be ignored on reception, and these fields MUST be copied into the assigned external IP address and assigned external port of the response ๐งผ.
PCP MAP requests can only delete or shorten lifetimes of MAP-created mappings ๐. if the PCP client tries to delete a static mapping (i.e., a mapping created outside of PCP itself), or an outbound (implicit or PEER-created) mapping, the PCP server MUST return NOT_AUTHORIZED ๐ซ. if the PCP client tries to delete a mapping that doesn't exist, the SUCCESS result code is returned (this is necessary so PCP returns the same response for retransmissions or duplicates of the same request) ๐. if the deletion request was properly formatted and successfully processed, a SUCCESS response is generated with the protocol and internal port number copied from the request, and the response lifetime set to zero ๐ฅณ. an inbound mapping (i.e., a static mapping or MAP-created dynamic mapping) MUST NOT have its lifetime reduced by transport protocol messages (e.g., TCP RST, TCP FIN) ๐. note the THIRD_PARTY option (Section 13.1), if authorized, can also delete PCP-created MAP mappings ๐งโโ๏ธโจ
16. Implementation Considerations โจ
Okay bestie, Section 16 is all non-normative guidance, so think of it as friendly tips from people who've already been burned ๐ซถ Here's what might be useful for implementers.
16.1. Implementing MAP with EDM Port-Mapping NAT
Okay so for implicit dynamic outbound mappings, some NAT devices have endpoint-independent mapping (EIM) behavior, and others have endpoint-dependent mapping (EDM) behavior. EIM NATs don't have the problem this section is about, and the IETF strongly encourages EIM behavior ๐ [RFC4787][RFC5382].
With EDM NAT devices, the same external port can get used by an outbound dynamic mapping and an inbound dynamic mapping (from the same internal host or from a different internal host). Now the MAP Opcode gets messy ๐ There are two ways people have pictured implementing it:
- Have outbound mappings use a different set of external ports than inbound mappings (for example, the ones created with MAP), so the two don't step on each other as much; or
- When a packet arrives (inbound from the Internet or outbound from an internal host), first try to process it with a dynamic outbound mapping. If none match, try an inbound mapping instead. This basically 'prioritizes' outbound mappings over inbound ones ๐
16.2. Lifetime of Explicit and Implicit Dynamic Mappings
No matter if a NAT is EIM or EDM, one (or more) outbound mappings that use the same internal port on the internal host might get created before or after a MAP request. When that happens, it's important that the NAT honor the lifetime returned in the MAP response. Specifically, if an inbound mapping was created with the MAP Opcode, the implementation needs to make sure that ending an outbound mapping (for example, via a TCP FIN handshake) doesn't prematurely destroy that MAP-created inbound mapping ๐ญ
16.3. PCP Failure Recovery
If something happens that makes the PCP server lose its dynamic mapping state (like a crash or a power outage), the mappings created by PCP are lost ๐ Occasional state loss might be unavoidable in a residential NAT device that doesn't write transient information to non-volatile memory. It's expected to be rare in a service provider environment, thanks to redundant power, disk drives for storage, and so on. Still, outright failure of service provider equipment (for example, a software malfunction) can make state get lost too.
The Epoch time lets a client figure out when a PCP server may have lost its state. When the Epoch Time value is seen outside the expected range, the PCP client can try to recreate its mappings following the procedures in this section โจ
Further analysis of PCP failure scenarios is planned for a future document [PCP-FAIL].
16.3.1. Recreating Mappings
A mapping renewal packet is formatted exactly like an original mapping request. From the client's point of view, it's a renewal of an existing mapping. From the point of view of a newly rebooted PCP server, though, it looks like a brand new mapping request ๐ฆ When a PCP client routinely renews its mappings before they expire, it'll automatically recreate all its lost mappings.
When the PCP server loses state and starts processing new PCP messages, its Epoch time resets and starts counting again. If a client receives a packet where the Epoch Time field is outside the expected range (Section 8.5), that means a reboot or similar state loss happened. The client can then renew its port mappings sooner, without waiting for the normal routine renewal time ๐โโ๏ธ
16.3.2. Maintaining Mappings
A PCP client refreshes a mapping by sending a new PCP request that contains information it learned from the earlier PCP response. The PCP server responds with the new lifetime. It's possible, because of reconfiguration or failure of the PCP server, that the external IP address and/or external port, or the PCP server itself, has changed (for example, due to a new route to a different PCP server). These events are rare, but they're not an error ๐ธ The PCP server will just return a new external address and/or external port to the client, and the client should record this new external address and port with its rendezvous service.
To notice these changes faster, a server that needs extremely high availability may find it useful to use shorter lifetimes in its PCP mapping requests, so it talks to the PCP server more often. This is an engineering trade-off based on (i) the acceptable downtime for the service in question, (ii) the expected likelihood of NAT or firewall state loss, and (iii) the amount of PCP maintenance traffic that's acceptable โ๏ธ
If the PCP client has several mappings, it only needs to retrieve the Epoch Time value for one of them to figure out whether the PCP server might have suffered a catastrophic loss of state ๐ง If the client wants to check the PCP server's Epoch time, it sends a PCP request for any one of its mappings, and that returns the current Epoch Time value. In that request, the PCP client can either extend the mapping lifetime (by asking for more time) or keep the current lifetime (by asking for the same number of seconds it knows are left on the lifetime).
If a PCP client changes its internal IP address (for example, because the internal host moved to a new network) and still wants to receive incoming traffic, it needs to create new mappings on that new network. New mappings will typically also require an update to the application-specific rendezvous server if the external address or port is different from the previous values (see Sections 10.1 and 11.5) ๐ฎ
16.3.3. SCTP
Even though SCTP has port numbers like TCP and UDP, SCTP works differently behind an address-sharing NAT, because SCTP port numbers are not changed [SCTPNAT]. Outbound dynamic SCTP mappings use the verification tag of the association instead of the local and remote peer port numbers. Just like with TCP, explicit outbound mappings can be made to reduce keepalive intervals, and explicit inbound mappings can be made by passive listeners that expect to receive new associations at the external port ๐
An SCTP-aware NAT does not (currently) rewrite SCTP port numbers, so it can't assign an external port that's different from the client's internal port. A PCP client making a MAP request for SCTP needs to be aware of this restriction ๐ซฃ The PCP client SHOULD make its SCTP MAP request just like it would for a TCP MAP request: in its initial PCP MAP request it SHOULD specify zero for the external address and port, and then in later renewals it SHOULD echo the assigned external address and port.
But here's the catch: since a current SCTP-aware NAT can only assign an external port that matches the internal port, it might not be able to do that if the external port is already assigned to a different PCP client. This is likely if there's more than one instance of a given SCTP service on the local network, because both instances will probably listen on the same well-known SCTP port on their respective hosts, but they can't both have the same external port on the NAT gateway's external address ๐ฉ A particular external port might also not be assignable for other reasons, like when the NAT device itself is already using it, or when policy prohibits it, as described in Section 11.3, "Processing a MAP Request". If the external port matching the internal port can't be assigned (and the SCTP-aware NAT doesn't do SCTP port rewriting), the SCTP-aware NAT MUST return a CANNOT_PROVIDE_EXTERNAL error to the requesting PCP client. Note that this puts an extra burden on the SCTP server whose MAP request failed, because it then has to tear down its existing listening socket and try again with a different internal port, over and over, until it finds an external port it can use ๐
The SCTP complications above happen because of address sharing. They're avoided when address sharing is avoided (for example, with 1:1 NAT or a firewall) โจ
16.4. Source Address Replicated in PCP Header
Every PCP request includes the PCP client's IP address, replicated in the PCP header. That's how unexpected address rewriting (NAT) on the path between the PCP client and its PCP server gets detected ๐ On operating systems that support the sockets API, the following steps are RECOMMENDED for a PCP client to insert the correct source address in the PCP header:
- Create a UDP socket.
- Call
connecton this UDP socket using the address and port of the desired PCP server. - Call the
getsockname()function to retrieve a sockaddr containing the source address the kernel will use for UDP packets sent through this socket. - If the IP address is an IPv4 address, encode the address into an IPv4-mapped IPv6 address. Place the IPv4-mapped IPv6 address or the native IPv6 address into the PCP Client's IP Address field in the PCP header.
- Send PCP requests using this connected UDP socket ๐
16.5. State Diagram โจ
okay so this one's the state machine for every mapping entry on the PCP-controlled device ๐ฆ quick heads up: it's non-normative, so it's a helpful picture, not a rule you have to follow.
CLOSE_MSG or
(NO_TRAFFIC and EXPIRY) +---------+ NO_TRAFFIC and EXPIRY
+-------------->| |<------------+
| |NO_ENTRY | |
| +-----------| |---------+ |
| | +---------+ | |
| | ^ | | |
| | NO_TRAFFIC | | | |
| | or | | | |
| | CLOSE_MSGS | | | |
| | | | | |
| |PEER request | | MAP request| |
| V | | V |
+---------+ | | +---------+
+-->| "P", | | | M-R | "M", |<--+
P-R | | PEER |-----------|--|-------->| MAP | | M-R or
+---| mapping| | | | mapping|---+ P-R or
+---------+ | | +---------+ CLOSE_MSGS
| ^ | | ^ |
| |PEER request | | MAP request| |
| | | | | |
| | | | | |
| | | | | |
| | | | outbound | |
| | | | TRAFFIC | |
| | | V | |
| | +---------+ | |
| +-----------| "I", |---------+ |
| | implicit| |
+-------------->| mapping |<------------+
TRAFFIC and EXPIRY +---------+ TRAFFIC and EXPIRY
Figure 16: PCP State Diagram
here's what all the bits mean, bestie ๐ซถ
NO_ENTRY: Invalid state that means the entry doesn't exist. It's the only possible start state ๐งM-R: MAP requestP-R: PEER requestM: Mapping entry when it gets created by a MAP requestP: Mapping entry when it gets created or managed by a PEER requestI: Implicit mapping created by an outgoing packet from the client (like a TCP SYN) ๐ this is also the state when a PCP-created mapping's lifetime runs out while there's still active trafficEXPIRY: PEER or MAP lifetime has expiredTRAFFIC: Traffic the PCP-controlled device sees using this entry within that entry's expiry time. It can be inbound or outboundNO_TRAFFIC: Means there is no TRAFFICCLOSE_MSG: Protocol messages from the client or server that close the session (like a TCP FIN or TCP RST), handled however the NAT or firewall device handles those messages ๐ฅ
Notes on the diagram:
- Okay, the 'and' clause means both events on either side of 'and' are needed for the transition. The 'or' clause means just one of the events is enough for the transition ๐
- Moving from state M to state I is implementation dependent ๐ธ
17. Deployment Considerations ๐
17.1. Ingress Filtering ๐ฆ
so, same deal as implicit dynamic mappings made by outgoing TCP SYN packets: explicit dynamic mappings made through PCP use the packet's source IP address as the internal address for the mapping ๐ which means ingress filtering [RFC2827] SHOULD be used on the path between the internal host and the PCP server, so nobody can sneak spoofed packets onto that path ๐
17.2. Mapping Quota ๐
on PCP-controlled devices that create state when a mapping gets made (like NAT), the PCP server SHOULD keep per-host and/or per-subscriber quotas for mappings ๐ how it handles quotas is implementation specific. The server can use a separate quota for implicit, explicit, and static mappings, one combined quota for all of them, or some other policy entirely โจ
18. Security Considerations ๐
okay this section is SERIOUS but I'm still gonna make it fun ๐ญ The goal of PCP is to help end nodes control their own NAT state, and to make NAT mappings more efficient and better at handling errors compared to the existing implicit mapping mechanisms in NAT boxes and stateful firewalls. The security goal is to limit any new denial-of-service opportunities and avoid introducing new attacks that could cause unauthorized changes to mapping state. One of the most serious results of unauthorized mapping changes is traffic theft ๐ Every mapping that a specific host could create using implicit mapping mechanisms is considered authorized already. Confidentiality of mappings is not a requirement, even when PCP messages travel over paths the mapped traffic would never take ๐ซถ
18.1. Simple Threat Model ๐ง
PCP servers are secure against off-path attackers who can't spoof a packet the PCP server would see as coming from the internal network ๐ PCP clients are secure against off-path attackers who can spoof the PCP server's IP address ๐ก๏ธ
Defending against attackers who can modify or drop packets between the internal network and the PCP server, or who can inject spoofed packets that look like they came from the internal network, is out of scope. An attacker like that can redirect traffic to a host of their choosing ๐ฌ
A PCP server is secure under this threat model if it is constrained so that it doesn't configure any explicit mapping it wouldn't configure implicitly. Mostly, this means PCP servers running on NAT boxes or stateful firewalls that support the PEER and MAP Opcodes can be secure under this threat model if (1) all of their hosts are within a single administrative domain (or if the internal hosts can be securely partitioned into separate administrative domains, like in the DS-Lite B4 case), (2) explicit mappings are created with the same lifetime as implicit mappings, and (3) the THIRD_PARTY option is not supported. PCP servers can also securely support the MAP Opcode under this threat model if the security policy on the device running the PCP server would permit endpoint-independent filtering of implicit mappings ๐ฆ
PCP servers that comply with the Simple Threat Model and don't implement a PCP security mechanism described in Section 18.2 MUST enforce the constraints described in the paragraph above ๐ฅ
18.1.1. Attacks Considered ๐
- If you let multiple administrative domains send PCP requests to a single PCP server that doesn't enforce a boundary between the domains, a node in one domain could run a denial-of-service attack on other domains or capture traffic meant for a node in another domain ๐ฑ
- If explicit mappings have longer lifetimes than implicit mappings, it's easier to pull off a denial-of-service attack than it would be without the PCP server around ๐ฅ
- If the PCP server supports deleting or reducing the lifetime of existing mappings, an attacking node could steal an existing mapping and receive traffic that was meant for another node ๐ซฃ
- If the THIRD_PARTY option is supported, it also lets an attacker open a window for an external node to attack an internal node, steal traffic meant for another node, or possibly facilitate a denial-of-service attack. One example of how THIRD_PARTY could give an attacker more power than a spoofed implicit mapping: the PCP server (especially one running in a service provider's network) might not know about internal filtering that would stop an equivalent implicit mapping from being spoofed, like filtering between a guest network and a corporate network ๐
- If the PCP server supports the MAP Opcode where the security policy wouldn't support endpoint-independent filtering of implicit mappings, the MAP Opcode changes the security properties of the device running the PCP server by allowing explicit mappings that break the security policy ๐จ
18.1.2. Deployment Examples Supporting the Simple Threat Model ๐ง
this section gives two examples of how the Simple Threat Model can work in real-world deployments โจ
18.1.2.1. Residential Gateway Deployment ๐
parity with lots of residential gateways out there today can be reached with a PCP server constrained as described in Section 18.1 above ๐ธ
18.2. Advanced Threat Model ๐
in the Advanced Threat Model, PCP makes sure attackers (on-path or off-path) can't create unauthorized mappings or make unauthorized changes to existing mappings. The protocol must also limit the chances of on-path or off-path attackers pulling off denial-of-service attacks ๐ก๏ธ
The Advanced Threat Model is needed in these cases:
- Security infrastructure equipment, like corporate firewalls, that doesn't create implicit mappings.
- Equipment (like CGNs or service provider firewalls) that serves multiple administrative domains and has no way to securely partition traffic from those domains.
- Any implementation that wants to be more permissive about authorizing explicit mappings than it is about authorizing implicit mappings.
- Implementations that want to support any deployment scenario that doesn't meet the constraints in Section 18.1.
To protect against attacks under this threat model, a PCP security mechanism that provides an authenticated, integrity-protected signaling channel would need to be specified ๐
PCP servers that implement a PCP security mechanism MAY accept unauthenticated requests. In their default configuration, PCP servers implementing the PCP security mechanism MUST still enforce the constraints described in Section 18.1 when processing unauthenticated requests ๐ซถ
18.3. Residual Threats ๐ง๏ธ
this section covers some threats that the two threat models above don't address, and suggests mitigation strategies for them ๐
18.3.1. Denial of Service ๐ซ
because the state created in a NAT or firewall is a thing, a per-host and/or per-subscriber quota will likely exist for both implicit dynamic mappings and explicit dynamic mappings. A host might make way too many implicit or explicit dynamic mappings, eating up an inordinate number of ports and causing a denial of service for other hosts ๐ค So Section 17.2 recommends that hosts be limited to a reasonable number of explicit dynamic mappings ๐
an attacker on the path between the PCP client and PCP server can drop PCP requests, drop PCP responses, or spoof a PCP error, and all of those effectively deny service ๐ญ Through these actions, the PCP client might not even know the PCP server actually processed the PCP request. An attacker sending a NO_RESOURCES error can make the PCP client stop sending messages to that server for a while ๐ฅบ There's no mitigation for this on-path attacker, sadly ๐
18.3.2. Ingress Filtering ๐ง
Okay bestie, this one is SO important ๐ญ. A host must NOT be able to sneakily create, delete, or refresh a mapping (or filtering) for someone else's host, because that can expose the other host to unwanted traffic, stop it from getting wanted traffic, or eat up the other host's mapping quota ๐. Both implicit and explicit dynamic mappings get made based on the source IP address in the packet, so they depend on ingress filtering to guard against spoof source IP addresses. Ingress filtering is the bouncer here ๐ซถ.
18.3.3. Mapping Theft ๐ฆน
This one is lowkey scary ๐. After a PCP server loses state, there's a gap before the PCP client notices the lower-than-expected Epoch Time value. During that gap, another host can snag the PCP client's mapping, either via an explicit dynamic mapping or an implicit dynamic mapping. Then incoming traffic gets sent to a different host, and that's what we call "theft" ๐ .
Rapid recovery shrinks that gap, but it doesn't completely eliminate the threat. The PCP client can shrink the gap too by using a relatively short lifetime, but that means more PCP chatter ๐. This threat is reduced by using persistent storage of explicit dynamic mappings in the PCP server (so it does not lose explicit dynamic mapping state), or by ensuring that the previous external IP address, protocol, and port cannot be used by another host (e.g., by using a different IP address pool) ๐.
18.3.4. Attacks against Server Discovery ๐
This document does not specify server discovery, beyond contacting the default gateway. That's it, that's the tea โ.
19. IANA Considerations ๐
IANA has performed the following actions.
19.1. Port Number ๐
PCP uses ports 5350 and 5351, previously assigned by IANA to NAT-PMP [RFC6886]. IANA has reassigned those ports to PCP. Port glow-up complete โจ.
19.2. Opcodes ๐ง
IANA has created a new protocol registry for PCP Opcodes, numbered 0-127, initially populated with the values:
Value Opcode
----- -------------------------
0 ANNOUNCE
1 MAP
2 PEER
3-31 Standards Action [RFC5226]
32-63 Specification Required [RFC5226]
96-126 Reserved for Private Use [RFC5226]
127 Reserved, Standards Action [RFC5226]
The value 127 is Reserved and may be assigned via Standards Action [RFC5226]. The values in the range 3-31 can be assigned via Standards Action [RFC5226], 32-63 via Specification Required [RFC5226], and the range 96-126 is for Private Use [RFC5226] ๐.
19.3. Result Codes ๐
IANA has created a new registry for PCP result codes, numbered 0-255, initially populated with the result codes from Section 7.4. The value 255 is Reserved and may be assigned via Standards Action [RFC5226].
The values in the range 14-127 can be assigned via Standards Action [RFC5226], 128-191 via Specification Required [RFC5226], and the range 191-254 is for Private Use [RFC5226] ๐ธ.
19.4. Options ๐ญ
IANA has created a new registry for PCP options, numbered 0-255, each with an associated mnemonic. The values 0-127 are mandatory to process, and 128-255 are optional to process. The initial registry contains the options described in Section 13. The option values 0, 127, and 255 are Reserved and may be assigned via Standards Action [RFC5226].
Additional PCP option codes in the ranges 4-63 and 128-191 can be created via Standards Action [RFC5226], the ranges 64-95 and 192-223 are for Specification Required [RFC5226], and the ranges 96-126 and 224-254 are for Private Use [RFC5226].
Documents describing an option should describe the processing for both the PCP client and server, and the information below:
Option Name: <mnemonic>
Number: <value>
Purpose: <textual description>
Valid for Opcodes: <list of Opcodes>
Length: <rules for length>
May appear in: <requests/responses/both>
Maximum occurrences: <count>
20. Acknowledgments ๐
Thanks to Xiaohong Deng, Alain Durand, Christian Jacquenet, Jacni Qin, Simon Perreault, James Yu, Tina TSOU (Ting ZOU), Felipe Miranda Costa, James Woodyatt, Dave Thaler, Masataka Ohta, Vijay K. Gurbani, Loa Andersson, Richard Barnes, Russ Housley, Adrian Farrel, Pete Resnick, Pasi Sarolahti, Robert Sparks, Wesley Eddy, Dan Harkins, Peter Saint-Andre, Stephen Farrell, Ralph Droms, Felipe Miranda Costa, Amit Jain, and Wim Henderickx for their comments and review. You all are the real MVPs ๐ซถ.
Thanks to Simon Perreault for highlighting the interaction of dynamic connections with PCP-created mappings and for many other review comments.
Thanks to Francis Dupont for his several thorough reviews of the specification, which improved the protocol significantly ๐.
Thanks to T. S. Ranganathan for the state diagram.
Thanks to Peter Lothberg for clock skew information, which guided the choice of tolerance levels for deciding when an Epoch time should be considered to be anomalous โฐ.
Thanks to Margaret Wasserman and Sam Hartman for writing the Security Considerations section ๐.
Thanks to authors of DHCPv6 for retransmission text.
21. References ๐
21.1. Normative References
- [RFC0768] Postel, J., "User Datagram Protocol", STD 6, RFC 768, August 1980.
- [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate Requirement Levels", BCP 14, RFC 2119, March 1997.
- [RFC2827] Ferguson, P. and D. Senie, "Network Ingress Filtering: Defeating Denial of Service Attacks which employ IP Source Address Spoofing", BCP 38, RFC 2827, May 2000.
- [RFC4086] Eastlake, D., Schiller, J., and S. Crocker, "Randomness Requirements for Security", BCP 106, RFC 4086, June 2005.
- [RFC4193] Hinden, R. and B. Haberman, "Unique Local IPv6 Unicast Addresses", RFC 4193, October 2005.
- [RFC4291] Hinden, R. and S. Deering, "IP Version 6 Addressing Architecture", RFC 4291, February 2006.
- [RFC5226] Narten, T. and H. Alvestrand, "Guidelines for Writing an IANA Considerations Section in RFCs", BCP 26, RFC 5226, May 2008.
- [RFC6056] Larsen, M. and F. Gont, "Recommendations for Transport-Protocol Port Randomization", BCP 156, RFC 6056, January 2011.
- [proto_numbers] IANA, "Protocol Numbers", 2011,
http://www.iana.org/assignments/protocol-numbers.
21.2. Informative References
- [IGDv1] UPnP Gateway Committee, "WANIPConnection:1", November 2001,
http://upnp.org/specs/gw/UPnP-gw-WANIPConnection-v1-Service.pdf. - [L2NAT] Miles, D. and M. Townsley, "Layer2-Aware NAT", Work in Progress, March 2009.
- [PCP-FAIL] Boucadair, M., Dupont, F., and R. Penno, "Port Control Protocol (PCP) Failure Scenarios", Work in Progress, August 2012.
- [PNP-IGD-PCP] Boucadair, M., Penno, R., and D. Wing, "Universal Plug and Play (UPnP) Internet Gateway Device (IGD)-Port Control Protocol (PCP) Interworking Function", Work in Progress, December 2012.
- [RFC0793] Postel, J., "Transmission Control Protocol", STD 7, RFC 793, September 1981.
- [RFC1918] Rekhter, Y., Moskowitz, R., Karrenberg, D., Groot, G., and E. Lear, "Address Allocation for Private Internets", BCP 5, RFC 1918, February 1996.
- [RFC2136] Vixie, P., Thomson, S., Rekhter, Y., and J. Bound, "Dynamic Updates in the Domain Name System (DNS UPDATE)", RFC 2136, April 1997.
- [RFC3007] Wellington, B., "Secure Domain Name System (DNS) Dynamic Update", RFC 3007, November 2000.
- [RFC3022] Srisuresh, P. and K. Egevang, "Traditional IP Network Address Translator (Traditional NAT)", RFC 3022, January 2001.
- [RFC3581] Rosenberg, J. and H. Schulzrinne, "An Extension to the Session Initiation Protocol (SIP) for Symmetric Response Routing", RFC 3581, August 2003.
- [RFC3587] Hinden, R., Deering, S., and E. Nordmark, "IPv6 Global Unicast Address Format", RFC 3587, August 2003.
- [RFC4303] Kent, S., "IP Encapsulating Security Payload (ESP)", RFC 4303, December 2005.
- [RFC4340] Kohler, E., Handley, M., and S. Floyd, "Datagram Congestion Control Protocol (DCCP)", RFC 4340, March 2006.
- [RFC4787] Audet, F. and C. Jennings, "Network Address Translation (NAT) Behavioral Requirements for Unicast UDP", BCP 127, RFC 4787, January 2007.
- [RFC4941] Narten, T., Draves, R., and S. Krishnan, "Privacy Extensions for Stateless Address Autoconfiguration in IPv6", RFC 4941, September 2007.
- [RFC4960] Stewart, R., "Stream Control Transmission Protocol", RFC 4960, September 2007.
- [RFC4961] Wing, D., "Symmetric RTP / RTP Control Protocol (RTCP)", BCP 131, RFC 4961, July 2007.
- [RFC5382] Guha, S., Biswas, K., Ford, B., Sivakumar, S., and P. Srisuresh, "NAT Behavioral Requirements for TCP", BCP 142, RFC 5382, October 2008.
- [RFC6092] Woodyatt, J., "Recommended Simple Security Capabilities in Customer Premises Equipment (CPE) for Providing Residential IPv6 Internet Service", RFC 6092, January 2011.
- [RFC6145] Li, X., Bao, C., and F. Baker, "IP/ICMP Translation Algorithm", RFC 6145, April 2011.
- [RFC6146] Bagnulo, M., Matthews, P., and I. van Beijnum, "Stateful NAT64: Network Address and Protocol Translation from IPv6 Clients to IPv4 Servers", RFC 6146, April 2011.
- [RFC6296] Wasserman, M. and F. Baker, "IPv6-to-IPv6 Network Prefix Translation", RFC 6296, June 2011.
- [RFC6333] Durand, A., Droms, R., Woodyatt, J., and Y. Lee, "Dual-Stack Lite Broadband Deployments Following IPv4 Exhaustion", RFC 6333, August 2011.
- [RFC6619] Arkko, J., Eggert, L., and M. Townsley, "Scalable Operation of Address Translators with Per-Interface Bindings", RFC 6619, June 2012.
- [RFC6763] Cheshire, S. and M. Krochmal, "DNS-Based Service Discovery", RFC 6763, February 2013.
- [RFC6886] Cheshire, S. and M. Krochmal, "NAT Port Mapping Protocol (NAT-PMP)", RFC 6886, April 2013.
- [RFC6888] Perreault, S., Ed., Yamagata, I., Miyakawa, S., Nakagawa, A., and H. Ashida, "Common Requirements for Carrier-Grade NATs (CGNs)", BCP 127, RFC 6888, April 2013.
- [SCTPNAT] Stewart, R., Tuexen, M., and I. Ruengeler, "Stream Control Transmission Protocol (SCTP) Network Address Translation", Work in Progress, February 2013.
Appendix A. NAT-PMP Transition โจ
Okay bestie, this one's about moving from the old NAT-PMP world to PCP ๐ฆ. PCP is the successor to the NAT Port Mapping Protocol, NAT-PMP [RFC6886], and it's giving very familiar vibes because it shares similar semantics, concepts, and packet formats ๐. So NAT-PMP and PCP both use the same port, and they both use the version negotiation capabilities of NAT-PMP and PCP to figure out which version to use ๐. This section explains how an orderly transition from NAT-PMP to PCP may be achieved ๐ธ.
A client that supports both NAT-PMP and PCP SHOULD send its request using the PCP packet format ๐
. That request gets received by either a NAT-PMP server or a PCP server ๐. If a NAT-PMP server gets it, the response is UNSUPP_VERSION, as indicated by the NAT-PMP specification [RFC6886], and that makes the client downgrade to NAT-PMP and resend its request in NAT-PMP format ๐. If a PCP server gets it, the response is as described by this document, and processing just continues as expected โจ.
A PCP server that supports both NAT-PMP and PCP can handle requests in either format ๐ง. You can tell which one it is by the first octet of the packet: if it's zero, it's NAT-PMP, and if it's non-zero, it's PCP ๐.
A PCP-only gateway that receives a NAT-PMP request (the one where the first octet is zero) will interpret it as a version mismatch ๐. Normal PCP processing then emits a PCP response that's compatible with NAT-PMP, and the PCP server doesn't need any special handling for that ๐ซถ. Honestly iconic โจ.
Authors' Addresses ๐
Dan Wing (editor)
Cisco Systems, Inc.
170 West Tasman Drive
San Jose, California 95134
USA
EMail: dwing@cisco.com
Stuart Cheshire
Apple Inc.
1 Infinite Loop
Cupertino, California 95014
USA
Phone: +1 408 974 3207
EMail: cheshire@apple.com
Mohamed Boucadair
France Telecom
Rennes 35000
France
EMail: mohamed.boucadair@orange.com
Reinaldo Penno
Cisco Systems, Inc.
170 West Tasman Drive
San Jose, California 95134
USA
EMail: repenno@cisco.com
Paul Selkirk
Internet Systems Consortium
950 Charter Street
Redwood City, California 94063
USA
EMail: pselkirk@isc.org
๐ฆ where this page comes from
- ietf: rfc6887 2013-04
- ๐ girlified by claude-haiku-5-5 on : a language model's rewrite of the original, under its terms