Howwise RSS
VoIP & Phone Systems9 min read

SIP Ports for a VoIP Firewall: Minimal Rules by Topology

Short answer

SIP ports in a VoIP firewall depend on topology: a phone registering outbound to a hosted PBX needs no inbound rules, while a PBX behind a firewall needs UDP 5060 or TCP 5061 plus an RTP range, locked to carrier IPs.

Key takeaways
  • A phone behind NAT that registers outbound to a hosted PBX needs no port forwards, only outbound SIP and RTP with stateful return traffic.
  • A PBX taking trunk calls needs SIP (5060 or 5061) and its RTP range allowed only from the carrier's signalling and media IPs, with default deny.
  • Size the RTP range as concurrent calls x 4 ports x 1.5 headroom, and make rtp.conf and the firewall match exactly.
  • Disable SIP ALG, set local_net and the external addresses in the PJSIP transport, and send keepalives every 30 to 60 seconds to survive short UDP timeouts.
  • Verify with tcpdump and sngrep on the PBX and nmap -sU from outside, never by opening 5060 to the world 'temporarily'.

Which SIP ports does my VoIP firewall need open?

The right rule set depends on who initiates the traffic. A stateful firewall automatically allows replies to flows started from inside, so anything the device starts needs no inbound rule. Only traffic that starts outside (a carrier sending an INVITE to your PBX) needs one.

Decide which row below describes you, then build only that rule set.

TopologyInbound rulesOutbound rulesNotes
Desk phone behind NAT, registering to a hosted PBXNoneUDP 5060 (or TCP/TLS 5060/5061) to the provider, UDP RTP to the provider's media range, DNS, NTP, provisioning HTTPSRegistration opens the NAT pinhole. Keepalives keep it open. Disable SIP ALG.
PBX behind a firewall with a SIP trunkSIP port from carrier signalling IPs only; RTP range from carrier media IPs onlySame ports to the carrier, DNS, NTPIf the trunk is a registration trunk (PBX registers out), inbound rules are often unnecessary, but IP-authenticated trunks need them.
PBX with remote phonesSIP port and RTP range from the phones' known IPs or VPN subnetAs aboveUnknown roaming IPs are the risky case. Prefer a VPN, or TLS plus SRTP with fail2ban.
Phone and PBX on the same LANNone from the internetNone beyond the trunkNever forward anything to the PBX GUI.
Tip: Double NAT (ISP router in front of your firewall) is the most forgotten case. Any forward or pinhole must exist on both devices, or the ISP router must be in bridge mode.

What ports do SIP and RTP actually use?

SIP signalling uses UDP and TCP 5060 for plain SIP and TCP 5061 for SIP-TLS. In Asterisk with PJSIP, a typical FreePBX layout is UDP 5060 and TLS 5061. Legacy chan_sip on FreePBX commonly sits on 5160 so it can coexist with PJSIP. Never assume: read the real binds.

RTP media is separate from signalling and uses a range of UDP ports. Asterisk defaults to 10000-20000 (rtpstart and rtpend in rtp.conf). Carrier media IPs frequently differ from their signalling IPs, and hardware phones and other PBXs use their own ranges, so the remote side's range is whatever their vendor specifies.

asterisk -rx 'pjsip show transports'
ss -lunp | grep asterisk      # UDP listeners
ss -ltnp | grep asterisk      # TCP/TLS listeners
grep -E '^rtp(start|end)' /etc/asterisk/rtp*.conf
asterisk -rx 'rtp show settings'

How big should the RTP port range be?

Each audio stream uses an RTP port and the next one for RTCP, so 2 ports per stream. A bridged call has two legs (caller to PBX, PBX to callee), so about 4 ports per call. Multiply by 1.5 for headroom, because ports are not instantly reused and transfers, queues and recordings add legs.

Worked example: 50 concurrent calls x 4 x 1.5 = 300 ports, so 10000-10300. A 10-call office needs 60, so 10000-10100 is already generous. A smaller range shrinks the exposed surface and makes firewall rules readable.

Keep the range identical in the PBX and the firewall. If rtp.conf says 10000-20000 and the firewall allows 10000-10200, any call Asterisk assigns above 10200 has one-way or no audio, and it fails randomly, which is hard to spot.

; /etc/asterisk/rtp.conf (FreePBX: Settings > Asterisk SIP Settings > RTP Settings)
[general]
rtpstart=10000
rtpend=10300

How do I allow only the carrier IP through the firewall?

Take the carrier's published signalling and media IP lists and write default-deny rules. Keep signalling and media in separate sets, because they differ and change at different times. Subscribe to the carrier's change notices, since an unlisted new media IP shows up as sudden one-way audio on some calls.

This nftables example uses documentation addresses. Replace them with your carrier's lists and your admin subnet.

table inet filter {
  set carrier_sig {
    type ipv4_addr; flags interval
    elements = { 203.0.113.10, 203.0.113.11 }
  }
  set carrier_media {
    type ipv4_addr; flags interval
    elements = { 198.51.100.0/24 }
  }
  chain input {
    type filter hook input priority 0; policy drop;
    ct state established,related accept
    ct state invalid drop
    iif lo accept
    ip saddr 192.168.10.0/24 tcp dport { 22, 443 } accept   # admin LAN only
    ip saddr @carrier_sig   udp dport 5060 accept
    ip saddr @carrier_sig   tcp dport 5061 accept           # only if TLS is used
    ip saddr @carrier_media udp dport 10000-10300 accept
  }
}
Warning: Load rules from the console or with a rollback timer, not over the SSH session you depend on. A wrong default-deny policy locks you out and drops live calls.

How do I do the same in the FreePBX Firewall module?

In FreePBX the Firewall module (Connectivity > Firewall) groups sources into zones. Trusted zone members can reach all services, External is the default for unknown addresses, and Other is for special cases. Services define which ports each zone may reach.

For a carrier, adding its IPs to Trusted is the quickest route but also grants GUI and SSH access to them. Cleaner is to leave carrier IPs out of Trusted and, under Services, enable only the SIP and RTP services for the External zone restricted by the carrier addresses, or create a custom service for your reduced RTP range. Keep the admin GUI, SSH and database services Trusted-only.

  1. Open Connectivity > Firewall > Services and note which services are enabled for External. Disable web GUI, SSH and anything unused.
  2. Add the carrier signalling IPs and media ranges to a custom service (or limit the SIP and RTP services to them), rather than to Trusted.
  3. Confirm the RTP range under Settings > Asterisk SIP Settings matches what the firewall allows.
  4. Enable Intrusion Detection (fail2ban) in the same module.
  5. Test from outside, then from the carrier's side with a real call in both directions.

Why not just port forward 5060 and 10000-20000 to the PBX?

Scanners such as SIPVicious sweep the internet for anything answering on 5060, usually within hours of opening it. They probe extension numbers, then brute-force secrets. A cracked extension is used to place premium-rate or international calls, and the bill arrives later.

A forward from any source also exposes bugs in the SIP stack and any weakly secured trunks or guest settings. Opening 5060 'temporarily' is the classic way this starts.

Mitigations, in order of strength: source-IP allowlists, a VPN for remote phones, strong random secrets (16+ characters, never the extension number), no anonymous or guest inbound (allowguest=no in chan_sip, and no endpoint named anonymous in PJSIP), fail2ban or FreePBX intrusion detection, country-level blocks, and outbound international dialling restricted by route and PIN.

Warning: Never expose the web admin GUI (80, 443, 8088 for the Asterisk HTTP server), SSH 22 or AMI 5038 to the internet. Expose WebRTC/WSS on 8089, IAX2 on UDP 4569, or STUN on UDP 3478 only if you actually use them, and then only to known sources.

Why do registrations drop or audio go one way behind NAT?

UDP has no connection, so the NAT or firewall keeps a timer-based mapping. Linux conntrack defaults are nf_conntrack_udp_timeout=30 seconds for unreplied flows and nf_conntrack_udp_timeout_stream=120 seconds once traffic flows both ways. Cheap routers often use 30 seconds. When the mapping expires, the provider's next packet is dropped, so the phone looks unregistered or loses inbound calls.

The fix has two halves: send traffic more often than the timeout, and raise the timeout where you control it.

SIP ALG is the other big cause. It rewrites Via, Contact and SDP in transit and often corrupts them, giving failed registrations, 'registered but no audio' and odd 400 errors. Disable it on every router and firewall in the path. With SIP-TLS the signalling is encrypted, so an ALG cannot read it anyway and only breaks plain-SIP parts. RTP stays on the normal range unless SRTP is enabled, but is still encrypted when it is.

SettingTypical valuePurpose
Phone registration expiry60 to 300 sRefreshes the NAT mapping and the registration
Phone NAT keepalive / OPTIONS20 to 30 sKeeps the pinhole open between registrations
PJSIP qualify_frequency (AOR)30 to 60 sPBX sends OPTIONS to detect dead or stale contacts
nf_conntrack_udp_timeout30 s default, raise to 60Unreplied UDP flows
nf_conntrack_udp_timeout_stream120 s default, raise to 180+Established bidirectional UDP
; /etc/asterisk/pjsip.conf (FreePBX: custom file, or Settings > Asterisk SIP Settings)
[transport-udp]
type=transport
protocol=udp
bind=0.0.0.0:5060
local_net=192.168.10.0/24
external_signaling_address=pbx.example.com
external_media_address=203.0.113.50

[1001]
type=endpoint
direct_media=no
rtp_symmetric=yes
force_rport=yes
rewrite_contact=yes

[1001]
type=aor
qualify_frequency=60

How do I test the rules and find the missing one?

Test from two positions. On the PBX, watch what arrives. From a different network (not your LAN), probe what is reachable. Match the symptom to the likely missing rule below.

Common errors are UDP-only rules when the provider uses TCP or TLS, an RTP range mismatch, and a second NAT upstream.

SymptomLikely causeCheck
No registration, no SIP packets seen5060 blocked, wrong transport, or ALGtcpdump on the PBX, then disable ALG
Registers but no audio at allRTP blocked or wrong external_media_addressrtp set debug on, look for packets both ways
Audio one wayNAT or rtp_symmetric issue, or one media IP missingCompare SDP c= line with source IP in sngrep
Call drops at about 30 secondsACK never received, Contact or ALG problemsngrep, look for repeated 200 OK; see our 30-second drop article
Inbound works, then stops after minutesUDP mapping timeoutShorten keepalive, raise UDP timeout
Failed auth floods in the logExposed 5060 under scanRestrict source IPs, enable fail2ban
  1. Capture signalling on the PBX: tcpdump -ni any port 5060 -w /tmp/sip.pcap
  2. View the call flow: sngrep, then open the call and check each INVITE, 200 OK and ACK.
  3. Turn on logging: asterisk -rx 'pjsip set logger on' and 'rtp set debug on'.
  4. From an outside host, run nmap -sU -p 5060 pbx.example.com. UDP results of open|filtered are normal. A host that is not in your allowlist should show no response.
  5. Place real test calls in both directions and confirm audio in both directions for at least two minutes, longer than the UDP timeout.

Common questions

What ports need to be open for VoIP?

For a hosted phone, none inbound: only outbound SIP (UDP 5060 or TCP 5061) and the provider's RTP range with return traffic allowed. For a PBX taking trunk calls, allow SIP and your RTP range inbound from the carrier IPs only.

Do I need to open both 5060 and 5061?

Only open what you use. 5060 carries plain SIP over UDP or TCP, and 5061 carries SIP over TLS on TCP. If your trunk uses TLS only, leave 5060 closed to that source.

Does RTCP need its own firewall rule?

No separate rule is needed if your rule covers the full RTP range, because RTCP uses the adjacent port. Allow the whole range you set in rtp.conf, and size it at about 4 ports per concurrent call.

Should I use STUN or just forward ports?

STUN on UDP 3478 helps a phone discover its public address, but it does not fix double NAT or symmetric NAT. For a PBX, set external_signaling_address and external_media_address explicitly rather than relying on STUN.

How do I know whether SIP ALG is causing my problem?

Capture on the PBX and compare the Contact and SDP addresses with what the phone sent. If they were rewritten, or if the problem disappears after turning ALG off and rebooting the router, it was ALG. Changing the SIP port to something other than 5060 also often sidesteps it.

Related topics:sipfirewallrtpasterisknat