SIP Phones Lose Registration Behind NAT: Causes and Fixes
SIP phones lose registration behind NAT because the router closes the UDP mapping (often 30-120 s) before the phone re-registers (often 3600 s). Send keepalives every 15-30 s so the PBX can still reach the phone.
- The NAT UDP mapping usually expires long before the REGISTER expiry, so inbound INVITEs hit a closed pinhole while outbound calls still work.
- Only traffic that crosses the NAT regularly keeps it open: set qualify_frequency to 20-30 s on the AOR and enable a 15-30 s keepalive on the phone.
- rewrite_contact, force_rport and rtp_symmetric fix address mismatches, but they never keep the pinhole open.
- Set the keepalive to about half of the shortest NAT timeout, which you can measure by raising the OPTIONS interval until inbound calls fail.
- Disable SIP ALG, avoid double NAT and make sure the phone keeps a stable source port, or switch to TCP/TLS with keepalives.
Why do SIP phones lose registration behind NAT?
A phone behind NAT sends its REGISTER from a private address. The router creates a UDP mapping such as 192.168.1.50:5060 to 203.0.113.10:23456 and forwards replies back through it. The PBX can only reach the phone through that mapping.
UDP has no connection state, so routers expire idle mappings with a timer. Consumer routers and carrier-grade NAT often use 30 to 120 seconds for UDP, while a typical REGISTER expiry is 3600 s. Once the mapping dies, an INVITE from the PBX to the old IP:port is dropped or sent to nobody.
The phone still believes it is registered, and Asterisk still holds the contact until its own expiry. Outbound calls work because the phone's first outbound packet builds a new mapping. That is why the problem looks random: inbound fails until the next outbound packet or re-registration.
| What you see | Likely cause | Confirm with |
|---|---|---|
| Inbound fails after a few idle minutes, outbound works, fixed by re-register | NAT UDP timeout shorter than keepalive | Gap between last packet and failed INVITE in tcpdump |
| Contact shows a private IP or wrong port | Missing rewrite_contact or rport | pjsip show contacts |
| Registration fails or expires immediately with 423 | Expiry below PBX minimum | pjsip set logger on, look for 423 Interval Too Brief |
| Phone changes source port on every REGISTER | Phone opens a new socket each time, so old mapping is orphaned | sngrep source ports |
| One-way audio only | RTP address mismatch, not pinhole | rtp set debug on |
| Odd Via or Contact rewriting, random failures | SIP ALG on the router | Capture on both sides of the router and compare |
How do I confirm the pinhole is closing?
Work through the checks in order. Each one rules out a different cause, so you do not change timers that were never the problem.
- Run pjsip show contacts and note the contact status (Avail, Unavail, NonQual) and the URI. The URI should hold the public IP and the source port the phone really uses.
- Run pjsip set logger on, wait for a REGISTER and compare its source address and port with the Contact header. If they differ, you need rewrite_contact. This is an address problem, not a timeout problem.
- Capture traffic with tcpdump and place a call to the phone after it has been idle for 5 minutes. If the INVITE goes out and no response arrives, retransmits at 0.5, 1, 2, 4 s, and finally a 408 or timeout appears, the mapping is closed.
- Check the source port across several REGISTERs. If it changes each time, the phone is the problem and keepalives alone will not help.
- Check whether the PBX gets any response to OPTIONS. If qualify is on and the contact flips to Unavail while the phone is still online, the NAT is dropping the OPTIONS.
- Capture on the phone LAN side and the WAN side at the same time. If SIP headers differ in ways you did not configure, SIP ALG is rewriting them.
# Asterisk CLI
pjsip show contacts
pjsip show endpoint 101
pjsip set logger on
rtp set debug on
# chan_sip
sip show peers
sip show peer 101
# shell on the PBX
tcpdump -ni any port 5060 -w /tmp/nat.pcap
sngrep -d any port 5060How do I measure the real NAT timeout and pick a keepalive?
The NAT timeout is a property of the router or carrier, so measure it instead of guessing. Set qualify_frequency on one test AOR to 30 s, then increase it step by step.
Start at 30 s and test a call after a few minutes idle. Then raise the interval to 45, 60, 90 and 120 s, repeating the test at each step. The first interval at which inbound calls fail brackets the timeout.
Set the keepalive to about half of the shortest timeout you found. That leaves room for one lost packet. If the timeout is 60 s, use 25-30 s.
| Where | Setting | Recommended value | Why |
|---|---|---|---|
| Phone | Registration expiry | 60-120 s on bad NAT, 300-600 s otherwise | Re-registration also refreshes the mapping |
| Phone | NAT keepalive (CRLF or OPTIONS) | 15-30 s | Holds the mapping open |
| Phone | Use rport / NAT traversal | On | Makes the PBX reply to the source port |
| PBX | qualify_frequency (AOR) | 20-30 s | OPTIONS refreshes the mapping from the PBX side |
| PBX | qualify_timeout | 3-4 s | Marks the contact Unavail after the wait |
| PBX | minimum_expiration | 60 | Avoids 423 for short phone expiries |
| PBX | default_expiration / maximum_expiration | 120 / 3600 | Caps what long-expiry phones can ask for |
| Router | SIP ALG | Off | It rewrites SIP and breaks mappings |
| Router | UDP timeout | 120 s or more where configurable | Gives keepalives more room |
PJSIP settings that keep registrations reachable
The AOR carries qualify and expiry options. The endpoint carries the NAT address handling. Both are needed, but only the AOR settings keep the pinhole open.
qualify_frequency makes Asterisk send OPTIONS to each contact at that interval. The phone answers with 200 OK, which travels through the mapping both ways and refreshes it. max_contacts limits simultaneous registrations, and remove_existing=yes replaces the oldest contact when a phone registers from a new port, so stale contacts do not pile up.
; pjsip.conf (or pjsip_custom.conf in FreePBX)
[101]
type=endpoint
transport=transport-udp
context=from-internal
disallow=all
allow=ulaw,alaw
rewrite_contact=yes
force_rport=yes
rtp_symmetric=yes
direct_media=no
aors=101
auth=101-auth
[101]
type=aor
max_contacts=1
remove_existing=yes
qualify_frequency=25
qualify_timeout=3.0
minimum_expiration=60
default_expiration=120
maximum_expiration=3600
[transport-udp]
type=transport
protocol=udp
bind=0.0.0.0:5060
external_signaling_address=203.0.113.5
external_media_address=203.0.113.5
local_net=192.168.0.0/16
local_net=10.0.0.0/8What do rewrite_contact, force_rport and rtp_symmetric actually fix?
rewrite_contact=yes tells Asterisk to ignore the private IP:port in the Contact header and use the packet's source IP:port instead. Without it, the PBX tries to send INVITEs to 192.168.1.50 and never reaches the phone. force_rport=yes sends responses to the source port even if the phone did not include rport.
rtp_symmetric=yes sends RTP to the address and port the PBX receives RTP from. direct_media=no keeps audio flowing through the PBX so two NATed endpoints are not asked to talk to each other directly.
All of these fix address mismatches. None of them creates traffic, so none of them holds the NAT mapping open. A phone with a perfect rewrite_contact setup still loses inbound calls after the mapping expires, because the rewritten contact points to a mapping that no longer exists.
| Setting | Fixes | Does not fix |
|---|---|---|
| rewrite_contact | Contact with private IP or wrong port | Idle timeout |
| force_rport | Responses sent to wrong port | Idle timeout |
| rtp_symmetric | One-way or no audio behind NAT | Missed INVITEs |
| direct_media=no | Direct RTP between NATed peers | Registration loss |
| qualify_frequency | Idle timeout, stale contacts | Wrong addresses |
chan_sip, FreePBX and router specifics
In chan_sip the equivalents are qualify=yes or a number of milliseconds, qualifyfreq=25, nat=force_rport,comedia, and defaultexpiry, minexpiry and maxexpiry. sip show peers shows UNREACHABLE or UNKNOWN when qualify fails, which is the same signal as Unavail in PJSIP.
In FreePBX, go to Settings > Asterisk SIP Settings > General SIP Settings. Set External Address and add every internal subnet under Local Networks. A wrong local network list makes Asterisk treat a remote phone as local and skip NAT handling. On the extension, open the Advanced tab and set Qualify Frequency, then enable Rewrite Contact, RTP Symmetric and Force rport.
On the router, turn off SIP ALG, avoid double NAT, and give the phone a fixed local SIP port so the mapping survives. If the router supports a UDP timeout, raise it. Where you cannot, TCP or TLS to the PBX (usually port 5061 for TLS) holds a single connection with TCP keepalives, though routers still expire idle TCP state.
; sip.conf peer (chan_sip)
[101]
type=friend
nat=force_rport,comedia
qualify=yes
qualifyfreq=25
defaultexpiry=120
minexpiry=60
maxexpiry=3600
directmedia=no What are the common mistakes?
Relying on rewrite_contact alone is the biggest one. It repairs the address but leaves the timer problem.
Setting qualify too high, for example 60 s or more, is the second. With a 30 s NAT timeout the mapping dies between probes.
A phone that asks for an expiry below the PBX minimum gets 423 Interval Too Brief and never registers. Lower minimum_expiration or raise the phone value.
Several phones behind one public IP using the same source port can clash on cheap routers. Give each phone a different local SIP port. A phone that picks a new source port on each REGISTER leaves the old mapping orphaned, so fix the phone's setting or use remove_existing=yes.
Common questions
What is a good SIP keepalive interval for IP phones?
Use 15-30 s as a starting point, and no more than half the shortest NAT UDP timeout. If you measure a 60 s timeout, 25-30 s is safe.
Why does the phone show registered while Asterisk shows Unavailable?
The phone only knows its last REGISTER got a 200 OK. Asterisk marks the contact Unavail when OPTIONS goes unanswered, which usually means the NAT mapping closed or the contact points to a stale port.
Can I fix this just by shortening the registration expiry?
Partly. A 60-120 s expiry refreshes the mapping at each REGISTER, but it adds load and still leaves gaps if the timeout is shorter. Keepalives are cheaper than frequent registrations.
Does SIP over TCP or TLS avoid the problem?
It helps because Asterisk reuses the phone's connection for inbound requests. NAT devices still expire idle TCP state, so you need keepalives, and a dropped connection means a new registration.
Why does the contact keep a different port after each re-registration?
The phone is probably opening a new socket, or the router is assigning a new external port. Use remove_existing=yes to clear stale contacts and set a fixed local SIP port on the phone.