Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Neutron 理解(11):用 NAT 把 Linux Network Namespace 連上外網

Linux network namespace 要連外網,大類上只有兩條路:網絡地址轉換(NAT)與橋接(bridging),而橋接又可按使用的網橋分為 linux bridge 與 Open vSwitch。本文聚焦 NAT 的具體配置與背後原理。

1. 環境與配置

可以把一個 network namespace 直接看成另一台計算機,理解起來更直觀:

Neutron 理解(11):用 NAT 把 Linux Network Namespace 連上外網

節點 host1 的 IP 為 192.168.1.32,實驗對端 host2 為 192.168.1.15。要讓 host1 上的 netns myspace 能 ping 通 host2,依次執行:

1  ip netns add myspace
   創建名為 myspace 的 network namespace
2  ip link add veth1 type veth peer name veth2
   創建一對 veth,兩端分別為 veth1 與 veth2
3  ip link set veth2 netns myspace
   把 veth2 加入 myspace 作為其網卡
4  ifconfig veth1 192.168.45.2 netmask 255.255.255.0 up
   配置 veth1 的 IP
5  ip netns exec myspace ifconfig veth2 192.168.45.3 netmask 255.255.255.0 up
   配置 veth2 的 IP,需與 veth1 同網段
6  ip netns exec myspace route add default gw 192.168.45.2
   把 myspace 的默認路由指向 veth1
7  echo 1 > /proc/sys/net/ipv4/ip_forward
   開啟內核 ip forwarding
8  iptables -t nat -A POSTROUTING -s 192.168.45.0/24 -o eth0 -j MASQUERADE
   配置 SNAT,把 myspace 發出的包源 IP 換成 eth0 的地址
9  iptables -t filter -A FORWARD -i eth0 -o veth1 -j ACCEPT
   默認 FORWARD 為 DROP 時,顯式允許 veth1 到 eth0 的轉發

配置完成後,host 的路由表會自動多出一條:

root@compute2:/home/s1# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.45.0    0.0.0.0         255.255.255.0   U     0      0        0 veth1

myspace 內部則是:

root@compute2:/home/s1# ip netns exec myspace route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.45.2    0.0.0.0         UG    0      0        0 veth2
192.168.45.0    0.0.0.0         255.255.255.0   U     0      0        0 veth2

第一條是顯式創建的,第二條自動生成。此時從 myspace ping 外網已經通了。

2. 原理

2.1 第八條 SNAT 為何不可省

不加 SNAT 時,ICMP Request 能到達對方,但 echo reply 回不來,因為它的目的地址是一個內部地址:

root@compute1:/home/s1# tcpdump -eni bridge1 -p icmp -v
tcpdump: listening on bridge1, link-type EN10MB (Ethernet), capture size 65535 bytes
07:40:03.827852 08:00:27:4f:56:17 > 08:00:27:c7:cf:ca, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 63, id 54069, offset 0, flags [DF], proto ICMP (1), length 84) 192.168.45.3 > 192.168.1.15: ICMP echo request, id 26569, seq 1, length 64
07:40:04.829779 08:00:27:4f:56:17 > 08:00:27:c7:cf:ca, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 63, id 54194, offset 0, flags [DF], proto ICMP (1), length 84)

加上第八條之後 ping 成功。在 host2 的 eth0 上可以看到 echo request 的源 IP 已是 host1 的地址:

07:44:19.360519 08:00:27:c7:cf:ca > 08:00:27:4f:56:17, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 64, id 54265, offset 0, flags [none], proto ICMP (1), length 84) 192.168.1.15 > 192.168.1.32: ICMP echo reply, id 28534, seq 7, length 64
07:44:20.358360 08:00:27:4f:56:17 > 08:00:27:c7:cf:ca, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 63, id 21278, offset 0, flags [DF], proto ICMP (1), length 84) 192.168.1.32 > 192.168.1.15: ICMP echo request, id 28534, seq 8, length 64

在 host1 的 eth0 上,來回包用的都是 host1 與 host2 的地址:

root@compute2:/home/s1# tcpdump -envi eth0 -p icmp -v
06:31:27.285150 08:00:27:4f:56:17 > 08:00:27:c7:cf:ca, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 63, id 58781, offset 0, flags [DF], proto ICMP (1), length 84) 192.168.1.32 > 192.168.1.15: ICMP echo request, id 29610, seq 158, length 64
06:31:27.285777 08:00:27:c7:cf:ca > 08:00:27:4f:56:17, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 64, id 23662, offset 0, flags [none], proto ICMP (1), length 84) 192.168.1.15 > 192.168.1.32: ICMP echo reply, id 29610, seq 158, length 64

而在 host1 的 veth1 上,發出與收到的包用的都是內部網段地址:

root@compute2:/home/s1# tcpdump -envi veth1 -p icmp -v
06:33:13.355956 b2:52:7e:b6:e9:4e > ee:53:ae:dd:6f:7f, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 64, id 6989, offset 0, flags [DF], proto ICMP (1), length 84) 192.168.45.3 > 192.168.1.15: ICMP echo request, id 29610, seq 264, length 64
06:33:13.356391 ee:53:ae:dd:6f:7f > b2:52:7e:b6:e9:4e, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 63, id 36067, offset 0, flags [none], proto ICMP (1), length 84) 192.168.1.15 > 192.168.45.3: ICMP echo reply, id 29610, seq 264, length 64

奇怪的是,nat 表裡並沒有顯式配置 DNAT,veth1 與 eth0 之間卻自動完成了地址轉換。原因是 ICMP 使用了 Query ID,而 NAT 會依 ICMP Query ID 自動對 echo reply 做 DNAT。依據 RFC 5508 的 3.1 節,內部主機發出 ICMP Query 時,內核 NAT 模塊會為外部地址分配一個匹配的 query ID(例中的 29610);收到 echo reply 時,再依 Query ID 與 ICMP header checksum 把外部 IP 轉回內部 IP,然後才做 forwarding。也就是說,ICMP Query ID 扮演的角色類似 TCP/UDP 的端口號,差別只在 NAT 為 ICMP 自動做了 DNAT,而 TCP/UDP 需要顯式添加規則。

2.2 關於 IP forwarding

Neutron 理解(11):用 NAT 把 Linux Network Namespace 連上外網

內核從 veth1 收到 myspace 發來的 ICMP 包後:先執行 PREROUTING(本例不需要);接著做路由決策,發現目的 IP 不在本機,需要 FORWARD,而 Linux 默認關閉 forwarding,所以要用第七條命令開啟;由於管理員通常把 FORWARD 默認策略設為 DROP,還需要第九條顯式 ACCEPT;然後執行 ip forwarding 查路由表轉到 eth0;接著 POSTROUTING 執行第八條 SNAT,避免包有去無回;最後從 eth0 發出。

2.3 myspace 的默認路由

myspace 只有一根 veth 連到 veth1,所以默認路由必須指向 veth1 的地址。

2.4 反向訪問:DNAT

上面的配置只解決了「內到外」。若希望外部能訪問 myspace 裡的服務,需要在 host1 上加 DNAT 規則,例如把 8080 端口收到的 TCP 轉到內部 IP 的 80 端口,同時配置 FORWARD 允許 eth0 進、veth1 出。流程是:對方訪問 192.168.1.32:8080;內核從 eth0 收到包;PREROUTING 把目的改為 192.168.45.3:80;路由決策確認需 FORWARD 並檢查 forwarding 與防火牆規則;查路由表轉到 veth1;POSTROUTING 中第八條因源地址不在內部網段而不匹配;包從 veth1 進入 myspace;最終被 80 端口的應用接收。

把這套「veth + 默認路由 + SNAT + FORWARD」的組合吃透,Neutron 裡 L3 Agent 做的事本質上就是同一件事,只不過全部自動化了。

Copyright © Linux教程網 All Rights Reserved