如何从虚拟 tun0 设备调试路由问题?

Dav*_*her 2 linux routing tun packet-crafting

我有一个程序(由我编写),它创建一个 tun0 设备并设置一个路由,以便可以从此设备读取发往 172.16.1.0/24 子网的数据包。我现在正试图向另一个方向前进,并将数据包写入 tun 设备,该设备可以通过

我的第一个努力,只是更改源地址和目标地址以及端口工作正常。我可以运行以下命令:

nc -u -s MY_IP -p 4001 172.16.1.3 4000
Run Code Online (Sandbox Code Playgroud)

我的输入得到了回应。

我的第二个努力,实际上是从头开始生成输出数据包,目前正在失败。

我可以运行tcpdump -i tun0并查看我编写的数据包:

11:30:14.433489 IP (tos 0x0, ttl 32, id 0, offset 0, flags [none], proto UDP (17), length 56) 172.16.1.2.54167 > Ubuntu-dbacher.local.4011: [udp sum ok] UDP, length 28
Run Code Online (Sandbox Code Playgroud)

但是我的听众 ( nc -l -u -s MY_IP -p 4011) 什么也没看到。

我怀疑有什么问题阻止了 tun0 设备将其数据包路由出去,但我不知道如何了解数据包被丢弃的位置。

$ ifconfig tun0
tun0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
          inet addr:172.16.1.1  P-t-P:172.16.1.1  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:500
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.16.1.0      172.16.1.1      255.255.255.0   UG    0      0        0 tun0
10.10.48.0      0.0.0.0         255.255.255.0   U     1      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth0
0.0.0.0         10.10.48.1      0.0.0.0         UG    0      0        0 eth0

$ cat /proc/sys/net/ipv4/ip_forward
1
Run Code Online (Sandbox Code Playgroud)

如何调试 tun 数据包被丢弃的位置?

(顺便说一句,所有数据包都是 UDP。)

pQd*_*pQd 6

只是一个预感 - linux 允许数据包路由吗?

运行cat /proc/sys/net/ipv4/ip_forward - 你希望看到 1. 如果它是 0 运行:

echo 1 > /proc/sys/net/ipv4/ip_forward
Run Code Online (Sandbox Code Playgroud)

还要检查您的 iptables - 一开始您可能希望接受 FORWARD 链:

iptables -P FORWARD ACCEPT
iptables -F FORWARD
Run Code Online (Sandbox Code Playgroud)

在对路由/防火墙进行故障排除时,我最常使用 tcpdump [您已经提到过]。