如何创建两个tun进行通信?点对点是什么意思?

发布于 2025-01-14 01:10:19 字数 1332 浏览 0 评论 0原文

为了在用户空间中实现 TCP 堆栈,我尝试设置两个 tun 设备并在它们之间交换数据以测试代码。但是,似乎所有写入 tun 的 IP 数据包都被丢弃。

例如:

tun0,ip:172.19.16.1/20。

tun1,IP:172.19.32.1/20。

当我使用 ping 172.19.16.2 时,tun 0 可以接收 ICMP 数据包(从 172.19.16.1 到 172.19.16.2)并将数据写入 tun0 进行回复。但是当我尝试从 tun0 发送 ICMP 到 tun1(172.19.16.1 到 172.19.32.1 或反之亦然)时,它失败了。 tun1 无法接收任何数据!为什么?我尝试从 tun1 向 tun0 发送 TCP 数据包,但也失败了。

从内核文档中,我知道tun是一个点对点设备,没有mac地址和arp。点对点是什么意思?可以创建两个或三个tun设备来相互通信吗?

import fcntl
import os
import if_tun
import ctypes

import struct
from scapy.all import *


from if_tun import IfReq, TUNSETIFF, IFF_TUN


def register_tun(name: str):
    fd = os.open("/dev/net/tun",os.O_RDWR)
    if fd < 0:
        return fd

    r = IfReq()

    ctypes.memset(ctypes.byref(r), 0, ctypes.sizeof(r))
    r.ifr_ifru.ifru_flags = IFF_TUN | 0x1000
    r.ifr_ifrn.ifrn_name = name.encode("utf-8")
    
    fcntl.ioctl(fd, TUNSETIFF,r)
    return fd


if __name__ == "__main__":
    fd = register_tun("tun2")
    if fd < 0:
        print("error")
    while True:
        type = input()
        a = IP(dst="172.19.16.1",src="172.19.32.1")/TCP()        
        a = IP(raw(a))
        a.show()
        print("write:")
        print(os.write(fd, raw(a)))
        buf = os.read(fd,1024)
        print("receive data")
        IP(raw(buf)).show()

In order to implement a tcp stack in userspace, I try to set two tun device and exechange data between them for testing code.However, it seems like that all IP packet wrote to tun are dropped.

For example:

tun0,ip:172.19.16.1/20.

tun1,ip:172.19.32.1/20.

when I use ping 172.19.16.2,tun 0 can receive ICMP packet(from 172.19.16.1 to 172.19.16.2) and write data to tun0 for replying. But when I try to send a ICMP from tun0 to tun1(172.19.16.1 to 172.19.32.1 or vice versa), it failed. tun1 can't receive any data! Why? I try to send TCP packet from tun1 to tun0, it also failed.

From kernel document,I know tun is a point-to-point device and haven't mac address and arp. What does point-to-point mean? Can create two or three tun device for communicating each other?

import fcntl
import os
import if_tun
import ctypes

import struct
from scapy.all import *


from if_tun import IfReq, TUNSETIFF, IFF_TUN


def register_tun(name: str):
    fd = os.open("/dev/net/tun",os.O_RDWR)
    if fd < 0:
        return fd

    r = IfReq()

    ctypes.memset(ctypes.byref(r), 0, ctypes.sizeof(r))
    r.ifr_ifru.ifru_flags = IFF_TUN | 0x1000
    r.ifr_ifrn.ifrn_name = name.encode("utf-8")
    
    fcntl.ioctl(fd, TUNSETIFF,r)
    return fd


if __name__ == "__main__":
    fd = register_tun("tun2")
    if fd < 0:
        print("error")
    while True:
        type = input()
        a = IP(dst="172.19.16.1",src="172.19.32.1")/TCP()        
        a = IP(raw(a))
        a.show()
        print("write:")
        print(os.write(fd, raw(a)))
        buf = os.read(fd,1024)
        print("receive data")
        IP(raw(buf)).show()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心是晴朗的。 2025-01-21 01:10:19

2 个 TUN 是否来自同一台机器?如果是这样,您需要在两个 TUN 上设置 sysctl net.ipv4.conf.tun0.accept_local=1。否则,内核将不会接受具有本地源 IP 的数据包。

来源:https://unix.stackexchange.com/questions/ 597213/routing-all-packets-trough-tun-device-before-forwarding

Are the 2 TUNs from the same machine? If so, you need to set the sysctl net.ipv4.conf.tun0.accept_local=1 on both TUNs. Otherwise, the kernel won't accept packets with a local source IP.

Source: https://unix.stackexchange.com/questions/597213/routing-all-packets-trough-tun-device-before-forwarding

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文