What is the difference between sending IP packets (from user-space) to a tun device and using a raw socket?
For the purpose of tunneling IP packets through user-space. Why would I use one method over the other?
raw socket:
s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
send(s, ip_pkt, len, 0);
tun device:
struct ifreq ifr;
fd = open("/dev/net/tun", O_RDWR);
ifr.ifr_flags = IFF_TUN;
ioctl(fd, TUNSETIFF, (void *) &ifr)
send(s, ip_pkt, len, 0);
A raw socket is a type of socket that allows access to the underlying transport provider. This topic focuses only on raw sockets and the IPv4 and IPv6 protocols. This is because most other protocols with the exception of ATM do not support raw sockets.
A raw socket is a type of network socket which allows a software application on the computer to send and obtain packets of information from the network without using the computer's operating system as a middleman.
RAW-sockets are an additional type of Internet socket available in addition to the well known DATAGRAM- and STREAM-sockets. They do allow the user to see and manipulate the information used for transmitting the data instead of hiding these details, like it is the case with the usually used STREAM- or DATAGRAM sockets.
Raw packet is used when you dont have any, the first bytes captured are directly the IPv6 or IPv4 header. Raw IP; the packet begins with an IPv4 or IPv6 header, with the "version" field of the header indicating whether it's an IPv4 or IPv6 header.
A TUN or a TAP device (just differeny layers in the OSI model), are actual virtual network cards that appear in all of the different tools like iptables, ifconfig, ip, route, tcpdump. So packets you write to this socket appear as they arrived remotely on the wire of this virtual network card.
SOCK_RAW method inserts the packet into the IP-stack, and it will appear as it it sent from a user-space application and should be output to a network card according to the routing table and/or flags configuring on the socket.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With