使用 tun 设备发送原始 IP 数据包

Send raw IP packet with tun device

我正在尝试通过 TUN device 以编程方式构建和发送 IP 数据包。

我已经设置了 TUN 设备和正确的路由:

# ip tuntap add mode tun tun0
# ip link set tun0 up
# ip addr add 10.0.0.2/24 dev tun0

这导致:

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use 
Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    600    0        0 wlp3s0
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 tun0
192.168.0.0     0.0.0.0         255.255.255.0   U     600    0        0 wlp3s0

$ ifconfig tun0
tun0: flags=4241<UP,POINTOPOINT,NOARP,MULTICAST>  mtu 1500
    inet 10.0.0.2  netmask 255.255.255.0  destination 10.0.0.2
    inet6 fe80::f834:5267:3a1:5d1d  prefixlen 64  scopeid 0x20<link>
    unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 500  (UNSPEC)

IP 转发已开启:# echo 1 > /proc/sys/net/ipv4/ip_forward

我已经为 tun0 个数据包设置了 NAT:

# iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o wlp3s0 -j MASQUERADE
# iptables -t nat -L -v
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination                   
0     0 MASQUERADE  all  --  any    wlp3s0  10.0.0.0/24          anywhere

然后我有 python 脚本来生成 ICMP 数据包:

import os
from fcntl import ioctl
import struct
import time
import random

# pip install pypacker==4.0
from pypacker.layer3.ip import IP
from pypacker.layer3.icmp import ICMP

TUNSETIFF = 0x400454ca
IFF_TUN   = 0x0001
IFF_NO_PI = 0x1000

ftun = os.open("/dev/net/tun", os.O_RDWR)
ioctl(ftun, TUNSETIFF, struct.pack("16sH", b"tun0", IFF_TUN | IFF_NO_PI))

req_nr = 1
req_id = random.randint(1, 65000)
while True:
    icmp_req = IP(src_s="10.0.0.2", dst_s="8.8.8.8", p=1) +\
        ICMP(type=8) +\
        ICMP.Echo(id=req_id, seq=req_nr, body_bytes=b"povilas-test")
    os.write(ftun, icmp_req.bin())
    time.sleep(1)
    req_nr += 1

我可以看到来自 tun0 接口的数据包:

# tshark -i tun0
1 0.000000000     10.0.0.2 → 8.8.8.8      ICMP 48 Echo (ping) request  id=0xb673, seq=1/256, ttl=64
2 1.001695939     10.0.0.2 → 8.8.8.8      ICMP 48 Echo (ping) request  id=0xb673, seq=2/512, ttl=64
3 2.003375319     10.0.0.2 → 8.8.8.8      ICMP 48 Echo (ping) request  id=0xb673, seq=3/768, ttl=6

但是 wlp3s0 接口是静默的,因此数据包似乎没有经过 NAT 转换并路由到 wlp3s0 接口,这是我的 WLAN 卡。

知道我遗漏了什么吗?

我是 运行 Debian 9。 事实证明数据包转发被禁用 - FORWARD 链的默认策略是 DROP:

# iptables -L -v
Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

所以我改变了政策:

# iptables -P FORWARD ACCEPT
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

此外,我必须将 IP 数据包源地址更改为 10.0.0.2 以外的其他地址,这是 tun0 接口的首选源地址:

$ ip route
10.0.0.0/24 dev tun0 proto kernel scope link src 10.0.0.2

所以我把包源地址改成了10.0.0.4:

icmp_req = IP(src_s="10.0.0.4", dst_s="8.8.8.8", p=1) +\
    ICMP(type=8) +\
    ICMP.Echo(id=req_id, seq=req_nr, body_bytes=b"povilas-test")

然后内核开始转发来自tun0接口的数据包到网关接口:

# tshark -i wlp3s0

5 0.008428567 192.168.0.103 → 8.8.8.8      ICMP 62 Echo (ping) request  id=0xb5c7, seq=9/2304, ttl=63
6 0.041114028      8.8.8.8 → 192.168.0.103 ICMP 62 Echo (ping) reply    id=0xb5c7, seq=9/2304, ttl=48 (request in 5)

ping 响应也被发送回 tun0:

# tshark -i tun0

1 0.000000000     10.0.0.4 → 8.8.8.8      ICMP 48 Echo (ping) request  id=0xb5c7, seq=113/28928, ttl=64
2 0.035470191      8.8.8.8 → 10.0.0.4     ICMP 48 Echo (ping) reply    id=0xb5c7, seq=113/28928, ttl=47 (request in 1)