From 5963e97c592e578542222afd8ca716c1655625de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Sat, 17 Oct 2026 21:02:25 +0500 Subject: [PATCH] Accept verified existing UPnP mappings for this server --- .../20261017-existing-router-mapping.md | 7 ++ crates/coa-core/src/upnp.rs | 78 ++++++++++++++++++- 2 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 changelog.d/20261017-existing-router-mapping.md diff --git a/changelog.d/20261017-existing-router-mapping.md b/changelog.d/20261017-existing-router-mapping.md new file mode 100644 index 0000000..4c5acc3 --- /dev/null +++ b/changelog.d/20261017-existing-router-mapping.md @@ -0,0 +1,7 @@ +--- +area: manager +type: fixed +audience: admins +title: Internet sharing accepts existing port forwards to the same server +--- +When a router rejects an already configured port forward, the Manager checks its destination. Matching enabled rules are accepted; conflicting rules report the affected TCP port and remain untouched. diff --git a/crates/coa-core/src/upnp.rs b/crates/coa-core/src/upnp.rs index 1cbc771..ae93aa2 100644 --- a/crates/coa-core/src/upnp.rs +++ b/crates/coa-core/src/upnp.rs @@ -118,8 +118,11 @@ pub fn external_ip(gw: &Gateway) -> Result { } pub fn add_mapping(gw: &Gateway, port: u16, lan: Ipv4Addr, what: &str) -> Result<()> { - soap( - gw, + add_mapping_with(port, lan, what, |action, args| soap(gw, action, args)) +} + +fn add_mapping_with(port: u16, lan: Ipv4Addr, what: &str, mut request: impl FnMut(&str, &[(&str, String)]) -> Result) -> Result<()> { + let result = request( "AddPortMapping", &[ ("NewRemoteHost", String::new()), @@ -131,8 +134,28 @@ pub fn add_mapping(gw: &Gateway, port: u16, lan: Ipv4Addr, what: &str) -> Result ("NewPortMappingDescription", format!("{DESCRIPTION_PREFIX} - {what}")), ("NewLeaseDuration", "0".into()), ], - ) - .map(|_| ()) + ); + match result { + Ok(_) => Ok(()), + Err(error) => { + // Some routers reject a mapping that already points to us. Read it back; + // never delete or replace another host's mapping. + let existing = request("GetSpecificPortMappingEntry", &[ + ("NewRemoteHost", String::new()), + ("NewExternalPort", port.to_string()), + ("NewProtocol", "TCP".into()), + ]); + if let Ok(xml) = existing { + let client = between(&xml, "", "").and_then(|v| v.parse::().ok()); + let internal_port = between(&xml, "", "").and_then(|v| v.parse::().ok()); + let enabled = between(&xml, "", ""); + if client == Some(lan) && internal_port == Some(port) && matches!(enabled, Some("1" | "true")) { + return Ok(()); + } + } + Err(Error::Invalid(format!("Could not forward TCP port {port}: {error}. Check the router's existing port forwarding rules for this port."))) + } + } } pub fn remove_mapping(gw: &Gateway, port: u16) -> Result<()> { @@ -148,6 +171,53 @@ mod tests { urn:schemas-upnp-org:service:WANIPConnection:1/ctl/IPConn\ "; + #[test] + fn existing_mapping_is_accepted_only_for_the_requested_destination() { + let lan = "192.168.1.5".parse().unwrap(); + for (client, port, enabled, accepted) in [ + ("192.168.1.5", "3724", "1", true), + ("192.168.1.5", "3724", "true", true), + ("192.168.1.6", "3724", "1", false), + ("192.168.1.5", "8085", "1", false), + ("192.168.1.5", "3724", "0", false), + ("", "", "", false), + ] { + let mut actions = Vec::new(); + let result = add_mapping_with(3724, lan, "Login", |action, args| { + actions.push(action.to_string()); + assert!(args.contains(&("NewExternalPort", "3724".into()))); + assert!(args.contains(&("NewProtocol", "TCP".into()))); + match action { + "AddPortMapping" => Err(Error::Invalid("ConflictInMappingEntry".into())), + "GetSpecificPortMappingEntry" => Ok(format!("{client}{port}{enabled}")), + _ => panic!("must not delete or modify an existing mapping"), + } + }); + assert_eq!(result.is_ok(), accepted); + assert_eq!(actions, ["AddPortMapping", "GetSpecificPortMappingEntry"]); + if let Err(error) = result { + assert!(error.to_string().contains("TCP port 3724")); + assert!(error.to_string().contains("ConflictInMappingEntry")); + } + } + } + + #[test] + fn successful_add_does_not_require_router_readback_support() { + add_mapping_with(8085, "192.168.1.5".parse().unwrap(), "World", |action, _| { + assert_eq!(action, "AddPortMapping"); + Ok(String::new()) + }).unwrap(); + } + + #[test] + fn failed_readback_keeps_the_original_error() { + let error = add_mapping_with(8085, "192.168.1.5".parse().unwrap(), "World", |action, _| { + Err(Error::Invalid(if action == "AddPortMapping" { "ConflictInMappingEntry" } else { "Unsupported action" }.into())) + }).unwrap_err().to_string(); + assert!(error.contains("ConflictInMappingEntry") && error.contains("8085")); + } + #[test] fn parses_ssdp_location_and_description() { let resp = "HTTP/1.1 200 OK\r\nCACHE-CONTROL: max-age=120\r\nLocation: http://192.168.1.1:5000/rootDesc.xml\r\nST: x\r\n\r\n";