I am trying to make a C program that will create a tun device interface for my pc physical interface. The entire idea in doing is that when I write any raw packets on my tunnel device, it will put that in my actual physical interface. My question is how do I map the tun device with the actual physical interface?

For example, my eth1 has ip address - How can I create a tun device so that I can read/write on the eth1 interface? Do I need to give the same ip address or a different ip address? Is route only way that I will write packets on tun device and will create a route that will have a default gateway of tun device as a phy device.

I am following this example, but I am unclear on the concept yet.

I have create a tunnel device by the following commands. My PC has got one real ethernet interface. This ethernet interface ip address connected to bunch of PCs and then to the gateway server to access internet.

I am trying to make a C program that will write OSPF hello packets on to the interface.

In order to do so, I am creating a tunnel device so that I can write OSPF hello packets on it. But before I am doing that, I am checking the tunnel device. Here my tunnel device is and I am setting the route get My understanding is that all the packets received or sent on the actual interface i.e. will be received by this tunnel device interface, whose IP address is But, when I did a ping to, the RX / Tx packets received are still zero. So, I think my tunnel isn't receiving any packets.

ip tuntap add mode tun dev tun0
ip addr add dev tun0
ip link set dev tun0 up
ip route get

enter image description here

enter image description here

Am I missing any concept here? How can I do so? I am still struggling in getting my tunnel device concepts clear.


You can't use a tun interface to "put actual packets in an existing interface". A tun interface is always a new network interface, distinct from all existing interfaces.

But you can use packet sockets to write raw packets to (or read raw packets from) an existing network interface. See man 7 packet for details. But you are bypassing all protocol layers with this, so you have to implement the correct protocol behaviour yourself, so unless you really, really know what you are doing, don't do this.

You didn't say what you actually want to achieve, and you seem to be a bit confused about the concepts, so this may be an XY problem, and it's entirely possible that neither a tun interface nor packet sockets are what you really need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.