As a personal project I want to build a TCP/IP stack using C/C++ and using a tap interface. I have a wlan0 interface (wireless) that is connected to the internet. And now I want to send and receive packets from the internet through this wlan0 interface.
How can I do this?
These are the commands I used to create my tap interface:
sudo ip tuntap add name tap1 mode tap
sudo ip link set tap1 up
sudo ip link set tap1 promisc on
sudo ip addr add 10.0.0.2/24 dev tap1
sudo ip route add dev tap1 10.0.0.0/24
this is the output of ip a
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether dc:f5:05:2c:12:a1 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.14/24 brd 192.168.1.255 scope global dynamic noprefixroute wlan0
valid_lft 84147sec preferred_lft 84147sec
inet6 fe80::1e59:c571:bc2f:66b9/64 scope link noprefixroute
valid_lft forever preferred_lft forever
6: tap1: <BROADCAST,MULTICAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether da:e8:0d:67:e7:fd brd ff:ff:ff:ff:ff:ff
inet 10.0.0.2/24 scope global tap1
valid_lft forever preferred_lft forever
inet6 fe80::d8e8:dff:fe67:e7fd/64 scope link
valid_lft forever preferred_lft forever
This is the code if you want to test it (I am still writing it) https://github.com/Bechir-Brahem/tuntap-device
The tap device is only a virtual ethernet interface - what you send on it, you can read it back on a device file (/dev/tap
), and what you write into this device, you get as incoming packet on tap0
.
What you can do:
You can do this by netlink or raw sockets. Essentially, it is a special socket type, you can send and receive raw ethernet packets on it.
You can bridge tap0
and wlan0
into a br0
bridge with the brctl
command. Wifi and ethernet interfaces can not be bridged together (they are different on the ethernet level, a 802.11 packet is meaningless on 802.3 and vice versa).
Probably you can not create a well-working tcp implementation below a hundred kB of C code. It is because tcp is only simple on the user level.