writing data to linux tun interface

时间:2018-08-22 13:55:48

标签: tun

i have created a linux tun interface, set ipaddr, broadcast etc.. using open/ioctl apis. This is how the tun interface looks like,

TEST_TUN: mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 500 link/none inet 45.45.45.1/24 scope global TEST_TUN valid_lft forever preferred_lft forever

Any message written by a virtual host(binded on addr 45.45.45.1:udp=7070) is received by tun_fd(fd returned during tun device creation).

If tun_fd writes an msg ( IP(dst=45.45.45.1)+transport(udp_dst=7070)+payload) is not received on the virtual host. wireshark capture shows that the packet is being received on the kernel side, but virtual host doesnot received any packet.

what could be the reasons for kernel not forwarding the packet to virtual host ?

1 个答案:

答案 0 :(得分:0)

是否已将您的 tun 设备开启?

如果没有,那么你可以简单地通过..

$sudo ifconfig tun0 up

你也可以通过ioctle命令来做到这一点..

如果您的设备已经处于启动状态,那么您的 linux destro 必须具有转发功能。 你可以通过..

#echo "1" > /proc/sys/net/ipv4/ip_forwarding

$sudo sysctl net.ipv4.ip_forward=1
$sudo sysctl -p

然后是路由规则..您必须设置路由规则,以便通过您的虚拟接口捕获所有流量..

$sudo ip route 128/1 dev tun

此命令将在您的内核路由表中添加一条路由,所有流量都将流经您的虚拟接口..

如果我理解正确,那么这可能对您有帮助..