r/AskComputerScience • • Jul 01 '19

Can someone explain Port Forwarding?

I've done some googling and port forwarding sounds a lot like stuff computers and routers already do, no? Mail, file transfer, and htt protocols access specific ports to do their jobs. I'm just confused on why you need to mess with this in your router/modem settings.

37 Upvotes

8 comments sorted by

4

u/TypicalCardiologist5 Jul 01 '19 edited Jul 01 '19

You only need to mess with port forwarding when you are hosting a service on a device behind your router. For example, if you are running a webserver on your computer with an internal IP address of 192.168.1.10, you need to forward ports 80 (http) and 443 (https) to 192.168.1.10 in order for the outside world to reach that computer. If you do not forward the ports, how is your router going to know which device on your network should get the traffic? It can't without port forwarding.

When you access services as a client (like visiting www.google.com) you don't need to do this because your router handles everything for you. It does so by opening a random local port that is mapped to your internal IP address. So for example when your iPad at 192.168.1.15 says "hey router, I want to visit Reddit.com," your router creates a random local port (for example, let's say port # 33500) mapped to the iPad's IP address. When the remote website sends traffic back to your router at port 33500, it says, "ohh, I opened that port for the device at 192.168.1.15, let me forward that to them." This process is called network address translation.

There are protocols such as UPnP that can automate the first process, but that's normally used for video games and stuff like that.

Also, if you a running a webserver with a public IP address, you would not need to use port forwarding because all traffic would be sent to that IP address. Port forwarding is only necessary when the device is on a private network.

2

u/onemanandhishat Jul 01 '19

Just to add on to the other answers, you are right that your router already does a similar thing for known traffic with a default port, like http or ftp. Port forwarding is typically done on other ports that are not pre-allocated for a specific service. Likewise, if you are accessing a service outside the network, that creates an entry in your router's forwarding table that replies from your target should be forwarded to your computer. The problem that needs port forwarding comes when you want to initiate the connection from outside.

The term for using local addresses inside the network is Network Address Translation (NAT). Simply, the IPv4 address format has too few permutations for the number of devices that are Internet enabled for them all to have a unique IP, so that's why devices within a network operate on a separate local addressing scheme. These days, ISPs also employ NAT at a wider level, your router may also be behind a layer of NAT. People who want to host home services like Plex or a web server now have to contact their ISP to arrange for a static IP address for their router in order to be able to access remotely.

1

u/expsychotic Jul 02 '19

I think it's worth mentioning that port forwarding is only necessary with ipv4. If you're using ipv6, then every device on your network can have a public ip address and port forwarding is unnecessary.

0

u/[deleted] Jul 01 '19

[deleted]

4

u/andrazte Jul 01 '19

Interesting!

2

u/mastermindxs Jul 02 '19

It's called NAT (network address translation) . It’s pretty neat!

1

u/badlawnchair Jul 03 '19 edited Jul 03 '19

They are a little different. NAT is translating from one IP to another IP, on an internal network, which is often part of the process. Just read the other examples as they're better.

3

u/OriginalName667 Jul 02 '19

The flip side of this is, what's the difference between outgoing client traffic, for which the modem seems to know where to send server responses, and incoming server traffic? The answer to this question helped me understand port forwarding a little better. The key is that. for outgoing client traffic, your computer initiated the conversation. When your packet is being sent off to the external server, the router does something clever by changing the port that your computer used to a different port that it then remembers corresponds to a connection specifically to your computer. (As an aside, this deals with issues where multiple computers are trying to use the same ports.) Why doesn't this work for a server, though? Well, outside clients are initiating the communication, so the router doesn't have this remembered entry in its tables, so it doesn't know where to send the traffic. Port forwarding solves this problem by always remembering that incoming traffic destined to certain port goes to a certain machine.

2

u/KuntaStillSingle Jul 02 '19

Does this represent any additional security risk over a non port forwarded connection?