AQA 7517, section 4.9.4.9
The problem
With NAT (4.9.4.8), devices on a private network are hidden behind the router's one public address. This is good for security and for saving addresses, but it causes a problem when someone outside the network needs to start a connection to a device inside.
An incoming request arrives at the router's public address, and the router has no entry in its NAT table, because nobody inside asked for it. It doesn't know which internal device should receive it, so it drops it.
The basic concept
Port forwarding is a rule set in the router that says: send any incoming traffic on a particular port to a particular device and port on the internal network.
The rule has these parts:
| Setting | Example |
|---|---|
| External port (the one used from outside) | 8080 |
| Internal IP address (the device to receive it) | 192.168.1.50 |
| Internal port (the service on that device) | 80 |
| Protocol | TCP |
So when a packet arrives at 86.12.34.56:8080, the router translates the destination to 192.168.1.50:80 and forwards it. Replies are translated back in the usual way.
The internal device needs a fixed address
The rule points at one private IP address. If the server got its address from DHCP (4.9.4.7) and the address changed, the router would forward the traffic to the wrong place, or nowhere, and port forwarding would stop working. So a server behind port forwarding is given a static IP address, or a DHCP reservation that always gives it the same address.
Why is it used?
Port forwarding is needed whenever a device inside a private network must be reachable from the Internet. Examples:
- Hosting a web server (or a website) from home or a small office.
- Running a game server so friends can join over the Internet.
- Reaching security cameras (CCTV) or a smart home hub while away.
- Remote desktop or SSH access to a computer at home or work.
- Connecting to a file server or media server from outside.
- Some peer-to-peer applications.
It also allows several servers to share one public address. Port 8080 can be forwarded to one machine and port 8081 to another, while the outside world sees only one IP address.
Port forwarding and NAT
| NAT | Port forwarding | |
|---|---|---|
| Direction | Handles outgoing connections started by internal devices | Handles incoming connections started from outside |
| Mappings are made | Automatically, as connections start | Manually, as a fixed rule set up by the administrator |
| Purpose | Share one public address, hide devices | Make a chosen service reachable from outside |
Security considerations
Port forwarding opens a hole in the protection that NAT gives, so it should be used with care.
- It lets outsiders connect to the internal device, so any weakness in that service could be attacked.
- Only forward the ports you need.
- Keep the software on that device up to date and use strong passwords and encryption.
- Combine it with a firewall.
Worked example
Task: a student runs a game server on a computer at 192.168.1.30, which listens on port 25565. Their router's public address is 86.12.34.56. What rule is needed so friends can connect, and what address do they use?
- Rule: forward external port 25565 (TCP) to internal address 192.168.1.30, port 25565.
- Friends connect to
86.12.34.56:25565. - When the router receives a packet for that port, it forwards it to the game server computer. Without the rule, the router would drop the packet, because it doesn't know which device inside should receive it.
Exam tip: outside users connect to the router's public IP address. The router recognises traffic arriving on a particular port (e.g. port 80 for a web server) and forwards it to the private IP address of the server. The server needs a static IP address, or the rule would stop working if DHCP changed it.
Check yourself
What is port forwarding?
A router rule that sends incoming traffic on a particular port to a specific device and port on the internal network.
Why is port forwarding needed when a network uses NAT?
NAT hides internal devices, and the router drops incoming connections that nobody inside started. Port forwarding tells the router which internal device should receive them.
Give two uses of port forwarding
Any two of: hosting a web server, running a game server, remote access to security cameras, remote desktop or SSH, accessing a home file or media server.
What is a security risk of port forwarding?
It makes the internal device reachable from the Internet, so any weakness in the forwarded service could be attacked.