# tun/tap programming



## jphartmann (Dec 18, 2013)

I'm writing code to forward IP frames from a mainframe system CTCA to the network.

First I create and configure the interface and change owner of the /dev/tun0 special file:

```
[root@fb91 ~]# ifconfig tun0 create inet 10.0.0.103  10.0.0.104
[root@fb91 ~]# ifconfig tun0
tun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> metric 0 mtu 1500
        options=80000<LINKSTATE>
        inet 10.0.0.103 --> 10.0.0.104 netmask 0xff000000 
        inet6 fe80::7254:d2ff:fe45:e0a0%tun0 prefixlen 64 scopeid 0x5 
        nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
[root@fb91 ~]# ls -l /dev/tun*
crw-------  1 uucp  dialer    0, 129 Dec 18 12:52 /dev/tun0
[root@fb91 ~]# 
[root@fb91 ~]# chown john:john /dev/tun0
[root@fb91 ~]# ls -l /dev/tun*
crw-------  1 john  john    0, 129 Dec 18 12:52 /dev/tun0
```
So far so good.  

Then from my own user I open /dev/tun0 and set up a read. I ping 10.0.0.104 and receive an IP packet. Everything works. In particular, I did not need to issue any ioctl() call at all.

Splendid.  So `man tun` is wrong in stating that I must be root to open the character device.

But wait:

```
[john@fb91 ~/src/testing]$ ifconfig tun0
tun0: flags=8010<POINTOPOINT,MULTICAST> metric 0 mtu 1500
        options=80000<LINKSTATE>
        nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
```
Closing the file seems to deconfigure the IP address while leaving the interface and the character device intact.

My questions are:


* Is there any way to make the IP address assignment permanent (until the next boot)?
* Apart from man tun is there any documentation of using /dev/tun<n> directly?
* What else ought I to know?
Thanks,


----------

