家用路由器及NAT协议

2021-11-22 09:47:20 浏览数 (1)

“家用路由器”和书上讲的“路由器”完全不是一个概念!如果仅仅从功能上来说,路由器 = 交换机 NAT。以下“家用路由器”专指家用路由器,而”路由器“专指书上讲的路由器。

路由器是工作在网络层的设备,交换机是工作在数据链路层的设备,因此路由器比交换机更高一层,正是因为高了一层,所以才能实现NAT这种功能。

一台家用路由器由一个CPU担任网关的角色,一个端口创建一个局域网地址池,连接到交换机芯片上分给四个局域网端口,另一端口接广域网口,与局端设备保持连接,获得一个公网IP。至于无线芯片,其实是连在交换机芯片上的。所以家用无线路由器严格来说产品名称应该是家庭网关。

参考博客1中给出如下描述:

Home routers are actually a combination of three networking components: a router, a firewall, and a switch. In a commercial setting the three pieces of hardware are kept separate but consumer routers are almost always a combination of both the routing and switching components with a firewall added in for good measure.

也就是说,家用路由器 = 路由器 防火墙 交换机  = 防火墙 交换机 NAT。其中关于防火墙和交换机,参考博客1中分别给出描述:

Firewall: Routers act as basic firewalls in a variety of ways including automatically rejecting incoming data that is not part of an ongoing exchange between a computer within your network and the outside world. If you request a music stream from Pandora, for example, your router says, “We’re expecting you, come on in” and that stream of data is directed to the device that made the request. On the other hand, if a sudden burst of port probing comes in from an unknown address your router acts as a bouncer and rejects the requests, effectively cloaking your computers. Even for a user with a single computer a simple $50 router is worth it for the firewall functionality alone.

Switch: In addition to the inside-to-outside network functionality outlined above, home routers also act as a network switch. A network switch is a piece of hardware that facilitates communication between computers on an internal network. Without the switching function the devices could talk through the router to the greater internet but not to each other—something as simple as copying an MP3 from your laptop to your desktop over the network would be impossible.

路由器和交换机的区别(参考博客2):

两者都是连接互联网的设备,它们之间主要区别就是,交换机发生在网络的第二层数据链路层,而路由器发生在第三层网络层。这个区别是两者各自工作方式的根本区别。路由器可以根据IP地址寻找下一个设备,可以处理TCP/IP协议,而交换机是根据MAC地址寻址的。

最后来看下NAT协议。我们都知道,一台家用路由器可以允许多台设备同时连接上网,如下图所示:

那么当设备通过家用路由器向网络发送请求后,返回的响应到达路由器时,路由器必须要知道该响应对应的是哪台设备发送的请求。我们向网络供应商(ISP)申请网络访问权限时,ISP会给路由器分配一个公网ip,路由器内部的设备只能使用内网ip。NAT的作用就是实现公网/内网ip以及端口的转换。为此,需要一张表,用于记录内外ip和端口的映射关系。

假设内网中有两台设备A和B,同时访问同一个外网ip的相同端口。那么在路由器处就会记录如下映射关系:

(remote ip_r : port_r)--(local ip_a : a_port)

(remote ip_r : port_r)--(local ip_b : b_port)

假设恰好a_port和b_port的值相同,那么来自远端的响应数据到达路由器时,路由器就无法确定该请求应该给A还是给B。对于这种情况,NAT采用一个三元组来进行区分:(remote ip_r : port_r)(nat port)(local ip_a : a_port),即通过增加nat port来进行区分。当A和B请求到来时,为两个请求生成两个尚未使用的端口:a_port和b_port,并记录如下关系:

(remote ip_r : port_r)(nat a_port)(local ip_a : same_port)

(remote ip_r : port_r)(nat b_port)(local ip_b : same_port)

即当A请求到达路由器时,路由器将请求的源ip换成ISP分配的公网ip,并将源端口换成a_port;而当请求B到达路由器时,路由器将请求的源ip换成ISP分配的公网ip,并将源端口换成b_port。当A和B的请求返回时,根据返回的目的端口(返回的目的端口就是请求的源端口)是a_port还是b_port即可确定该将响应给A还是B。

参考博客:

1、https://www.howtogeek.com/99001/htg-explains-routers-and-switches/ Understanding Routers,Switches,and Network Hardware

2、https://baijiahao.baidu.com/s?id=1596373286946216952&wfr=spider&for=pc 路由器和交换机的区别?路由器的作用和工作原理

0 人点赞