使用 Rasperry Pi 路由器进行流量重定向

使用 Rasperry Pi 路由器进行流量重定向

我使用 Rasperry Pi(运行 raspbian)作为 Wi-Fi 路由器。它将流量从以太网线(eth0 - 线 + ppp0 - L2TP 连接,用于 lx2tp)重定向到 wlan0(USB Wi-Fi 适配器,用于 AP 功能的 hostapd)。

问题是,有些网站加载速度非常慢,甚至根本无法加载。以 soundcloud.com 为例 - 该命令curl -L soundcloud.com在路由器上可以快速执行,但在通过 Wi-Fi 连接到路由器的笔记本电脑上却永远挂起。这一定是流量重定向问题。

这是我的 /etc/network/interfaces:

pi@pi ~ $ cat /etc/network/interfaces
auto lo

iface lo inet loopback
iface eth0 inet dhcp

iface wlan0 inet static
    address 192.168.1.1
    netmask 255.255.255.0

up iptables-restore < /etc/iptables.ipv4.nat

重定向规则如下:

pi@pi ~ $ cat 1.sh
sudo iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
sudo iptables -A FORWARD -i ppp0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o ppp0 -j ACCEPT
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

这个设置有问题吗?

更新:

Pi 上的路由表:

pi@pi ~ $ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         *               0.0.0.0         U     0      0        0 ppp0
10.89.64.0      *               255.255.248.0   U     0      0        0 eth0
85.21.0.0       10.89.64.1      255.255.255.0   UG    0      0        0 eth0
hdns2.corbina.n 10.89.64.1      255.255.255.255 UGH   0      0        0 eth0
192.168.1.0     *               255.255.255.0   U     0      0        0 wlan0
hdns1.corbina.n 10.89.64.1      255.255.255.255 UGH   0      0        0 eth0

更新2:

事实证明这不是一些 SSL 问题(这是来自 OSX 主机上的 vagrant Ubuntu box):

vagrant@precise64:~$ curl -v -L soundcloud.com/pure_virtual
* Hostname was NOT found in DNS cache
*   Trying 93.184.220.127...
* Connected to soundcloud.com (93.184.220.127) port 80 (#0)
> GET /pure_virtual HTTP/1.1
> User-Agent: curl/7.38.0
> Host: soundcloud.com
> Accept: */*
> 
< HTTP/1.1 302 Found
< Date: Sat, 04 Apr 2015 17:38:06 GMT
< Location: https://soundcloud.com/pure_virtual
* Server am/2 is not blacklisted
< Server: am/2
< X-Frame-Options: SAMEORIGIN
< Content-Length: 0
< 
* Connection #0 to host soundcloud.com left intact
* Issue another request to this URL: 'https://soundcloud.com/pure_virtual'
* Found bundle for host soundcloud.com: 0x7f73d4f2d0b0
* Hostname was NOT found in DNS cache
*   Trying 93.184.220.127...
* Connected to soundcloud.com (93.184.220.127) port 443 (#1)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):

* Operation timed out after 0 milliseconds with 0 out of 0 bytes received
* Closing connection 1
curl: (28) Operation timed out after 0 milliseconds with 0 out of 0 bytes received

答案1

您的设置似乎非常标准。这意味着问题可能出在很多方面。

找到问题的最直接方法是查看流量是如何流动的Wireshark- 一个允许您使用 GUI 查看数据包如何流动的程序。

首先,使用以下命令在 Raspberry Pi 上捕获一些数据:

sudo tcpdump -n -w part1.pcap not port 22

(解释:该-n标志停止 DNS 解析,因为我们不关心这一点。该-w part1.pcap部分将把数据写入 pcap 文件,以便我们稍后可以将其提供给 WireShark。它将not port 22过滤掉我们不关心的 SSH 流量。)

现在,运行该命令,打开与 Raspberry Pi 的第二个 SSH 连接并执行curl -L soundcloud.com(和其他相关命令)。完成后,返回第一个 SSH 窗口并执行“Ctrl + C”以停止 tcpdump。这将创建文件part1.pcap

对于第二轮,执行相同的操作,但使用不同的文件名,如下所示:

sudo tcpdump -n -w part2.pcap not port 22

现在,在电脑上手动访问 soundcloud.com。加载完成后,再次按“Ctrl + C”结束 tcpdump。

最后,在您的 PC 上下载并安装 Wireshark,传输 2 个 pcap 文件,然后使用 Wireshark 查看它们。通过比较这两个文件,您应该会找到有关正在发生的事情的有力线索。

答案2

这似乎是一个 MTU/数据包碎片问题,可以通过以下 iptables 命令解决:

iptables -I FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu

相关内容