Python program writing to TUN interface doesn't get any response?

26 views Asked by At

I am trying to implement a basic tunnelling script in python. I read data from TUN and then write it to a TCP wrapper library I wrote.

HOST A = 192.0.2.2 while B is 192.0.2.3.

When i run "ping 192.0.2.3" from host A, i see that the data was successfully sent over tcp to host B. However when host B writes to the TUN device, it isn't getting any response.

import struct
from fcntl import ioctl
import select
import Neo

def openTun(tunName):
    tun = open("/dev/net/tun", "r+b", buffering=0)
    LINUX_IFF_TUN = 0x0001
    LINUX_IFF_NO_PI = 0x1000
    LINUX_TUNSETIFF = 0x400454CA
    flags = LINUX_IFF_TUN | LINUX_IFF_NO_PI
    ifs = struct.pack("16sH22s", tunName, flags, b"")
    ioctl(tun, LINUX_TUNSETIFF, ifs)
    return tun

def serverUp():
    comm = Neo.Neo()
    comm = Neo.Neo()
    comm.start_server()
    comm.get_new_conn()
    return comm

def clientUp():
    comm = Neo.Neo()
    comm.connect_client("192.168.127.192")
    return comm


ttype = "server"# i change this to client on host B



tun = openTun(b"tun0")
if ttype == "server":
    tcp = serverUp()
else:
    tcp = clientUp()
    
print("sockets are up")

while True:
    inputs = [tun, tcp.sock]
    outputs = []
    inputs,outputs,errs = select.select(inputs, outputs, inputs)
    for fd in inputs:
        if fd == tun:
            data = tun.read(2000)
            print(data, "from tun")
            tcp.send_data(data)
            print("sent")
        if fd == tcp.sock:
            data = tcp.receive_data()
            print(data, "from tcp")
            tun.write(data)

Also, my routing table on HOST B looks like this -

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.79.30   0.0.0.0         UG    100    0        0 enp0s3
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 enp0s3
192.0.2.0       192.0.2.3       255.255.255.0   UG    0      0        0 tun0
192.0.2.2       0.0.0.0         255.255.255.255 UH    0      0        0 tun0
192.168.79.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s3

I would greatly appreciate any advice on how to go about fixing this issue.

I tried double checking the file descriptors given to the select command. I verified via ping that they are able to read from the TUNs, but it's not working for packets received on the wire.

0

There are 0 answers