net/portmapper: add Probe metrics for all PCP result codes

Probe only counted the OK, not authorized and address mismatch PCP
result codes. All other codes went to
portmap_pcp_unhandled_response_code.

This adds all RFC 6887 result codes. IANA has allocated more for
RFCs 7652 and 7843, but we are leaving those out since we don't
use authentication or third-party options.

PCP replies with an opcode other than ANNOUNCE now go to a new
portmap_pcp_unhandled_opcode metric instead of
portmap_pcp_unhandled_response_code, same as NAT-PMP.

Updates tailscale/corp#51202

Change-Id: I00bc0309914280c4238cb2aaa322a595fe94d699
Signed-off-by: Francois Marier <[email protected]>
This commit is contained in:
Francois Marier authored and François Marier committed 2026-11-25 10:45:03 -07:00
1 parent 1a3a1da16f
commit 649ca43716
3 files changed
+110 -20

No files matched your search

+14 -3
View File
@@ -29,14 +29,25 @@ const (
// the IP assignment is valid for.
pcpMapLifetimeSec = 7200
pcpCodeOK pcpResultCode = 0
pcpCodeNotAuthorized pcpResultCode = 2
pcpCodeOK pcpResultCode = 0
pcpCodeUnsupportedVersion pcpResultCode = 1
pcpCodeNotAuthorized pcpResultCode = 2
pcpCodeMalformedRequest pcpResultCode = 3
pcpCodeUnsupportedOpcode pcpResultCode = 4
pcpCodeUnsupportedOption pcpResultCode = 5
pcpCodeMalformedOption pcpResultCode = 6
pcpCodeNetworkFailure pcpResultCode = 7
pcpCodeNoResources pcpResultCode = 8
pcpCodeUnsupportedProtocol pcpResultCode = 9
pcpCodeUserExceededQuota pcpResultCode = 10
pcpCodeCannotProvideExternal pcpResultCode = 11
// From RFC 6887:
// ADDRESS_MISMATCH: The source IP address of the request packet does
// not match the contents of the PCP Client's IP Address field, due
// to an unexpected NAT on the path between the PCP client and the
// PCP-controlled NAT or firewall.
pcpCodeAddressMismatch pcpResultCode = 12
pcpCodeAddressMismatch pcpResultCode = 12
pcpCodeExcessiveRemotePeers pcpResultCode = 13
pcpOpReply = 0x80 // OR'd into request's op code on response
pcpOpAnnounce = 0
+17 -13
View File
@@ -12,25 +12,29 @@ func _() {
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[pcpCodeOK-0]
_ = x[pcpCodeUnsupportedVersion-1]
_ = x[pcpCodeNotAuthorized-2]
_ = x[pcpCodeMalformedRequest-3]
_ = x[pcpCodeUnsupportedOpcode-4]
_ = x[pcpCodeUnsupportedOption-5]
_ = x[pcpCodeMalformedOption-6]
_ = x[pcpCodeNetworkFailure-7]
_ = x[pcpCodeNoResources-8]
_ = x[pcpCodeUnsupportedProtocol-9]
_ = x[pcpCodeUserExceededQuota-10]
_ = x[pcpCodeCannotProvideExternal-11]
_ = x[pcpCodeAddressMismatch-12]
_ = x[pcpCodeExcessiveRemotePeers-13]
}
const (
_pcpResultCode_name_0 = "OK"
_pcpResultCode_name_1 = "NotAuthorized"
_pcpResultCode_name_2 = "AddressMismatch"
)
const _pcpResultCode_name = "OKUnsupportedVersionNotAuthorizedMalformedRequestUnsupportedOpcodeUnsupportedOptionMalformedOptionNetworkFailureNoResourcesUnsupportedProtocolUserExceededQuotaCannotProvideExternalAddressMismatchExcessiveRemotePeers"
var _pcpResultCode_index = [...]uint8{0, 2, 20, 33, 49, 66, 83, 98, 112, 123, 142, 159, 180, 195, 215}
func (i pcpResultCode) String() string {
switch {
case i == 0:
return _pcpResultCode_name_0
case i == 2:
return _pcpResultCode_name_1
case i == 12:
return _pcpResultCode_name_2
default:
idx := int(i) - 0
if i < 0 || idx >= len(_pcpResultCode_index)-1 {
return "pcpResultCode(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _pcpResultCode_name[_pcpResultCode_index[idx]:_pcpResultCode_index[idx+1]]
}
+79 -4
View File
@@ -1138,11 +1138,38 @@ func (c *Client) Probe(ctx context.Context) (res portmappertype.ProbeResult, err
res.PCP = false
metricPCPAddressMismatch.Add(1)
continue
case pcpCodeUnsupportedVersion:
// TODO(fmarier): this is a PCP-only server rejecting our NAT-PMP
// request, so we should remember that NAT-PMP is not supported and
// continue (#21799).
metricPCPUnsupportedVersion.Add(1)
case pcpCodeMalformedRequest:
metricPCPMalformedRequest.Add(1)
case pcpCodeUnsupportedOpcode:
metricPCPUnsupportedOpcode.Add(1)
case pcpCodeUnsupportedOption:
metricPCPUnsupportedOption.Add(1)
case pcpCodeMalformedOption:
metricPCPMalformedOption.Add(1)
case pcpCodeNetworkFailure:
metricPCPNetworkFailure.Add(1)
case pcpCodeNoResources:
metricPCPNoResources.Add(1)
case pcpCodeUnsupportedProtocol:
metricPCPUnsupportedProtocol.Add(1)
case pcpCodeUserExceededQuota:
metricPCPUserExceededQuota.Add(1)
case pcpCodeCannotProvideExternal:
metricPCPCannotProvideExternal.Add(1)
case pcpCodeExcessiveRemotePeers:
metricPCPExcessiveRemotePeers.Add(1)
default:
// Fall through to unexpected log line.
metricPCPUnhandledResponseCode.Add(1)
}
// Fall through to unexpected log line.
} else {
metricPCPUnhandledOpcode.Add(1)
}
metricPCPUnhandledResponseCode.Add(1)
c.logf("unexpected PCP probe response: %+v", pres)
}
if pres, ok := parsePMPResponse(buf[:n]); ok {
@@ -1256,7 +1283,7 @@ var uPnPIGDPacket = []byte("M-SEARCH * HTTP/1.1\r\n" +
"MAN: \"ssdp:discover\"\r\n" +
"MX: 2\r\n\r\n")
// PCP/PMP metrics
// PCP/PMP Probe metrics
var (
// metricPXPResponse counts the number of times we received a PMP/PCP response.
metricPXPResponse = clientmetric.NewCounter("portmap_pxp_response")
@@ -1280,6 +1307,54 @@ var (
// we received an (as yet) unhandled PCP result code.
metricPCPUnhandledResponseCode = clientmetric.NewCounter("portmap_pcp_unhandled_response_code")
// metricPCPUnhandledOpcode counts the number of times
// we received an unhandled PCP opcode.
metricPCPUnhandledOpcode = clientmetric.NewCounter("portmap_pcp_unhandled_opcode")
// metricPCPUnsupportedVersion counts the number of times
// we received a PCP unsupported version result code.
metricPCPUnsupportedVersion = clientmetric.NewCounter("portmap_pcp_unsupported_version")
// metricPCPMalformedRequest counts the number of times
// we received a PCP malformed request result code.
metricPCPMalformedRequest = clientmetric.NewCounter("portmap_pcp_malformed_request")
// metricPCPUnsupportedOpcode counts the number of times
// we received a PCP unsupported opcode result code.
metricPCPUnsupportedOpcode = clientmetric.NewCounter("portmap_pcp_unsupported_opcode")
// metricPCPUnsupportedOption counts the number of times
// we received a PCP unsupported option result code.
metricPCPUnsupportedOption = clientmetric.NewCounter("portmap_pcp_unsupported_option")
// metricPCPMalformedOption counts the number of times
// we received a PCP malformed option result code.
metricPCPMalformedOption = clientmetric.NewCounter("portmap_pcp_malformed_option")
// metricPCPNetworkFailure counts the number of times
// we received a PCP network failure result code.
metricPCPNetworkFailure = clientmetric.NewCounter("portmap_pcp_network_failure")
// metricPCPNoResources counts the number of times
// we received a PCP no resources result code.
metricPCPNoResources = clientmetric.NewCounter("portmap_pcp_no_resources")
// metricPCPUnsupportedProtocol counts the number of times
// we received a PCP unsupported protocol result code.
metricPCPUnsupportedProtocol = clientmetric.NewCounter("portmap_pcp_unsupported_protocol")
// metricPCPUserExceededQuota counts the number of times
// we received a PCP user exceeded quota result code.
metricPCPUserExceededQuota = clientmetric.NewCounter("portmap_pcp_user_exceeded_quota")
// metricPCPCannotProvideExternal counts the number of times
// we received a PCP cannot provide external result code.
metricPCPCannotProvideExternal = clientmetric.NewCounter("portmap_pcp_cannot_provide_external")
// metricPCPExcessiveRemotePeers counts the number of times
// we received a PCP excessive remote peers result code.
metricPCPExcessiveRemotePeers = clientmetric.NewCounter("portmap_pcp_excessive_remote_peers")
// metricPMPSent counts the number of times we sent a PMP request.
metricPMPSent = clientmetric.NewCounter("portmap_pmp_sent")
@@ -1308,7 +1383,7 @@ var (
metricPMPNotAuthorized = clientmetric.NewCounter("portmap_pmp_not_authorized")
)
// UPnP metrics
// UPnP Probe metrics
var (
// metricUPnPSent counts the number of times we sent a UPnP request.
metricUPnPSent = clientmetric.NewCounter("portmap_upnp_sent")